Skip to content
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

Reset locale on login #18023

Merged
merged 4 commits into from
Dec 19, 2021
Merged
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
33 changes: 33 additions & 0 deletions routers/web/user/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,33 @@ func AutoSignIn(ctx *context.Context) (bool, error) {
return false, err
}

if err := resetLocale(ctx, u); err != nil {
return false, err
}

middleware.DeleteCSRFCookie(ctx.Resp)
return true, nil
}

func resetLocale(ctx *context.Context, u *user_model.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 := user_model.UpdateUserCols(db.DefaultContext, 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 {
// Check auto-login.
isSucceed, err := AutoSignIn(ctx)
Expand Down Expand Up @@ -832,6 +855,11 @@ func handleOAuth2SignIn(ctx *context.Context, source *login.Source, u *user_mode
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 {
middleware.DeleteRedirectToCookie(ctx.Resp)
ctx.RedirectToFirst(redirectTo)
Expand Down Expand Up @@ -1573,6 +1601,11 @@ func handleAccountActivation(ctx *context.Context, user *user_model.User) {
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.Redirect(setting.AppSubURL + "/")
}
Expand Down