summaryrefslogtreecommitdiffstats
path: root/routers/api/v1/admin
diff options
context:
space:
mode:
authoryp05327 <576951401@qq.com>2024-01-15 07:51:43 +0100
committerGitHub <noreply@github.com>2024-01-15 07:51:43 +0100
commitce0225c1b87d682f53b87496c8dd6ccee0396f0b (patch)
tree2ab05a9d17869c2a16e5d0e21522106043d823d5 /routers/api/v1/admin
parentFix when private user following user, private user will not be counted in his... (diff)
downloadforgejo-ce0225c1b87d682f53b87496c8dd6ccee0396f0b.tar.xz
forgejo-ce0225c1b87d682f53b87496c8dd6ccee0396f0b.zip
Forbid removing the last admin user (#28337)
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'routers/api/v1/admin')
-rw-r--r--routers/api/v1/admin/user.go9
1 files changed, 8 insertions, 1 deletions
diff --git a/routers/api/v1/admin/user.go b/routers/api/v1/admin/user.go
index 91b5f3a1b0..b4cc42ea5d 100644
--- a/routers/api/v1/admin/user.go
+++ b/routers/api/v1/admin/user.go
@@ -183,6 +183,8 @@ func EditUser(ctx *context.APIContext) {
// responses:
// "200":
// "$ref": "#/responses/User"
+ // "400":
+ // "$ref": "#/responses/error"
// "403":
// "$ref": "#/responses/forbidden"
// "422":
@@ -264,6 +266,10 @@ func EditUser(ctx *context.APIContext) {
ctx.ContextUser.Visibility = api.VisibilityModes[form.Visibility]
}
if form.Admin != nil {
+ if !*form.Admin && user_model.IsLastAdminUser(ctx, ctx.ContextUser) {
+ ctx.Error(http.StatusBadRequest, "LastAdmin", ctx.Tr("auth.last_admin"))
+ return
+ }
ctx.ContextUser.IsAdmin = *form.Admin
}
if form.AllowGitHook != nil {
@@ -341,7 +347,8 @@ func DeleteUser(ctx *context.APIContext) {
if err := user_service.DeleteUser(ctx, ctx.ContextUser, ctx.FormBool("purge")); err != nil {
if models.IsErrUserOwnRepos(err) ||
models.IsErrUserHasOrgs(err) ||
- models.IsErrUserOwnPackages(err) {
+ models.IsErrUserOwnPackages(err) ||
+ models.IsErrDeleteLastAdminUser(err) {
ctx.Error(http.StatusUnprocessableEntity, "", err)
} else {
ctx.Error(http.StatusInternalServerError, "DeleteUser", err)