mirror of
https://github.com/go-gitea/gitea
synced 2025-07-12 13:37:20 +00:00
Forbid removing the last admin user (#28337)
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
@ -730,9 +730,18 @@ func CreateUser(ctx context.Context, u *User, overwriteDefault ...*CreateUserOve
|
||||
return committer.Commit()
|
||||
}
|
||||
|
||||
// IsLastAdminUser check whether user is the last admin
|
||||
func IsLastAdminUser(ctx context.Context, user *User) bool {
|
||||
if user.IsAdmin && CountUsers(ctx, &CountUserFilter{IsAdmin: util.OptionalBoolTrue}) <= 1 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// CountUserFilter represent optional filters for CountUsers
|
||||
type CountUserFilter struct {
|
||||
LastLoginSince *int64
|
||||
IsAdmin util.OptionalBool
|
||||
}
|
||||
|
||||
// CountUsers returns number of users.
|
||||
@ -741,13 +750,25 @@ func CountUsers(ctx context.Context, opts *CountUserFilter) int64 {
|
||||
}
|
||||
|
||||
func countUsers(ctx context.Context, opts *CountUserFilter) int64 {
|
||||
sess := db.GetEngine(ctx).Where(builder.Eq{"type": "0"})
|
||||
sess := db.GetEngine(ctx)
|
||||
cond := builder.NewCond()
|
||||
cond = cond.And(builder.Eq{"type": UserTypeIndividual})
|
||||
|
||||
if opts != nil && opts.LastLoginSince != nil {
|
||||
sess = sess.Where(builder.Gte{"last_login_unix": *opts.LastLoginSince})
|
||||
if opts != nil {
|
||||
if opts.LastLoginSince != nil {
|
||||
cond = cond.And(builder.Gte{"last_login_unix": *opts.LastLoginSince})
|
||||
}
|
||||
|
||||
if !opts.IsAdmin.IsNone() {
|
||||
cond = cond.And(builder.Eq{"is_admin": opts.IsAdmin.IsTrue()})
|
||||
}
|
||||
}
|
||||
|
||||
count, err := sess.Where(cond).Count(new(User))
|
||||
if err != nil {
|
||||
log.Error("user.countUsers: %v", err)
|
||||
}
|
||||
|
||||
count, _ := sess.Count(new(User))
|
||||
return count
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user