Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support delete user email in admin panel #31690

Merged
merged 4 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions models/user/email_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions routers/web/admin/emails.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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("")
}
1 change: 1 addition & 0 deletions routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
18 changes: 18 additions & 0 deletions templates/admin/emails/list.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
</th>
<th>{{ctx.Locale.Tr "admin.emails.primary"}}</th>
<th>{{ctx.Locale.Tr "admin.emails.activated"}}</th>
<th></th>
</tr>
</thead>
<tbody>
Expand All @@ -59,6 +60,11 @@
{{svg (Iif .IsActivated "octicon-check" "octicon-x")}}
{{end}}
</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>
{{end}}
</tbody>
Expand Down Expand Up @@ -95,4 +101,16 @@
</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" .}}