gitea源码

applications.go 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package setting
  5. import (
  6. "net/http"
  7. "strings"
  8. auth_model "code.gitea.io/gitea/models/auth"
  9. "code.gitea.io/gitea/models/db"
  10. user_model "code.gitea.io/gitea/models/user"
  11. "code.gitea.io/gitea/modules/setting"
  12. "code.gitea.io/gitea/modules/templates"
  13. "code.gitea.io/gitea/modules/util"
  14. "code.gitea.io/gitea/modules/web"
  15. "code.gitea.io/gitea/services/context"
  16. "code.gitea.io/gitea/services/forms"
  17. )
  18. const (
  19. tplSettingsApplications templates.TplName = "user/settings/applications"
  20. )
  21. // Applications render manage access token page
  22. func Applications(ctx *context.Context) {
  23. ctx.Data["Title"] = ctx.Tr("settings.applications")
  24. ctx.Data["PageIsSettingsApplications"] = true
  25. ctx.Data["UserDisabledFeatures"] = user_model.DisabledFeaturesWithLoginType(ctx.Doer)
  26. loadApplicationsData(ctx)
  27. ctx.HTML(http.StatusOK, tplSettingsApplications)
  28. }
  29. // ApplicationsPost response for add user's access token
  30. func ApplicationsPost(ctx *context.Context) {
  31. form := web.GetForm(ctx).(*forms.NewAccessTokenForm)
  32. ctx.Data["Title"] = ctx.Tr("settings")
  33. ctx.Data["PageIsSettingsApplications"] = true
  34. ctx.Data["UserDisabledFeatures"] = user_model.DisabledFeaturesWithLoginType(ctx.Doer)
  35. _ = ctx.Req.ParseForm()
  36. var scopeNames []string
  37. const accessTokenScopePrefix = "scope-"
  38. for k, v := range ctx.Req.Form {
  39. if strings.HasPrefix(k, accessTokenScopePrefix) {
  40. scopeNames = append(scopeNames, v...)
  41. }
  42. }
  43. scope, err := auth_model.AccessTokenScope(strings.Join(scopeNames, ",")).Normalize()
  44. if err != nil {
  45. ctx.ServerError("GetScope", err)
  46. return
  47. }
  48. if !scope.HasPermissionScope() {
  49. ctx.Flash.Error(ctx.Tr("settings.at_least_one_permission"), true)
  50. }
  51. if ctx.HasError() {
  52. loadApplicationsData(ctx)
  53. ctx.HTML(http.StatusOK, tplSettingsApplications)
  54. return
  55. }
  56. t := &auth_model.AccessToken{
  57. UID: ctx.Doer.ID,
  58. Name: form.Name,
  59. Scope: scope,
  60. }
  61. exist, err := auth_model.AccessTokenByNameExists(ctx, t)
  62. if err != nil {
  63. ctx.ServerError("AccessTokenByNameExists", err)
  64. return
  65. }
  66. if exist {
  67. ctx.Flash.Error(ctx.Tr("settings.generate_token_name_duplicate", t.Name))
  68. ctx.Redirect(setting.AppSubURL + "/user/settings/applications")
  69. return
  70. }
  71. if err := auth_model.NewAccessToken(ctx, t); err != nil {
  72. ctx.ServerError("NewAccessToken", err)
  73. return
  74. }
  75. ctx.Flash.Success(ctx.Tr("settings.generate_token_success"))
  76. ctx.Flash.Info(t.Token)
  77. ctx.Redirect(setting.AppSubURL + "/user/settings/applications")
  78. }
  79. // DeleteApplication response for delete user access token
  80. func DeleteApplication(ctx *context.Context) {
  81. if err := auth_model.DeleteAccessTokenByID(ctx, ctx.FormInt64("id"), ctx.Doer.ID); err != nil {
  82. ctx.Flash.Error("DeleteAccessTokenByID: " + err.Error())
  83. } else {
  84. ctx.Flash.Success(ctx.Tr("settings.delete_token_success"))
  85. }
  86. ctx.JSONRedirect(setting.AppSubURL + "/user/settings/applications")
  87. }
  88. func loadApplicationsData(ctx *context.Context) {
  89. ctx.Data["AccessTokenScopePublicOnly"] = auth_model.AccessTokenScopePublicOnly
  90. tokens, err := db.Find[auth_model.AccessToken](ctx, auth_model.ListAccessTokensOptions{UserID: ctx.Doer.ID})
  91. if err != nil {
  92. ctx.ServerError("ListAccessTokens", err)
  93. return
  94. }
  95. ctx.Data["Tokens"] = tokens
  96. ctx.Data["EnableOAuth2"] = setting.OAuth2.Enabled
  97. // Handle specific ordered token categories for admin or non-admin users
  98. tokenCategoryNames := auth_model.GetAccessTokenCategories()
  99. if !ctx.Doer.IsAdmin {
  100. tokenCategoryNames = util.SliceRemoveAll(tokenCategoryNames, "admin")
  101. }
  102. ctx.Data["TokenCategories"] = tokenCategoryNames
  103. if setting.OAuth2.Enabled {
  104. ctx.Data["Applications"], err = db.Find[auth_model.OAuth2Application](ctx, auth_model.FindOAuth2ApplicationsOptions{
  105. OwnerID: ctx.Doer.ID,
  106. })
  107. if err != nil {
  108. ctx.ServerError("GetOAuth2ApplicationsByUserID", err)
  109. return
  110. }
  111. ctx.Data["Grants"], err = auth_model.GetOAuth2GrantsByUserID(ctx, ctx.Doer.ID)
  112. if err != nil {
  113. ctx.ServerError("GetOAuth2GrantsByUserID", err)
  114. return
  115. }
  116. }
  117. }