forked from forgejo/forgejo
Backport #28337 by @yp05327
Co-authored-by: yp05327 <576951401@qq.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
(cherry picked from commit 376fa0d8c4
)
This commit is contained in:
parent
c287b1383f
commit
5124be72aa
8 changed files with 80 additions and 7 deletions
|
@ -717,9 +717,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.
|
||||
|
@ -728,13 +737,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
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue