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

Admin should not delete himself #19423

Merged
merged 3 commits into from May 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Expand Up @@ -2529,6 +2529,7 @@ users.allow_import_local = May Import Local Repositories
users.allow_create_organization = May Create Organizations
users.update_profile = Update User Account
users.delete_account = Delete User Account
users.cannot_delete_self = "You cannot delete yourself"
users.still_own_repo = This user still owns one or more repositories. Delete or transfer these repositories first.
users.still_has_org = This user is a member of an organization. Remove the user from any organizations first.
users.still_own_packages = This user still owns one or more packages. Delete these packages first.
Expand Down
6 changes: 6 additions & 0 deletions routers/api/v1/admin/user.go
Expand Up @@ -310,6 +310,12 @@ func DeleteUser(ctx *context.APIContext) {
return
}

// admin should not delete themself
if ctx.ContextUser.ID == ctx.Doer.ID {
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("you cannot delete yourself"))
return
}

if err := user_service.DeleteUser(ctx.ContextUser); err != nil {
if models.IsErrUserOwnRepos(err) ||
models.IsErrUserHasOrgs(err) ||
Expand Down
9 changes: 9 additions & 0 deletions routers/web/admin/users.go
Expand Up @@ -416,6 +416,15 @@ func DeleteUser(ctx *context.Context) {
return
}

// admin should not delete themself
if u.ID == ctx.Doer.ID {
ctx.Flash.Error(ctx.Tr("admin.users.cannot_delete_self"))
ctx.JSON(http.StatusOK, map[string]interface{}{
lunny marked this conversation as resolved.
Show resolved Hide resolved
"redirect": setting.AppSubURL + "/admin/users/" + url.PathEscape(ctx.Params(":userid")),
})
return
}

if err = user_service.DeleteUser(u); err != nil {
switch {
case models.IsErrUserOwnRepos(err):
Expand Down