gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package org
  4. import (
  5. "fmt"
  6. "net/http"
  7. "code.gitea.io/gitea/models/auth"
  8. "code.gitea.io/gitea/models/db"
  9. "code.gitea.io/gitea/modules/setting"
  10. "code.gitea.io/gitea/modules/templates"
  11. shared_user "code.gitea.io/gitea/routers/web/shared/user"
  12. user_setting "code.gitea.io/gitea/routers/web/user/setting"
  13. "code.gitea.io/gitea/services/context"
  14. )
  15. const (
  16. tplSettingsApplications templates.TplName = "org/settings/applications"
  17. tplSettingsOAuthApplicationEdit templates.TplName = "org/settings/applications_oauth2_edit"
  18. )
  19. func newOAuth2CommonHandlers(org *context.Organization) *user_setting.OAuth2CommonHandlers {
  20. return &user_setting.OAuth2CommonHandlers{
  21. OwnerID: org.Organization.ID,
  22. BasePathList: fmt.Sprintf("%s/org/%s/settings/applications", setting.AppSubURL, org.Organization.Name),
  23. BasePathEditPrefix: fmt.Sprintf("%s/org/%s/settings/applications/oauth2", setting.AppSubURL, org.Organization.Name),
  24. TplAppEdit: tplSettingsOAuthApplicationEdit,
  25. }
  26. }
  27. // Applications render org applications page (for org, at the moment, there are only OAuth2 applications)
  28. func Applications(ctx *context.Context) {
  29. ctx.Data["Title"] = ctx.Tr("settings.applications")
  30. ctx.Data["PageIsOrgSettings"] = true
  31. ctx.Data["PageIsSettingsApplications"] = true
  32. apps, err := db.Find[auth.OAuth2Application](ctx, auth.FindOAuth2ApplicationsOptions{
  33. OwnerID: ctx.Org.Organization.ID,
  34. })
  35. if err != nil {
  36. ctx.ServerError("GetOAuth2ApplicationsByUserID", err)
  37. return
  38. }
  39. ctx.Data["Applications"] = apps
  40. if _, err := shared_user.RenderUserOrgHeader(ctx); err != nil {
  41. ctx.ServerError("RenderUserOrgHeader", err)
  42. return
  43. }
  44. ctx.HTML(http.StatusOK, tplSettingsApplications)
  45. }
  46. // OAuthApplicationsPost response for adding an oauth2 application
  47. func OAuthApplicationsPost(ctx *context.Context) {
  48. ctx.Data["Title"] = ctx.Tr("settings.applications")
  49. ctx.Data["PageIsOrgSettings"] = true
  50. ctx.Data["PageIsSettingsApplications"] = true
  51. oa := newOAuth2CommonHandlers(ctx.Org)
  52. oa.AddApp(ctx)
  53. }
  54. // OAuth2ApplicationShow displays the given application
  55. func OAuth2ApplicationShow(ctx *context.Context) {
  56. ctx.Data["PageIsOrgSettings"] = true
  57. ctx.Data["PageIsSettingsApplications"] = true
  58. oa := newOAuth2CommonHandlers(ctx.Org)
  59. oa.EditShow(ctx)
  60. }
  61. // OAuth2ApplicationEdit response for editing oauth2 application
  62. func OAuth2ApplicationEdit(ctx *context.Context) {
  63. ctx.Data["Title"] = ctx.Tr("settings.applications")
  64. ctx.Data["PageIsOrgSettings"] = true
  65. ctx.Data["PageIsSettingsApplications"] = true
  66. oa := newOAuth2CommonHandlers(ctx.Org)
  67. oa.EditSave(ctx)
  68. }
  69. // OAuthApplicationsRegenerateSecret handles the post request for regenerating the secret
  70. func OAuthApplicationsRegenerateSecret(ctx *context.Context) {
  71. ctx.Data["Title"] = ctx.Tr("settings")
  72. ctx.Data["PageIsOrgSettings"] = true
  73. ctx.Data["PageIsSettingsApplications"] = true
  74. oa := newOAuth2CommonHandlers(ctx.Org)
  75. oa.RegenerateSecret(ctx)
  76. }
  77. // DeleteOAuth2Application deletes the given oauth2 application
  78. func DeleteOAuth2Application(ctx *context.Context) {
  79. oa := newOAuth2CommonHandlers(ctx.Org)
  80. oa.DeleteApp(ctx)
  81. }
  82. // TODO: revokes the grant with the given id