Skip to content

Commit cc04481

Browse files
yp05327wolfogre
andauthored
1 parent d8f82cb commit cc04481

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed

models/user/email_address.go

+1
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ type SearchEmailOptions struct {
395395

396396
// SearchEmailResult is an e-mail address found in the user or email_address table
397397
type SearchEmailResult struct {
398+
ID int64
398399
UID int64
399400
Email string
400401
IsActivated bool

options/locale/locale_en-US.ini

+4
Original file line numberDiff line numberDiff line change
@@ -2982,6 +2982,10 @@ emails.not_updated = Failed to update the requested email address: %v
29822982
emails.duplicate_active = This email address is already active for a different user.
29832983
emails.change_email_header = Update Email Properties
29842984
emails.change_email_text = Are you sure you want to update this email address?
2985+
emails.delete = Delete Email
2986+
emails.delete_desc = Are you sure you want to delete this email address?
2987+
emails.deletion_success = The email address has been deleted.
2988+
emails.delete_primary_email_error = You can not delete the primary email.
29852989

29862990
orgs.org_manage_panel = Organization Management
29872991
orgs.name = Name

routers/web/admin/emails.go

+30
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"code.gitea.io/gitea/modules/optional"
1616
"code.gitea.io/gitea/modules/setting"
1717
"code.gitea.io/gitea/services/context"
18+
"code.gitea.io/gitea/services/user"
1819
)
1920

2021
const (
@@ -150,3 +151,32 @@ func ActivateEmail(ctx *context.Context) {
150151
redirect.RawQuery = q.Encode()
151152
ctx.Redirect(redirect.String())
152153
}
154+
155+
// DeleteEmail serves a POST request for delete a user's email
156+
func DeleteEmail(ctx *context.Context) {
157+
u, err := user_model.GetUserByID(ctx, ctx.FormInt64("Uid"))
158+
if err != nil || u == nil {
159+
ctx.ServerError("GetUserByID", err)
160+
return
161+
}
162+
163+
email, err := user_model.GetEmailAddressByID(ctx, u.ID, ctx.FormInt64("id"))
164+
if err != nil || email == nil {
165+
ctx.ServerError("GetEmailAddressByID", err)
166+
return
167+
}
168+
169+
if err := user.DeleteEmailAddresses(ctx, u, []string{email.Email}); err != nil {
170+
if user_model.IsErrPrimaryEmailCannotDelete(err) {
171+
ctx.Flash.Error(ctx.Tr("admin.emails.delete_primary_email_error"))
172+
ctx.JSONRedirect("")
173+
return
174+
}
175+
ctx.ServerError("DeleteEmailAddresses", err)
176+
return
177+
}
178+
log.Trace("Email address deleted: %s %s", u.Name, email.Email)
179+
180+
ctx.Flash.Success(ctx.Tr("admin.emails.deletion_success"))
181+
ctx.JSONRedirect("")
182+
}

routers/web/web.go

+1
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,7 @@ func registerRoutes(m *web.Router) {
725725
m.Group("/emails", func() {
726726
m.Get("", admin.Emails)
727727
m.Post("/activate", admin.ActivateEmail)
728+
m.Post("/delete", admin.DeleteEmail)
728729
})
729730

730731
m.Group("/orgs", func() {

templates/admin/emails/list.tmpl

+18
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
</th>
3939
<th>{{ctx.Locale.Tr "admin.emails.primary"}}</th>
4040
<th>{{ctx.Locale.Tr "admin.emails.activated"}}</th>
41+
<th></th>
4142
</tr>
4243
</thead>
4344
<tbody>
@@ -59,6 +60,11 @@
5960
{{svg (Iif .IsActivated "octicon-check" "octicon-x")}}
6061
{{end}}
6162
</td>
63+
<td>
64+
<div class="tw-flex tw-gap-2">
65+
<a class="delete-button" href="" data-url="{{$.Link}}/delete" data-id="{{.ID}}" data-data-uid="{{.UID}}">{{svg "octicon-trash"}}</a>
66+
</div>
67+
</td>
6268
</tr>
6369
{{end}}
6470
</tbody>
@@ -95,4 +101,16 @@
95101
</div>
96102

97103
</div>
104+
105+
<div class="ui g-modal-confirm delete modal">
106+
<div class="header">
107+
{{svg "octicon-trash"}}
108+
{{ctx.Locale.Tr "admin.emails.delete"}}
109+
</div>
110+
<div class="content">
111+
{{ctx.Locale.Tr "admin.emails.delete_desc"}}
112+
</div>
113+
{{template "base/modal_actions_confirm" .}}
114+
</div>
115+
98116
{{template "admin/layout_footer" .}}

0 commit comments

Comments
 (0)