From d0e52fd641655de8cd7738735b867cc35f988e87 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Thu, 25 Jul 2024 19:11:04 +0900 Subject: [PATCH] Support delete user email in admin panel (#31690) ![QQ_1721784609320](https://github.com/user-attachments/assets/23f08bf3-93f4-44d7-963d-10380ef8c1f1) ![QQ_1721784616403](https://github.com/user-attachments/assets/667cbd1e-5e21-4489-8d18-2a7be85190db) ![QQ_1721784626722](https://github.com/user-attachments/assets/495beb94-dfa2-481c-aa60-d5115cad1ae1) --------- Co-authored-by: Jason Song (cherry picked from commit cc044818c33ff066c4e5869c9e75de9707def6ed) --- models/user/email_address.go | 1 + options/locale/locale_en-US.ini | 4 ++++ routers/web/admin/emails.go | 30 ++++++++++++++++++++++++++++++ routers/web/web.go | 1 + templates/admin/emails/list.tmpl | 18 ++++++++++++++++++ 5 files changed, 54 insertions(+) diff --git a/models/user/email_address.go b/models/user/email_address.go index 50c0cfbfb1..8c6f24e57b 100644 --- a/models/user/email_address.go +++ b/models/user/email_address.go @@ -350,6 +350,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 40807f238a..0c462d03ed 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -3094,6 +3094,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 = Manage organizations orgs.name = Name diff --git a/routers/web/admin/emails.go b/routers/web/admin/emails.go index 2cf4035c6a..f0d8555070 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,32 @@ 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 + } + + 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("") + 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("") +} diff --git a/routers/web/web.go b/routers/web/web.go index 3480a18844..edf0769a4b 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -696,6 +696,7 @@ func registerRoutes(m *web.Route) { 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 388863df9b..b07c6fcc01 100644 --- a/templates/admin/emails/list.tmpl +++ b/templates/admin/emails/list.tmpl @@ -38,6 +38,7 @@