Reset locale on login (#18023) (#18025)

Backport #18023

Although we reset the locale in a number of places there were several ways of logging in that were missing the same code.

Fix #18020

Signed-off-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: Gusted <williamzijl7@hotmail.com>
This commit is contained in:
zeripath 2021-12-19 15:04:31 +00:00 committed by GitHub
parent fe91d9617b
commit c69b3b65f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -98,10 +98,33 @@ func AutoSignIn(ctx *context.Context) (bool, error) {
return false, err return false, err
} }
if err := resetLocale(ctx, u); err != nil {
return false, err
}
middleware.DeleteCSRFCookie(ctx.Resp) middleware.DeleteCSRFCookie(ctx.Resp)
return true, nil return true, nil
} }
func resetLocale(ctx *context.Context, u *models.User) error {
// Language setting of the user overwrites the one previously set
// If the user does not have a locale set, we save the current one.
if len(u.Language) == 0 {
u.Language = ctx.Locale.Language()
if err := models.UpdateUserCols(u, "language"); err != nil {
return err
}
}
middleware.SetLocaleCookie(ctx.Resp, u.Language, 0)
if ctx.Locale.Language() != u.Language {
ctx.Locale = middleware.Locale(ctx.Resp, ctx.Req)
}
return nil
}
func checkAutoLogin(ctx *context.Context) bool { func checkAutoLogin(ctx *context.Context) bool {
// Check auto-login. // Check auto-login.
isSucceed, err := AutoSignIn(ctx) isSucceed, err := AutoSignIn(ctx)
@ -738,6 +761,11 @@ func handleOAuth2SignIn(ctx *context.Context, u *models.User, gothUser goth.User
log.Error("UpdateExternalUser failed: %v", err) log.Error("UpdateExternalUser failed: %v", err)
} }
if err := resetLocale(ctx, u); err != nil {
ctx.ServerError("resetLocale", err)
return
}
if redirectTo := ctx.GetCookie("redirect_to"); len(redirectTo) > 0 { if redirectTo := ctx.GetCookie("redirect_to"); len(redirectTo) > 0 {
middleware.DeleteRedirectToCookie(ctx.Resp) middleware.DeleteRedirectToCookie(ctx.Resp)
ctx.RedirectToFirst(redirectTo) ctx.RedirectToFirst(redirectTo)
@ -1447,6 +1475,11 @@ func handleAccountActivation(ctx *context.Context, user *models.User) {
log.Error("Error storing session[%s]: %v", ctx.Session.ID(), err) log.Error("Error storing session[%s]: %v", ctx.Session.ID(), err)
} }
if err := resetLocale(ctx, user); err != nil {
ctx.ServerError("resetLocale", err)
return
}
ctx.Flash.Success(ctx.Tr("auth.account_activated")) ctx.Flash.Success(ctx.Tr("auth.account_activated"))
ctx.Redirect(setting.AppSubURL + "/") ctx.Redirect(setting.AppSubURL + "/")
} }