|
| 1 | +// Copyright 2022 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package admin |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | + |
| 11 | + "code.gitea.io/gitea/models/auth" |
| 12 | + "code.gitea.io/gitea/modules/base" |
| 13 | + "code.gitea.io/gitea/modules/context" |
| 14 | + "code.gitea.io/gitea/modules/setting" |
| 15 | + user_setting "code.gitea.io/gitea/routers/web/user/setting" |
| 16 | +) |
| 17 | + |
| 18 | +var ( |
| 19 | + tplSettingsApplications base.TplName = "admin/applications/list" |
| 20 | + tplSettingsOauth2ApplicationEdit base.TplName = "admin/applications/oauth2_edit" |
| 21 | +) |
| 22 | + |
| 23 | +func newOAuth2CommonHandlers() *user_setting.OAuth2CommonHandlers { |
| 24 | + return &user_setting.OAuth2CommonHandlers{ |
| 25 | + OwnerID: 0, |
| 26 | + BasePathList: fmt.Sprintf("%s/admin/applications", setting.AppSubURL), |
| 27 | + BasePathEditPrefix: fmt.Sprintf("%s/admin/applications/oauth2", setting.AppSubURL), |
| 28 | + TplAppEdit: tplSettingsOauth2ApplicationEdit, |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// Applications render org applications page (for org, at the moment, there are only OAuth2 applications) |
| 33 | +func Applications(ctx *context.Context) { |
| 34 | + ctx.Data["Title"] = ctx.Tr("settings.applications") |
| 35 | + ctx.Data["PageIsAdmin"] = true |
| 36 | + ctx.Data["PageIsAdminApplications"] = true |
| 37 | + |
| 38 | + apps, err := auth.GetOAuth2ApplicationsByUserID(ctx, 0) |
| 39 | + if err != nil { |
| 40 | + ctx.ServerError("GetOAuth2ApplicationsByUserID", err) |
| 41 | + return |
| 42 | + } |
| 43 | + ctx.Data["Applications"] = apps |
| 44 | + |
| 45 | + ctx.HTML(http.StatusOK, tplSettingsApplications) |
| 46 | +} |
| 47 | + |
| 48 | +// ApplicationsPost response for adding an oauth2 application |
| 49 | +func ApplicationsPost(ctx *context.Context) { |
| 50 | + ctx.Data["Title"] = ctx.Tr("settings.applications") |
| 51 | + ctx.Data["PageIsAdmin"] = true |
| 52 | + ctx.Data["PageIsAdminApplications"] = true |
| 53 | + |
| 54 | + oa := newOAuth2CommonHandlers() |
| 55 | + oa.AddApp(ctx) |
| 56 | +} |
| 57 | + |
| 58 | +// EditApplication displays the given application |
| 59 | +func EditApplication(ctx *context.Context) { |
| 60 | + ctx.Data["PageIsAdmin"] = true |
| 61 | + ctx.Data["PageIsAdminApplications"] = true |
| 62 | + |
| 63 | + oa := newOAuth2CommonHandlers() |
| 64 | + oa.EditShow(ctx) |
| 65 | +} |
| 66 | + |
| 67 | +// EditApplicationPost response for editing oauth2 application |
| 68 | +func EditApplicationPost(ctx *context.Context) { |
| 69 | + ctx.Data["Title"] = ctx.Tr("settings.applications") |
| 70 | + ctx.Data["PageIsAdmin"] = true |
| 71 | + ctx.Data["PageIsAdminApplications"] = true |
| 72 | + |
| 73 | + oa := newOAuth2CommonHandlers() |
| 74 | + oa.EditSave(ctx) |
| 75 | +} |
| 76 | + |
| 77 | +// ApplicationsRegenerateSecret handles the post request for regenerating the secret |
| 78 | +func ApplicationsRegenerateSecret(ctx *context.Context) { |
| 79 | + ctx.Data["Title"] = ctx.Tr("settings") |
| 80 | + ctx.Data["PageIsAdmin"] = true |
| 81 | + ctx.Data["PageIsAdminApplications"] = true |
| 82 | + |
| 83 | + oa := newOAuth2CommonHandlers() |
| 84 | + oa.RegenerateSecret(ctx) |
| 85 | +} |
| 86 | + |
| 87 | +// DeleteApplication deletes the given oauth2 application |
| 88 | +func DeleteApplication(ctx *context.Context) { |
| 89 | + oa := newOAuth2CommonHandlers() |
| 90 | + oa.DeleteApp(ctx) |
| 91 | +} |
| 92 | + |
| 93 | +// TODO: revokes the grant with the given id |
0 commit comments