You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.0 KiB

  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package setting
  6. import (
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/auth"
  9. "code.gitea.io/gitea/modules/base"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/setting"
  12. )
  13. const (
  14. tplSettingsApplications base.TplName = "user/settings/applications"
  15. )
  16. // Applications render manage access token page
  17. func Applications(ctx *context.Context) {
  18. ctx.Data["Title"] = ctx.Tr("settings")
  19. ctx.Data["PageIsSettingsApplications"] = true
  20. loadApplicationsData(ctx)
  21. ctx.HTML(200, tplSettingsApplications)
  22. }
  23. // ApplicationsPost response for add user's access token
  24. func ApplicationsPost(ctx *context.Context, form auth.NewAccessTokenForm) {
  25. ctx.Data["Title"] = ctx.Tr("settings")
  26. ctx.Data["PageIsSettingsApplications"] = true
  27. if ctx.HasError() {
  28. loadApplicationsData(ctx)
  29. ctx.HTML(200, tplSettingsApplications)
  30. return
  31. }
  32. t := &models.AccessToken{
  33. UID: ctx.User.ID,
  34. Name: form.Name,
  35. }
  36. if err := models.NewAccessToken(t); err != nil {
  37. ctx.ServerError("NewAccessToken", err)
  38. return
  39. }
  40. ctx.Flash.Success(ctx.Tr("settings.generate_token_success"))
  41. ctx.Flash.Info(t.Sha1)
  42. ctx.Redirect(setting.AppSubURL + "/user/settings/applications")
  43. }
  44. // DeleteApplication response for delete user access token
  45. func DeleteApplication(ctx *context.Context) {
  46. if err := models.DeleteAccessTokenByID(ctx.QueryInt64("id"), ctx.User.ID); err != nil {
  47. ctx.Flash.Error("DeleteAccessTokenByID: " + err.Error())
  48. } else {
  49. ctx.Flash.Success(ctx.Tr("settings.delete_token_success"))
  50. }
  51. ctx.JSON(200, map[string]interface{}{
  52. "redirect": setting.AppSubURL + "/user/settings/applications",
  53. })
  54. }
  55. func loadApplicationsData(ctx *context.Context) {
  56. tokens, err := models.ListAccessTokens(ctx.User.ID)
  57. if err != nil {
  58. ctx.ServerError("ListAccessTokens", err)
  59. return
  60. }
  61. ctx.Data["Tokens"] = tokens
  62. }