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