+ {{ctx.Locale.Tr "auth.forgot_password_title"}} +
+{{.ForgotPasswordMessage}}
+diff --git a/models/user/user.go b/models/user/user.go index 7eba2a359d8..25edbb37087 100644 --- a/models/user/user.go +++ b/models/user/user.go @@ -1052,8 +1052,16 @@ func GetInactiveUsers(ctx context.Context, olderThan time.Duration) ([]*User, er // GetUserByID returns the user object by given ID if exists. TODO: for most cases, this function should be avoided. // Use correct GetXxxUser functions instead to handle user types & states correctly. func GetUserByID(ctx context.Context, id int64) (*User, error) { - u := new(User) - has, err := db.GetEngine(ctx).ID(id).Get(u) + return getUserByIDAndTypes(ctx, id) +} + +func getUserByIDAndTypes(ctx context.Context, id int64, types ...UserType) (*User, error) { + u := &User{} + sess := db.GetEngine(ctx).ID(id) + if len(types) > 0 { + sess = sess.In("`type`", types) + } + has, err := sess.Get(u) if err != nil { return nil, err } else if !has { @@ -1392,34 +1400,41 @@ func parseLocalPartToNameID(localPart string) (string, int64) { return name, id } -// GetUserByEmail returns the user object by given e-mail if exists. func GetUserByEmail(ctx context.Context, email string) (*User, error) { - if len(email) == 0 { - return nil, ErrUserNotExist{Name: email} - } + return getUserByEmailAndTypes(ctx, email, UserTypeIndividual, UserTypeBot) +} +func getUserByEmailAndTypes(ctx context.Context, email string, types ...UserType) (*User, error) { + if email == "" { + return nil, ErrUserNotExist{} + } email = strings.ToLower(email) - // Otherwise, check in alternative list for activated email addresses + + // check in activated email addresses emailAddress, has, err := db.Get[EmailAddress](ctx, builder.Eq{"lower_email": email, "is_activated": true}) if err != nil { return nil, err } if has { - return GetUserByID(ctx, emailAddress.UID) + return getUserByIDAndTypes(ctx, emailAddress.UID, types...) } - // Finally, if email address is the protected email address: + // if email address is the protected email address: if localPart, ok := strings.CutSuffix(email, strings.ToLower("@"+setting.Service.NoReplyAddress)); ok { name, id := parseLocalPartToNameID(localPart) if id != 0 { - return GetUserByID(ctx, id) + return getUserByIDAndTypes(ctx, id, types...) } - return GetIndividualUserByName(ctx, name) + return getUserByNameWithTypes(ctx, name, types...) } return nil, ErrUserNotExist{Name: email} } +func GetIndividualUserByEmail(ctx context.Context, email string) (*User, error) { + return getUserByEmailAndTypes(ctx, email, UserTypeIndividual) +} + func GetIndividualUserByPrimaryEmail(ctx context.Context, email string) (*User, error) { email = strings.ToLower(strings.TrimSpace(email)) if len(email) == 0 { diff --git a/options/locale/locale_en-US.json b/options/locale/locale_en-US.json index 9dafaad4f88..d30f441d40a 100644 --- a/options/locale/locale_en-US.json +++ b/options/locale/locale_en-US.json @@ -421,8 +421,7 @@ "auth.oauth.signin.error.access_denied": "The authorization request was denied.", "auth.oauth.signin.error.temporarily_unavailable": "Authorization failed because the authentication server is temporarily unavailable. Please try again later.", "auth.oauth_callback_unable_auto_reg": "Auto Registration is enabled, but OAuth2 Provider %[1]s returned missing fields: %[2]s, unable to create an account automatically. Please create or link to an account, or contact the site administrator.", - "auth.disable_forgot_password_mail": "Account recovery is disabled because no email address is set up. Please contact your site administrator.", - "auth.disable_forgot_password_mail_admin": "Account recovery is only available when an email address is set up.", + "auth.disable_forgot_password_mail": "Account recovery is disabled because no mailer service is configured on this server. Please contact your site administrator.", "auth.email_domain_blacklisted": "You cannot register with your email address.", "auth.authorize_application": "Authorize Application", "auth.authorize_redirect_notice": "You will be redirected to %s if you authorize this application.", diff --git a/routers/web/auth/password.go b/routers/web/auth/password.go index 93025356cc8..ffa5ec52e3a 100644 --- a/routers/web/auth/password.go +++ b/routers/web/auth/password.go @@ -5,6 +5,7 @@ package auth import ( "errors" + "html/template" "net/http" audit_model "gitea.dev/models/audit" @@ -29,65 +30,57 @@ var ( // tplMustChangePassword template for updating a user's password tplMustChangePassword templates.TplName = "user/auth/change_passwd" tplForgotPassword templates.TplName = "user/auth/forgot_passwd" + tplForgotPasswordMsg templates.TplName = "user/auth/forgot_passwd_msg" tplResetPassword templates.TplName = "user/auth/reset_passwd" ) -// ForgotPasswd render the forget password page +func renderForgotPasswordMessage(ctx *context.Context, msg template.HTML) { + ctx.Data["ForgotPasswordMessage"] = msg + ctx.HTML(http.StatusOK, tplForgotPasswordMsg) +} + func ForgotPasswd(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("auth.forgot_password_title") if setting.MailService == nil { - log.Warn("no mail service configured") - ctx.Data["IsResetDisable"] = true - ctx.HTML(http.StatusOK, tplForgotPassword) + msg := ctx.Tr("auth.disable_forgot_password_mail") + renderForgotPasswordMessage(ctx, msg) return } ctx.Data["Email"] = ctx.FormString("email") - - ctx.Data["IsResetRequest"] = true ctx.HTML(http.StatusOK, tplForgotPassword) } -// ForgotPasswdPost response for forget password request func ForgotPasswdPost(ctx *context.Context) { - // TODO: REFACTOR-RENDER-WITH-ERR: form-fetch-action needs navigation for the sent-email and resend-limit views currently rendered by this POST. - ctx.Data["Title"] = ctx.Tr("auth.forgot_password_title") - if setting.MailService == nil { ctx.NotFound() return } - ctx.Data["IsResetRequest"] = true email := ctx.FormString("email") + + ctx.Data["Title"] = ctx.Tr("auth.forgot_password_title") ctx.Data["Email"] = email + msg := ctx.Tr("auth.reset_password_mail_sent_prompt", email, timeutil.MinutesToFriendly(setting.Service.ResetPwdCodeLives, ctx.Locale)) - u, err := user_model.GetUserByEmail(ctx, email) - if err == nil && !u.IsIndividual() { - err = user_model.ErrUserNotExist{} - } - if err != nil { - if user_model.IsErrUserNotExist(err) { - ctx.Data["ResetPwdCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ResetPwdCodeLives, ctx.Locale) - ctx.Data["IsResetSent"] = true - ctx.HTML(http.StatusOK, tplForgotPassword) - return - } - + u, err := user_model.GetIndividualUserByEmail(ctx, email) + if errors.Is(err, util.ErrNotExist) { + renderForgotPasswordMessage(ctx, msg) + return + } else if err != nil { ctx.ServerErrorAuto(err) return } if !u.IsLocal() && !u.IsOAuth2() { - ctx.Data["Err_Email"] = true - ctx.RenderWithErrDeprecated(ctx.Tr("auth.non_local_account"), tplForgotPassword, nil) + ctx.Flash.Error(ctx.Tr("auth.non_local_account"), true) + ctx.HTML(http.StatusOK, tplForgotPassword) return } if ctx.Cache.IsExist("MailResendLimit_" + u.LowerName) { - ctx.Data["ResendLimited"] = true - ctx.HTML(http.StatusOK, tplForgotPassword) + renderForgotPasswordMessage(ctx, msg) return } @@ -101,9 +94,7 @@ func ForgotPasswdPost(ctx *context.Context) { log.Error("Set cache(MailResendLimit) fail: %v", err) } - ctx.Data["ResetPwdCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ResetPwdCodeLives, ctx.Locale) - ctx.Data["IsResetSent"] = true - ctx.HTML(http.StatusOK, tplForgotPassword) + renderForgotPasswordMessage(ctx, msg) } func commonResetPassword(ctx *context.Context) (*user_model.User, *auth.TwoFactor) { diff --git a/templates/user/auth/forgot_passwd.tmpl b/templates/user/auth/forgot_passwd.tmpl index 7a710cd4262..f65fa941ba3 100644 --- a/templates/user/auth/forgot_passwd.tmpl +++ b/templates/user/auth/forgot_passwd.tmpl @@ -1,35 +1,21 @@ {{template "base/head" .}} -
{{.ForgotPasswordMessage}}
+