Skip to content

Commit 3a3d951

Browse files
committed
fix
1 parent bfaa8c5 commit 3a3d951

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

models/user/user.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -831,16 +831,17 @@ type CountUserFilter struct {
831831
IsActive optional.Option[bool]
832832
}
833833

834-
// HasUsers returns true if any user exists in the database.
835-
// It performs a much more efficient check than counting all users.
836-
func HasUsers(ctx context.Context) (bool, error) {
837-
sess := db.GetEngine(ctx)
838-
exists, err := sess.Exist(new(User))
834+
// HasUsers checks whether there are any users in the database, or only one user exists.
835+
func HasUsers(ctx context.Context) (ret struct {
836+
HasAnyUser, HasOnlyOneUser bool
837+
}, err error) {
838+
res, err := db.GetEngine(ctx).Table(&User{}).Cols("id").Limit(2).Query()
839839
if err != nil {
840-
return false, fmt.Errorf("error checking user existence: %w", err)
840+
return ret, fmt.Errorf("error checking user existence: %w", err)
841841
}
842-
843-
return exists, nil
842+
ret.HasAnyUser = len(res) != 0
843+
ret.HasOnlyOneUser = len(res) == 1
844+
return ret, nil
844845
}
845846

846847
// CountUsers returns number of users.

routers/install/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,6 @@ func SubmitInstall(ctx *context.Context) {
608608
// The name is not called as "PostInstall" to avoid misinterpretation as a handler for "POST /install"
609609
func InstallDone(ctx *context.Context) { //nolint
610610
hasUsers, _ := user_model.HasUsers(ctx)
611-
ctx.Data["IsAccountCreated"] = hasUsers
611+
ctx.Data["IsAccountCreated"] = hasUsers.HasAnyUser
612612
ctx.HTML(http.StatusOK, tplPostInstall)
613613
}

routers/web/auth/auth.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,12 +421,10 @@ func SignOut(ctx *context.Context) {
421421
// SignUp render the register page
422422
func SignUp(ctx *context.Context) {
423423
ctx.Data["Title"] = ctx.Tr("sign_up")
424-
425424
ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/sign_up"
426425

427426
hasUsers, _ := user_model.HasUsers(ctx)
428-
429-
ctx.Data["IsFirstTimeRegistration"] = !hasUsers
427+
ctx.Data["IsFirstTimeRegistration"] = !hasUsers.HasAnyUser
430428

431429
oauth2Providers, err := oauth2.GetOAuth2Providers(ctx, optional.Some(true))
432430
if err != nil {
@@ -614,7 +612,13 @@ func createUserInContext(ctx *context.Context, tpl templates.TplName, form any,
614612
// sends a confirmation email if required.
615613
func handleUserCreated(ctx *context.Context, u *user_model.User, gothUser *goth.User) (ok bool) {
616614
// Auto-set admin for the only user.
617-
if user_model.CountUsers(ctx, nil) == 1 {
615+
hasUsers, err := user_model.HasUsers(ctx)
616+
if err != nil {
617+
ctx.ServerError("HasUsers", err)
618+
return false
619+
}
620+
if hasUsers.HasOnlyOneUser {
621+
// the only user is the one just created, will set it as admin
618622
opts := &user_service.UpdateOptions{
619623
IsActive: optional.Some(true),
620624
IsAdmin: optional.Some(true),

0 commit comments

Comments
 (0)