mirror of
https://github.com/go-gitea/gitea.git
synced 2025-06-23 08:44:38 +02:00
Add post-installation redirect based on admin account status (#34493)
This PR adds a feature to direct users to appropriate pages after system installation: - If no admin credentials were provided during installation, redirect to the registration page with a prominent notice about creating the first administrative account - If admin credentials were already set, redirect directly to the login page  --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
parent
7de114a332
commit
0548c10293
@ -831,6 +831,20 @@ type CountUserFilter struct {
|
|||||||
IsActive optional.Option[bool]
|
IsActive optional.Option[bool]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 ret, fmt.Errorf("error checking user existence: %w", err)
|
||||||
|
}
|
||||||
|
ret.HasAnyUser = len(res) != 0
|
||||||
|
ret.HasOnlyOneUser = len(res) == 1
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
// CountUsers returns number of users.
|
// CountUsers returns number of users.
|
||||||
func CountUsers(ctx context.Context, opts *CountUserFilter) int64 {
|
func CountUsers(ctx context.Context, opts *CountUserFilter) int64 {
|
||||||
return countUsers(ctx, opts)
|
return countUsers(ctx, opts)
|
||||||
|
@ -421,6 +421,7 @@ remember_me.compromised = The login token is not valid anymore which may indicat
|
|||||||
forgot_password_title= Forgot Password
|
forgot_password_title= Forgot Password
|
||||||
forgot_password = Forgot password?
|
forgot_password = Forgot password?
|
||||||
need_account = Need an account?
|
need_account = Need an account?
|
||||||
|
sign_up_tip = You are registering the first account in the system, which has administrator privileges. Please carefully remember your username and password. If you forget the username or password, please refer to the Gitea documentation to recover the account.
|
||||||
sign_up_now = Register now.
|
sign_up_now = Register now.
|
||||||
sign_up_successful = Account was successfully created. Welcome!
|
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.
|
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.
|
||||||
|
@ -601,5 +601,7 @@ func SubmitInstall(ctx *context.Context) {
|
|||||||
// InstallDone shows the "post-install" page, makes it easier to develop the page.
|
// 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"
|
// The name is not called as "PostInstall" to avoid misinterpretation as a handler for "POST /install"
|
||||||
func InstallDone(ctx *context.Context) { //nolint
|
func InstallDone(ctx *context.Context) { //nolint
|
||||||
|
hasUsers, _ := user_model.HasUsers(ctx)
|
||||||
|
ctx.Data["IsAccountCreated"] = hasUsers.HasAnyUser
|
||||||
ctx.HTML(http.StatusOK, tplPostInstall)
|
ctx.HTML(http.StatusOK, tplPostInstall)
|
||||||
}
|
}
|
||||||
|
@ -421,9 +421,11 @@ func SignOut(ctx *context.Context) {
|
|||||||
// SignUp render the register page
|
// SignUp render the register page
|
||||||
func SignUp(ctx *context.Context) {
|
func SignUp(ctx *context.Context) {
|
||||||
ctx.Data["Title"] = ctx.Tr("sign_up")
|
ctx.Data["Title"] = ctx.Tr("sign_up")
|
||||||
|
|
||||||
ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/sign_up"
|
ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/sign_up"
|
||||||
|
|
||||||
|
hasUsers, _ := user_model.HasUsers(ctx)
|
||||||
|
ctx.Data["IsFirstTimeRegistration"] = !hasUsers.HasAnyUser
|
||||||
|
|
||||||
oauth2Providers, err := oauth2.GetOAuth2Providers(ctx, optional.Some(true))
|
oauth2Providers, err := oauth2.GetOAuth2Providers(ctx, optional.Some(true))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.ServerError("UserSignUp", err)
|
ctx.ServerError("UserSignUp", err)
|
||||||
@ -610,7 +612,13 @@ func createUserInContext(ctx *context.Context, tpl templates.TplName, form any,
|
|||||||
// sends a confirmation email if required.
|
// sends a confirmation email if required.
|
||||||
func handleUserCreated(ctx *context.Context, u *user_model.User, gothUser *goth.User) (ok bool) {
|
func handleUserCreated(ctx *context.Context, u *user_model.User, gothUser *goth.User) (ok bool) {
|
||||||
// Auto-set admin for the only user.
|
// 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{
|
opts := &user_service.UpdateOptions{
|
||||||
IsActive: optional.Some(true),
|
IsActive: optional.Some(true),
|
||||||
IsAdmin: user_service.UpdateOptionFieldFromValue(true),
|
IsAdmin: user_service.UpdateOptionFieldFromValue(true),
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
<!-- the "cup" has a handler, so move it a little leftward to make it visually in the center -->
|
<!-- 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-ml-[-30px]"><img width="160" src="{{AssetUrlPrefix}}/img/loading.png" alt aria-hidden="true"></div>
|
||||||
<div class="tw-my-[2em] tw-text-[18px]">
|
<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-after-install" href="{{AppSubUrl}}{{Iif .IsAccountCreated "/user/login" "/user/sign_up"}}">{{ctx.Locale.Tr "install.installing_desc"}}</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -7,6 +7,9 @@
|
|||||||
{{end}}
|
{{end}}
|
||||||
</h4>
|
</h4>
|
||||||
<div class="ui attached segment">
|
<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">
|
<form class="ui form" action="{{.SignUpLink}}" method="post">
|
||||||
{{.CsrfTokenHtml}}
|
{{.CsrfTokenHtml}}
|
||||||
{{if or (not .LinkAccountMode) (and .LinkAccountMode .LinkAccountModeRegister)}}
|
{{if or (not .LinkAccountMode) (and .LinkAccountMode .LinkAccountModeRegister)}}
|
||||||
|
@ -104,7 +104,7 @@ function initPreInstall() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function initPostInstall() {
|
function initPostInstall() {
|
||||||
const el = document.querySelector('#goto-user-login');
|
const el = document.querySelector('#goto-after-install');
|
||||||
if (!el) return;
|
if (!el) return;
|
||||||
|
|
||||||
const targetUrl = el.getAttribute('href');
|
const targetUrl = el.getAttribute('href');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user