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.

applications.go 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.Token)
  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. ctx.Data["EnableOAuth2"] = setting.OAuth2.Enable
  63. if setting.OAuth2.Enable {
  64. ctx.Data["Applications"], err = models.GetOAuth2ApplicationsByUserID(ctx.User.ID)
  65. if err != nil {
  66. ctx.ServerError("GetOAuth2ApplicationsByUserID", err)
  67. return
  68. }
  69. ctx.Data["Grants"], err = models.GetOAuth2GrantsByUserID(ctx.User.ID)
  70. if err != nil {
  71. ctx.ServerError("GetOAuth2GrantsByUserID", err)
  72. return
  73. }
  74. }
  75. }