From d4af0366f7b7d152b54c2dde93457253cb07ea0d Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Wed, 24 Jul 2024 01:27:50 +0000 Subject: [PATCH 1/3] add admin delete email --- models/user/email_address.go | 1 + options/locale/locale_en-US.ini | 4 ++++ routers/web/admin/emails.go | 31 +++++++++++++++++++++++++++++++ routers/web/web.go | 1 + templates/admin/emails/list.tmpl | 18 ++++++++++++++++++ 5 files changed, 55 insertions(+) diff --git a/models/user/email_address.go b/models/user/email_address.go index 71b96c00be131..5c04909ed7c59 100644 --- a/models/user/email_address.go +++ b/models/user/email_address.go @@ -395,6 +395,7 @@ type SearchEmailOptions struct { // SearchEmailResult is an e-mail address found in the user or email_address table type SearchEmailResult struct { + ID int64 UID int64 Email string IsActivated bool diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 746288bb9a0b9..6a748aed0022f 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -2982,6 +2982,10 @@ emails.not_updated = Failed to update the requested email address: %v emails.duplicate_active = This email address is already active for a different user. emails.change_email_header = Update Email Properties emails.change_email_text = Are you sure you want to update this email address? +emails.delete = Delete Email +emails.delete_desc = Are you sure you want to delete this email address? +emails.deletion_success = The email address has been deleted. +emails.delete_primary_email_error = You can not delete the primary email. orgs.org_manage_panel = Organization Management orgs.name = Name diff --git a/routers/web/admin/emails.go b/routers/web/admin/emails.go index 2cf4035c6a808..97dc22b73349a 100644 --- a/routers/web/admin/emails.go +++ b/routers/web/admin/emails.go @@ -15,6 +15,7 @@ import ( "code.gitea.io/gitea/modules/optional" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/services/context" + "code.gitea.io/gitea/services/user" ) const ( @@ -150,3 +151,33 @@ func ActivateEmail(ctx *context.Context) { redirect.RawQuery = q.Encode() ctx.Redirect(redirect.String()) } + +// DeleteEmail serves a POST request for delete a user's email +func DeleteEmail(ctx *context.Context) { + u, err := user_model.GetUserByID(ctx, ctx.FormInt64("Uid")) + if err != nil || u == nil { + ctx.ServerError("GetUserByID", err) + return + } + + email, err := user_model.GetEmailAddressByID(ctx, u.ID, ctx.FormInt64("id")) + if err != nil || email == nil { + ctx.ServerError("GetEmailAddressByID", err) + return + } + + redirect := setting.AppSubURL + "/admin/emails" + if err := user.DeleteEmailAddresses(ctx, u, []string{email.Email}); err != nil { + if user_model.IsErrPrimaryEmailCannotDelete(err) { + ctx.Flash.Error(ctx.Tr("admin.emails.delete_primary_email_error")) + ctx.JSONRedirect(redirect) + return + } + ctx.ServerError("DeleteEmailAddresses", err) + return + } + log.Trace("Email address deleted: %s %s", u.Name, email.Email) + + ctx.Flash.Success(ctx.Tr("admin.emails.deletion_success")) + ctx.JSONRedirect(redirect) +} diff --git a/routers/web/web.go b/routers/web/web.go index d08e8da772857..0e16c1ca6c9cf 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -725,6 +725,7 @@ func registerRoutes(m *web.Router) { m.Group("/emails", func() { m.Get("", admin.Emails) m.Post("/activate", admin.ActivateEmail) + m.Post("/delete", admin.DeleteEmail) }) m.Group("/orgs", func() { diff --git a/templates/admin/emails/list.tmpl b/templates/admin/emails/list.tmpl index 1f226afcc4ccc..7274babc3abb4 100644 --- a/templates/admin/emails/list.tmpl +++ b/templates/admin/emails/list.tmpl @@ -38,6 +38,7 @@ {{ctx.Locale.Tr "admin.emails.primary"}} {{ctx.Locale.Tr "admin.emails.activated"}} + @@ -59,6 +60,11 @@ {{svg (Iif .IsActivated "octicon-check" "octicon-x")}} {{end}} + +
+ {{svg "octicon-trash"}} +
+ {{end}} @@ -95,4 +101,16 @@ + + + {{template "admin/layout_footer" .}} From afcdb892ac7d4b9757d35755ff30559e784666e7 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Wed, 24 Jul 2024 11:16:07 +0900 Subject: [PATCH 2/3] Update templates/admin/emails/list.tmpl Co-authored-by: Jason Song --- templates/admin/emails/list.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/admin/emails/list.tmpl b/templates/admin/emails/list.tmpl index 7274babc3abb4..93fbb9dfc232d 100644 --- a/templates/admin/emails/list.tmpl +++ b/templates/admin/emails/list.tmpl @@ -62,7 +62,7 @@
- {{svg "octicon-trash"}} + {{svg "octicon-trash"}}
From 73a9bc559cafd096707b05d6c9d4b8403b5a9c97 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Wed, 24 Jul 2024 06:12:17 +0000 Subject: [PATCH 3/3] follow wxiaoguang's suggestion --- routers/web/admin/emails.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/routers/web/admin/emails.go b/routers/web/admin/emails.go index 97dc22b73349a..f0d85550706fe 100644 --- a/routers/web/admin/emails.go +++ b/routers/web/admin/emails.go @@ -166,11 +166,10 @@ func DeleteEmail(ctx *context.Context) { return } - redirect := setting.AppSubURL + "/admin/emails" if err := user.DeleteEmailAddresses(ctx, u, []string{email.Email}); err != nil { if user_model.IsErrPrimaryEmailCannotDelete(err) { ctx.Flash.Error(ctx.Tr("admin.emails.delete_primary_email_error")) - ctx.JSONRedirect(redirect) + ctx.JSONRedirect("") return } ctx.ServerError("DeleteEmailAddresses", err) @@ -179,5 +178,5 @@ func DeleteEmail(ctx *context.Context) { log.Trace("Email address deleted: %s %s", u.Name, email.Email) ctx.Flash.Success(ctx.Tr("admin.emails.deletion_success")) - ctx.JSONRedirect(redirect) + ctx.JSONRedirect("") }