From 495cae8c1a2657e79f3d252eb399659487039198 Mon Sep 17 00:00:00 2001 From: Kerwin Bryant Date: Sat, 17 May 2025 03:43:03 +0000 Subject: [PATCH 1/7] feat: redirect to registration page for first-time admin setup after installation --- options/locale/locale_en-US.ini | 1 + routers/install/install.go | 2 ++ routers/web/auth/auth.go | 2 ++ templates/post-install.tmpl | 2 +- templates/user/auth/signup_inner.tmpl | 3 +++ web_src/js/features/install.ts | 2 +- 6 files changed, 10 insertions(+), 2 deletions(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index b6411f77777a3..69aad0c5f38d0 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -421,6 +421,7 @@ remember_me.compromised = The login token is not valid anymore which may indicat forgot_password_title= Forgot Password forgot_password = Forgot password? need_account = Need an account? +sign_up_tip = You are registering the first account in the system. Upon successful registration, you will become the Super Administrator. Please carefully remember your username and password, as losing this information could cause significant inconvenience later. sign_up_now = Register now. sign_up_successful = Account was successfully created. Welcome! confirmation_mail_sent_prompt_ex = A new confirmation email has been sent to %s. Please check your inbox within the next %s to complete the registration process. If your registration email address is incorrect, you can sign in again and change it. diff --git a/routers/install/install.go b/routers/install/install.go index 2962f3948fea1..bd7810e809dfc 100644 --- a/routers/install/install.go +++ b/routers/install/install.go @@ -558,6 +558,8 @@ func SubmitInstall(ctx *context.Context) { } log.Info("Admin account already exist") u, _ = user_model.GetUserByName(ctx, u.Name) + } else { + ctx.Data["IsAccountCreated"] = true } nt, token, err := auth_service.CreateAuthTokenForUserID(ctx, u.ID) diff --git a/routers/web/auth/auth.go b/routers/web/auth/auth.go index 69b9d285b78cc..f07e821e019c1 100644 --- a/routers/web/auth/auth.go +++ b/routers/web/auth/auth.go @@ -424,6 +424,8 @@ func SignUp(ctx *context.Context) { ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/sign_up" + ctx.Data["IsFirstTimeRegistration"] = user_model.CountUsers(ctx, nil) == 0 + oauth2Providers, err := oauth2.GetOAuth2Providers(ctx, optional.Some(true)) if err != nil { ctx.ServerError("UserSignUp", err) diff --git a/templates/post-install.tmpl b/templates/post-install.tmpl index 0c9aa35c9093a..44099ce29d5d0 100644 --- a/templates/post-install.tmpl +++ b/templates/post-install.tmpl @@ -4,7 +4,7 @@
- {{ctx.Locale.Tr "install.installing_desc"}} + {{ctx.Locale.Tr "install.installing_desc"}}
diff --git a/templates/user/auth/signup_inner.tmpl b/templates/user/auth/signup_inner.tmpl index d66568199dd8d..a3f6e1471f8d8 100644 --- a/templates/user/auth/signup_inner.tmpl +++ b/templates/user/auth/signup_inner.tmpl @@ -7,6 +7,9 @@ {{end}}
+ {{if .IsFirstTimeRegistration}} +

{{ctx.Locale.Tr "auth.sign_up_tip"}}

+ {{end}}
{{.CsrfTokenHtml}} {{if or (not .LinkAccountMode) (and .LinkAccountMode .LinkAccountModeRegister)}} diff --git a/web_src/js/features/install.ts b/web_src/js/features/install.ts index 34df4757f9f80..bd8e57265c846 100644 --- a/web_src/js/features/install.ts +++ b/web_src/js/features/install.ts @@ -104,7 +104,7 @@ function initPreInstall() { } function initPostInstall() { - const el = document.querySelector('#goto-user-login'); + const el = document.querySelector('#goto-signup-or-signin'); if (!el) return; const targetUrl = el.getAttribute('href'); From 3e3316f8254796f2532aaca445f9eb798d561740 Mon Sep 17 00:00:00 2001 From: Kerwin Bryant Date: Sat, 17 May 2025 14:06:26 +0000 Subject: [PATCH 2/7] fix --- models/user/user.go | 15 +++++++++++++++ options/locale/locale_en-US.ini | 2 +- routers/install/install.go | 4 ++-- routers/web/auth/auth.go | 4 +++- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/models/user/user.go b/models/user/user.go index d7331d79f0b09..c3f082be05707 100644 --- a/models/user/user.go +++ b/models/user/user.go @@ -831,6 +831,21 @@ type CountUserFilter struct { IsActive optional.Option[bool] } +// HasUsers returns true if any user exists in the database. +// It performs a much more efficient check than counting all users. +func HasUsers(ctx context.Context) (bool, error) { + sess := db.GetEngine(ctx) + cond := builder.NewCond() + cond = cond.And(builder.Eq{"type": UserTypeIndividual}) + + exists, err := sess.Where(cond).Limit(1).Exist(new(User)) + if err != nil { + return false, fmt.Errorf("error checking user existence: %w", err) + } + + return exists, nil +} + // CountUsers returns number of users. func CountUsers(ctx context.Context, opts *CountUserFilter) int64 { return countUsers(ctx, opts) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 69aad0c5f38d0..cf3d574667aaa 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -421,7 +421,7 @@ remember_me.compromised = The login token is not valid anymore which may indicat forgot_password_title= Forgot Password forgot_password = Forgot password? need_account = Need an account? -sign_up_tip = You are registering the first account in the system. Upon successful registration, you will become the Super Administrator. Please carefully remember your username and password, as losing this information could cause significant inconvenience later. +sign_up_tip = You are registering the first account in the system. Please carefully remember your username and password, as losing this information could cause significant inconvenience later. sign_up_now = Register now. sign_up_successful = Account was successfully created. Welcome! confirmation_mail_sent_prompt_ex = A new confirmation email has been sent to %s. Please check your inbox within the next %s to complete the registration process. If your registration email address is incorrect, you can sign in again and change it. diff --git a/routers/install/install.go b/routers/install/install.go index bd7810e809dfc..68f402b210dd7 100644 --- a/routers/install/install.go +++ b/routers/install/install.go @@ -558,8 +558,6 @@ func SubmitInstall(ctx *context.Context) { } log.Info("Admin account already exist") u, _ = user_model.GetUserByName(ctx, u.Name) - } else { - ctx.Data["IsAccountCreated"] = true } nt, token, err := auth_service.CreateAuthTokenForUserID(ctx, u.ID) @@ -609,5 +607,7 @@ func SubmitInstall(ctx *context.Context) { // InstallDone shows the "post-install" page, makes it easier to develop the page. // The name is not called as "PostInstall" to avoid misinterpretation as a handler for "POST /install" func InstallDone(ctx *context.Context) { //nolint + hasUsers, _ := user_model.HasUsers(ctx) + ctx.Data["IsAccountCreated"] = hasUsers ctx.HTML(http.StatusOK, tplPostInstall) } diff --git a/routers/web/auth/auth.go b/routers/web/auth/auth.go index f07e821e019c1..bb9bb66f49ed0 100644 --- a/routers/web/auth/auth.go +++ b/routers/web/auth/auth.go @@ -424,7 +424,9 @@ func SignUp(ctx *context.Context) { ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/sign_up" - ctx.Data["IsFirstTimeRegistration"] = user_model.CountUsers(ctx, nil) == 0 + hasUsers, _ := user_model.HasUsers(ctx) + + ctx.Data["IsFirstTimeRegistration"] = !hasUsers oauth2Providers, err := oauth2.GetOAuth2Providers(ctx, optional.Some(true)) if err != nil { From a60a7227844d7d3aeca20a6d475a1069b0865e00 Mon Sep 17 00:00:00 2001 From: Kerwin Bryant Date: Wed, 21 May 2025 14:03:48 +0000 Subject: [PATCH 3/7] fix --- models/user/user.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/models/user/user.go b/models/user/user.go index c3f082be05707..e805d7485f91f 100644 --- a/models/user/user.go +++ b/models/user/user.go @@ -835,10 +835,7 @@ type CountUserFilter struct { // It performs a much more efficient check than counting all users. func HasUsers(ctx context.Context) (bool, error) { sess := db.GetEngine(ctx) - cond := builder.NewCond() - cond = cond.And(builder.Eq{"type": UserTypeIndividual}) - - exists, err := sess.Where(cond).Limit(1).Exist(new(User)) + exists, err := sess.Exist(new(User)) if err != nil { return false, fmt.Errorf("error checking user existence: %w", err) } From 3a3d9519f96ea14670fcc94164167e0dc209a70f Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sun, 8 Jun 2025 20:36:34 +0800 Subject: [PATCH 4/7] fix --- models/user/user.go | 17 +++++++++-------- routers/install/install.go | 2 +- routers/web/auth/auth.go | 12 ++++++++---- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/models/user/user.go b/models/user/user.go index e805d7485f91f..718f7bd77b995 100644 --- a/models/user/user.go +++ b/models/user/user.go @@ -831,16 +831,17 @@ type CountUserFilter struct { IsActive optional.Option[bool] } -// HasUsers returns true if any user exists in the database. -// It performs a much more efficient check than counting all users. -func HasUsers(ctx context.Context) (bool, error) { - sess := db.GetEngine(ctx) - exists, err := sess.Exist(new(User)) +// HasUsers checks whether there are any users in the database, or only one user exists. +func HasUsers(ctx context.Context) (ret struct { + HasAnyUser, HasOnlyOneUser bool +}, err error) { + res, err := db.GetEngine(ctx).Table(&User{}).Cols("id").Limit(2).Query() if err != nil { - return false, fmt.Errorf("error checking user existence: %w", err) + return ret, fmt.Errorf("error checking user existence: %w", err) } - - return exists, nil + ret.HasAnyUser = len(res) != 0 + ret.HasOnlyOneUser = len(res) == 1 + return ret, nil } // CountUsers returns number of users. diff --git a/routers/install/install.go b/routers/install/install.go index 68f402b210dd7..64beca59a202b 100644 --- a/routers/install/install.go +++ b/routers/install/install.go @@ -608,6 +608,6 @@ func SubmitInstall(ctx *context.Context) { // The name is not called as "PostInstall" to avoid misinterpretation as a handler for "POST /install" func InstallDone(ctx *context.Context) { //nolint hasUsers, _ := user_model.HasUsers(ctx) - ctx.Data["IsAccountCreated"] = hasUsers + ctx.Data["IsAccountCreated"] = hasUsers.HasAnyUser ctx.HTML(http.StatusOK, tplPostInstall) } diff --git a/routers/web/auth/auth.go b/routers/web/auth/auth.go index bb9bb66f49ed0..d80ec7a7fce8b 100644 --- a/routers/web/auth/auth.go +++ b/routers/web/auth/auth.go @@ -421,12 +421,10 @@ func SignOut(ctx *context.Context) { // SignUp render the register page func SignUp(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("sign_up") - ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/sign_up" hasUsers, _ := user_model.HasUsers(ctx) - - ctx.Data["IsFirstTimeRegistration"] = !hasUsers + ctx.Data["IsFirstTimeRegistration"] = !hasUsers.HasAnyUser oauth2Providers, err := oauth2.GetOAuth2Providers(ctx, optional.Some(true)) if err != nil { @@ -614,7 +612,13 @@ func createUserInContext(ctx *context.Context, tpl templates.TplName, form any, // sends a confirmation email if required. func handleUserCreated(ctx *context.Context, u *user_model.User, gothUser *goth.User) (ok bool) { // Auto-set admin for the only user. - if user_model.CountUsers(ctx, nil) == 1 { + hasUsers, err := user_model.HasUsers(ctx) + if err != nil { + ctx.ServerError("HasUsers", err) + return false + } + if hasUsers.HasOnlyOneUser { + // the only user is the one just created, will set it as admin opts := &user_service.UpdateOptions{ IsActive: optional.Some(true), IsAdmin: optional.Some(true), From a49359184270f9d80d5573a398555b001d5495e8 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sun, 8 Jun 2025 20:38:54 +0800 Subject: [PATCH 5/7] better html id --- templates/post-install.tmpl | 2 +- web_src/js/features/install.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/post-install.tmpl b/templates/post-install.tmpl index 44099ce29d5d0..9baac4f84c885 100644 --- a/templates/post-install.tmpl +++ b/templates/post-install.tmpl @@ -4,7 +4,7 @@
diff --git a/web_src/js/features/install.ts b/web_src/js/features/install.ts index bd8e57265c846..ca4bcce8819d0 100644 --- a/web_src/js/features/install.ts +++ b/web_src/js/features/install.ts @@ -104,7 +104,7 @@ function initPreInstall() { } function initPostInstall() { - const el = document.querySelector('#goto-signup-or-signin'); + const el = document.querySelector('#goto-after-install'); if (!el) return; const targetUrl = el.getAttribute('href'); From 2eefd69fa8c8747a30e372192ea5820664610470 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sun, 8 Jun 2025 21:04:48 +0800 Subject: [PATCH 6/7] fix lint --- models/user/user.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/models/user/user.go b/models/user/user.go index 718f7bd77b995..5a7a420ced918 100644 --- a/models/user/user.go +++ b/models/user/user.go @@ -834,7 +834,8 @@ type CountUserFilter struct { // HasUsers checks whether there are any users in the database, or only one user exists. func HasUsers(ctx context.Context) (ret struct { HasAnyUser, HasOnlyOneUser bool -}, err error) { +}, err error, +) { res, err := db.GetEngine(ctx).Table(&User{}).Cols("id").Limit(2).Query() if err != nil { return ret, fmt.Errorf("error checking user existence: %w", err) From f8bf3592bd7c651c16c5f17ed472cbda0ee67ec8 Mon Sep 17 00:00:00 2001 From: kerwin612 Date: Mon, 9 Jun 2025 13:32:04 +0800 Subject: [PATCH 7/7] Optimize the sign_up_tip --- options/locale/locale_en-US.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 702fc70267d7c..5d6a54b3b5e8b 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -421,7 +421,7 @@ remember_me.compromised = The login token is not valid anymore which may indicat forgot_password_title= Forgot Password forgot_password = Forgot password? need_account = Need an account? -sign_up_tip = You are registering the first account in the system. Please carefully remember your username and password, as losing this information could cause significant inconvenience later. +sign_up_tip = You are registering the first account in the system, which has administrator privileges. Please carefully remember your username and password, as forgetting these credentials may require system reset and reinitialization. sign_up_now = Register now. sign_up_successful = Account was successfully created. Welcome! confirmation_mail_sent_prompt_ex = A new confirmation email has been sent to %s. Please check your inbox within the next %s to complete the registration process. If your registration email address is incorrect, you can sign in again and change it.