Skip to content

Commit 9efa471

Browse files
authored
Admin should not delete himself (#19423)
Admin should not be able to delete themselves. Also partially fix #15449
1 parent 290cc88 commit 9efa471

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,6 +2529,7 @@ users.allow_import_local = May Import Local Repositories
25292529
users.allow_create_organization = May Create Organizations
25302530
users.update_profile = Update User Account
25312531
users.delete_account = Delete User Account
2532+
users.cannot_delete_self = "You cannot delete yourself"
25322533
users.still_own_repo = This user still owns one or more repositories. Delete or transfer these repositories first.
25332534
users.still_has_org = This user is a member of an organization. Remove the user from any organizations first.
25342535
users.still_own_packages = This user still owns one or more packages. Delete these packages first.

routers/api/v1/admin/user.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,12 @@ func DeleteUser(ctx *context.APIContext) {
310310
return
311311
}
312312

313+
// admin should not delete themself
314+
if ctx.ContextUser.ID == ctx.Doer.ID {
315+
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("you cannot delete yourself"))
316+
return
317+
}
318+
313319
if err := user_service.DeleteUser(ctx.ContextUser); err != nil {
314320
if models.IsErrUserOwnRepos(err) ||
315321
models.IsErrUserHasOrgs(err) ||

routers/web/admin/users.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,15 @@ func DeleteUser(ctx *context.Context) {
416416
return
417417
}
418418

419+
// admin should not delete themself
420+
if u.ID == ctx.Doer.ID {
421+
ctx.Flash.Error(ctx.Tr("admin.users.cannot_delete_self"))
422+
ctx.JSON(http.StatusOK, map[string]interface{}{
423+
"redirect": setting.AppSubURL + "/admin/users/" + url.PathEscape(ctx.Params(":userid")),
424+
})
425+
return
426+
}
427+
419428
if err = user_service.DeleteUser(u); err != nil {
420429
switch {
421430
case models.IsErrUserOwnRepos(err):

0 commit comments

Comments
 (0)