aboutsummaryrefslogtreecommitdiffstats
path: root/services/user/user.go
diff options
context:
space:
mode:
authoryp05327 <576951401@qq.com>2024-01-15 15:51:43 +0900
committerGitHub <noreply@github.com>2024-01-15 06:51:43 +0000
commitce0225c1b87d682f53b87496c8dd6ccee0396f0b (patch)
tree2ab05a9d17869c2a16e5d0e21522106043d823d5 /services/user/user.go
parentb820019fec1bd955b6cb9dba1ce2f2571924ae64 (diff)
downloadgitea-ce0225c1b87d682f53b87496c8dd6ccee0396f0b.tar.gz
gitea-ce0225c1b87d682f53b87496c8dd6ccee0396f0b.zip
Forbid removing the last admin user (#28337)
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'services/user/user.go')
-rw-r--r--services/user/user.go7
1 files changed, 6 insertions, 1 deletions
diff --git a/services/user/user.go b/services/user/user.go
index 932e359c9f..8bf083192f 100644
--- a/services/user/user.go
+++ b/services/user/user.go
@@ -129,6 +129,10 @@ func DeleteUser(ctx context.Context, u *user_model.User, purge bool) error {
return fmt.Errorf("%s is an organization not a user", u.Name)
}
+ if user_model.IsLastAdminUser(ctx, u) {
+ return models.ErrDeleteLastAdminUser{UID: u.ID}
+ }
+
if purge {
// Disable the user first
// NOTE: This is deliberately not within a transaction as it must disable the user immediately to prevent any further action by the user to be purged.
@@ -295,7 +299,8 @@ func DeleteInactiveUsers(ctx context.Context, olderThan time.Duration) error {
}
if err := DeleteUser(ctx, u, false); err != nil {
// Ignore users that were set inactive by admin.
- if models.IsErrUserOwnRepos(err) || models.IsErrUserHasOrgs(err) || models.IsErrUserOwnPackages(err) {
+ if models.IsErrUserOwnRepos(err) || models.IsErrUserHasOrgs(err) ||
+ models.IsErrUserOwnPackages(err) || models.IsErrDeleteLastAdminUser(err) {
continue
}
return err