1
1
mirror of https://github.com/go-gitea/gitea synced 2024-12-23 00:54:28 +00:00

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 <i@wolfogre.com>
This commit is contained in:
yp05327 2024-07-25 19:11:04 +09:00 committed by GitHub
parent d8f82cbc78
commit cc044818c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 54 additions and 0 deletions

View File

@ -395,6 +395,7 @@ type SearchEmailOptions struct {
// SearchEmailResult is an e-mail address found in the user or email_address table // SearchEmailResult is an e-mail address found in the user or email_address table
type SearchEmailResult struct { type SearchEmailResult struct {
ID int64
UID int64 UID int64
Email string Email string
IsActivated bool IsActivated bool

View File

@ -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.duplicate_active = This email address is already active for a different user.
emails.change_email_header = Update Email Properties emails.change_email_header = Update Email Properties
emails.change_email_text = Are you sure you want to update this email address? 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.org_manage_panel = Organization Management
orgs.name = Name orgs.name = Name

View File

@ -15,6 +15,7 @@ import (
"code.gitea.io/gitea/modules/optional" "code.gitea.io/gitea/modules/optional"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/services/context" "code.gitea.io/gitea/services/context"
"code.gitea.io/gitea/services/user"
) )
const ( const (
@ -150,3 +151,32 @@ func ActivateEmail(ctx *context.Context) {
redirect.RawQuery = q.Encode() redirect.RawQuery = q.Encode()
ctx.Redirect(redirect.String()) 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("")
}

View File

@ -725,6 +725,7 @@ func registerRoutes(m *web.Router) {
m.Group("/emails", func() { m.Group("/emails", func() {
m.Get("", admin.Emails) m.Get("", admin.Emails)
m.Post("/activate", admin.ActivateEmail) m.Post("/activate", admin.ActivateEmail)
m.Post("/delete", admin.DeleteEmail)
}) })
m.Group("/orgs", func() { m.Group("/orgs", func() {

View File

@ -38,6 +38,7 @@
</th> </th>
<th>{{ctx.Locale.Tr "admin.emails.primary"}}</th> <th>{{ctx.Locale.Tr "admin.emails.primary"}}</th>
<th>{{ctx.Locale.Tr "admin.emails.activated"}}</th> <th>{{ctx.Locale.Tr "admin.emails.activated"}}</th>
<th></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -59,6 +60,11 @@
{{svg (Iif .IsActivated "octicon-check" "octicon-x")}} {{svg (Iif .IsActivated "octicon-check" "octicon-x")}}
{{end}} {{end}}
</td> </td>
<td>
<div class="tw-flex tw-gap-2">
<a class="delete-button" href="" data-url="{{$.Link}}/delete" data-id="{{.ID}}" data-data-uid="{{.UID}}">{{svg "octicon-trash"}}</a>
</div>
</td>
</tr> </tr>
{{end}} {{end}}
</tbody> </tbody>
@ -95,4 +101,16 @@
</div> </div>
</div> </div>
<div class="ui g-modal-confirm delete modal">
<div class="header">
{{svg "octicon-trash"}}
{{ctx.Locale.Tr "admin.emails.delete"}}
</div>
<div class="content">
{{ctx.Locale.Tr "admin.emails.delete_desc"}}
</div>
{{template "base/modal_actions_confirm" .}}
</div>
{{template "admin/layout_footer" .}} {{template "admin/layout_footer" .}}