From 4d6293500dec6034b4b9daa5d0a13e4ff68d7d8a Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sat, 18 Dec 2021 19:57:18 +0000 Subject: [PATCH 1/2] Reset locale on login Fix #18020 Signed-off-by: Andrew Thornton --- routers/web/user/auth.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/routers/web/user/auth.go b/routers/web/user/auth.go index 0f1ede85a73a0..0417dc3f7c4da 100644 --- a/routers/web/user/auth.go +++ b/routers/web/user/auth.go @@ -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) @@ -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("UpdateUserCols", err) + return + } + if redirectTo := ctx.GetCookie("redirect_to"); len(redirectTo) > 0 { middleware.DeleteRedirectToCookie(ctx.Resp) ctx.RedirectToFirst(redirectTo) @@ -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 + "/") } From 893a32c145529a32e7ac771ef53977b0ffe13fb8 Mon Sep 17 00:00:00 2001 From: zeripath Date: Sun, 19 Dec 2021 05:01:27 +0000 Subject: [PATCH 2/2] Update routers/web/user/auth.go Co-authored-by: Gusted --- routers/web/user/auth.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/web/user/auth.go b/routers/web/user/auth.go index 0417dc3f7c4da..8cb55a447b5f4 100644 --- a/routers/web/user/auth.go +++ b/routers/web/user/auth.go @@ -856,7 +856,7 @@ func handleOAuth2SignIn(ctx *context.Context, source *login.Source, u *user_mode } if err := resetLocale(ctx, u); err != nil { - ctx.ServerError("UpdateUserCols", err) + ctx.ServerError("resetLocale", err) return }