Skip to content

Add post-installation redirect based on admin account status #34493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions models/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,18 @@ 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to make handleUserCreated use the same logic.

And there is no need to add "WHERE ...", just check whether any record exists.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a key logic in handleUserCreated, which is to determine whether the current account is the first: CountUsers(ctx, nil) == 1; I am not quite sure if it can be replaced with the newly added method.

sess := db.GetEngine(ctx)
exists, err := sess.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)
Expand Down
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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. 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 <b>%s</b>. 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.
Expand Down
2 changes: 2 additions & 0 deletions routers/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,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)
}
4 changes: 4 additions & 0 deletions routers/web/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,10 @@ func SignUp(ctx *context.Context) {

ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/sign_up"

hasUsers, _ := user_model.HasUsers(ctx)

ctx.Data["IsFirstTimeRegistration"] = !hasUsers

oauth2Providers, err := oauth2.GetOAuth2Providers(ctx, optional.Some(true))
if err != nil {
ctx.ServerError("UserSignUp", err)
Expand Down
2 changes: 1 addition & 1 deletion templates/post-install.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<!-- the "cup" has a handler, so move it a little leftward to make it visually in the center -->
<div class="tw-ml-[-30px]"><img width="160" src="{{AssetUrlPrefix}}/img/loading.png" alt aria-hidden="true"></div>
<div class="tw-my-[2em] tw-text-[18px]">
<a id="goto-user-login" href="{{AppSubUrl}}/user/login">{{ctx.Locale.Tr "install.installing_desc"}}</a>
<a id="goto-signup-or-signin" href="{{AppSubUrl}}{{if .IsAccountCreated}}/user/login{{else}}/user/sign_up{{end}}">{{ctx.Locale.Tr "install.installing_desc"}}</a>
</div>
</div>
</div>
Expand Down
3 changes: 3 additions & 0 deletions templates/user/auth/signup_inner.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
{{end}}
</h4>
<div class="ui attached segment">
{{if .IsFirstTimeRegistration}}
<p>{{ctx.Locale.Tr "auth.sign_up_tip"}}</p>
{{end}}
<form class="ui form" action="{{.SignUpLink}}" method="post">
{{.CsrfTokenHtml}}
{{if or (not .LinkAccountMode) (and .LinkAccountMode .LinkAccountModeRegister)}}
Expand Down
2 changes: 1 addition & 1 deletion web_src/js/features/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down