From ed664a9e1dae4d4660e60c981173bbc5102e69ea Mon Sep 17 00:00:00 2001 From: John Olheiser Date: Tue, 25 Feb 2020 17:42:43 -0600 Subject: [PATCH] Change admin dashboard to POST (#10465) (#10466) * Change admin dashboard to POST (#10465) * Add form and convert to POST * Redirect for flash * Convert octicons back to fa for 1.11 Signed-off-by: jolheiser --- modules/auth/admin.go | 10 ++++ routers/admin/admin.go | 30 +++++++---- routers/routes/routes.go | 1 + templates/admin/dashboard.tmpl | 91 ++++++++++++++++++---------------- web_src/less/_admin.less | 4 ++ 5 files changed, 81 insertions(+), 55 deletions(-) diff --git a/modules/auth/admin.go b/modules/auth/admin.go index 6e225891d..bebb4055b 100644 --- a/modules/auth/admin.go +++ b/modules/auth/admin.go @@ -47,3 +47,13 @@ type AdminEditUserForm struct { func (f *AdminEditUserForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { return validate(errs, ctx.Data, f, ctx.Locale) } + +// AdminDashboardForm form for admin dashboard operations +type AdminDashboardForm struct { + Op int `binding:"required"` +} + +// Validate validates form fields +func (f *AdminDashboardForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { + return validate(errs, ctx.Data, f, ctx.Locale) +} diff --git a/routers/admin/admin.go b/routers/admin/admin.go index 055b8f5a5..e8237f5b4 100644 --- a/routers/admin/admin.go +++ b/routers/admin/admin.go @@ -16,6 +16,7 @@ import ( "time" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/auth" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/cron" @@ -30,7 +31,6 @@ import ( "gitea.com/macaron/macaron" "gitea.com/macaron/session" - "github.com/unknwon/com" ) const ( @@ -144,14 +144,28 @@ func Dashboard(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("admin.dashboard") ctx.Data["PageIsAdmin"] = true ctx.Data["PageIsAdminDashboard"] = true + ctx.Data["Stats"] = models.GetStatistic() + // FIXME: update periodically + updateSystemStatus() + ctx.Data["SysStatus"] = sysStatus + ctx.HTML(200, tplDashboard) +} + +// DashboardPost run an admin operation +func DashboardPost(ctx *context.Context, form auth.AdminDashboardForm) { + ctx.Data["Title"] = ctx.Tr("admin.dashboard") + ctx.Data["PageIsAdmin"] = true + ctx.Data["PageIsAdminDashboard"] = true + ctx.Data["Stats"] = models.GetStatistic() + updateSystemStatus() + ctx.Data["SysStatus"] = sysStatus // Run operation. - op, _ := com.StrTo(ctx.Query("op")).Int() - if op > 0 { + if form.Op > 0 { var err error var success string - switch Operation(op) { + switch Operation(form.Op) { case cleanInactivateUser: success = ctx.Tr("admin.dashboard.delete_inactivate_accounts_success") err = models.DeleteInactivateUsers() @@ -189,15 +203,9 @@ func Dashboard(ctx *context.Context) { } else { ctx.Flash.Success(success) } - ctx.Redirect(setting.AppSubURL + "/admin") - return } - ctx.Data["Stats"] = models.GetStatistic() - // FIXME: update periodically - updateSystemStatus() - ctx.Data["SysStatus"] = sysStatus - ctx.HTML(200, tplDashboard) + ctx.Redirect(setting.AppSubURL + "/admin") } // SendTestMail send test mail to confirm mail service is OK diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 6fec2c055..455c0b16e 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -408,6 +408,7 @@ func RegisterRoutes(m *macaron.Macaron) { // ***** START: Admin ***** m.Group("/admin", func() { m.Get("", adminReq, admin.Dashboard) + m.Post("", adminReq, bindIgnErr(auth.AdminDashboardForm{}), admin.DashboardPost) m.Get("/config", admin.Config) m.Post("/config/test_mail", admin.SendTestMail) m.Group("/monitor", func() { diff --git a/templates/admin/dashboard.tmpl b/templates/admin/dashboard.tmpl index 262db04b9..8ee63b541 100644 --- a/templates/admin/dashboard.tmpl +++ b/templates/admin/dashboard.tmpl @@ -15,50 +15,53 @@ {{.i18n.Tr "admin.dashboard.operations"}}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
{{.i18n.Tr "admin.dashboard.delete_inactivate_accounts"}} {{.i18n.Tr "admin.dashboard.operation_run"}}
{{.i18n.Tr "admin.dashboard.delete_repo_archives"}} {{.i18n.Tr "admin.dashboard.operation_run"}}
{{.i18n.Tr "admin.dashboard.delete_missing_repos"}} {{.i18n.Tr "admin.dashboard.operation_run"}}
{{.i18n.Tr "admin.dashboard.git_gc_repos"}} {{.i18n.Tr "admin.dashboard.operation_run"}}
{{.i18n.Tr "admin.dashboard.resync_all_sshkeys"}} {{.i18n.Tr "admin.dashboard.operation_run"}}
{{.i18n.Tr "admin.dashboard.resync_all_hooks"}} {{.i18n.Tr "admin.dashboard.operation_run"}}
{{.i18n.Tr "admin.dashboard.reinit_missing_repos"}} {{.i18n.Tr "admin.dashboard.operation_run"}}
{{.i18n.Tr "admin.dashboard.sync_external_users"}} {{.i18n.Tr "admin.dashboard.operation_run"}}
{{.i18n.Tr "admin.dashboard.git_fsck"}} {{.i18n.Tr "admin.dashboard.operation_run"}}
{{.i18n.Tr "admin.dashboard.delete_generated_repository_avatars"}} {{.i18n.Tr "admin.dashboard.operation_run"}}
+
+ {{.CsrfTokenHtml}} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
{{.i18n.Tr "admin.dashboard.delete_inactivate_accounts"}}
{{.i18n.Tr "admin.dashboard.delete_repo_archives"}}
{{.i18n.Tr "admin.dashboard.delete_missing_repos"}}
{{.i18n.Tr "admin.dashboard.git_gc_repos"}}
{{.i18n.Tr "admin.dashboard.resync_all_sshkeys"}}
{{.i18n.Tr "admin.dashboard.resync_all_hooks"}}
{{.i18n.Tr "admin.dashboard.reinit_missing_repos"}}
{{.i18n.Tr "admin.dashboard.sync_external_users"}}
{{.i18n.Tr "admin.dashboard.git_fsck"}}
{{.i18n.Tr "admin.dashboard.delete_generated_repository_avatars"}}
+

diff --git a/web_src/less/_admin.less b/web_src/less/_admin.less index 0bd685142..15781cf77 100644 --- a/web_src/less/_admin.less +++ b/web_src/less/_admin.less @@ -28,6 +28,10 @@ } } } + + form button[type='submit'] { + padding: 5px 8px; + } } .ui.header,