diff --git a/app/Modules/Users/Application/UseCases/ChangePreferredLocale.php b/app/Modules/Users/Application/UseCases/ChangePreferredLocale.php index c56c84b..4bfadc1 100644 --- a/app/Modules/Users/Application/UseCases/ChangePreferredLocale.php +++ b/app/Modules/Users/Application/UseCases/ChangePreferredLocale.php @@ -4,19 +4,24 @@ declare(strict_types=1); namespace App\Modules\Users\Application\UseCases; +use App\Modules\Shared\Application\Contracts\TransactionManager; use App\Modules\Users\Domain\Contracts\UserAccountRepository; use App\Modules\Users\Domain\Enums\Locale; /** - * A user chooses the language of the user interface from their profile menu. - * The choice is kept on the account, so it follows them to every device. + * A user chooses the language of the user interface: from their profile menu, + * or on the signed-out pages before signing in. The choice is kept on the + * account, so it follows them to every device. */ final class ChangePreferredLocale { - public function __construct(private UserAccountRepository $users) {} + public function __construct( + private UserAccountRepository $users, + private TransactionManager $transactions, + ) {} public function execute(int $userId, Locale $locale): void { - $this->users->changeLocale($userId, $locale); + $this->transactions->run(fn () => $this->users->changeLocale($userId, $locale)); } } diff --git a/app/Modules/Users/Domain/Enums/Locale.php b/app/Modules/Users/Domain/Enums/Locale.php index 70f0609..ad99564 100644 --- a/app/Modules/Users/Domain/Enums/Locale.php +++ b/app/Modules/Users/Domain/Enums/Locale.php @@ -6,21 +6,11 @@ namespace App\Modules\Users\Domain\Enums; /** * The languages of the user interface. A user's choice is kept on their - * account; signed-out pages keep it in the session. + * account; signed-out pages keep it in the session. Their names are the + * translations layout.languages.. */ enum Locale: string { case Finnish = 'fi'; case English = 'en'; - - /** - * The language's name in the language itself, as language pickers show it. - */ - public function nativeName(): string - { - return match ($this) { - self::Finnish => 'Suomi', - self::English => 'English', - }; - } } diff --git a/app/Modules/Users/Infrastructure/Providers/UsersServiceProvider.php b/app/Modules/Users/Infrastructure/Providers/UsersServiceProvider.php index f532c2f..deaa72a 100644 --- a/app/Modules/Users/Infrastructure/Providers/UsersServiceProvider.php +++ b/app/Modules/Users/Infrastructure/Providers/UsersServiceProvider.php @@ -14,13 +14,16 @@ use App\Modules\Users\Infrastructure\Persistence\Repositories\EloquentUserAccoun use App\Modules\Users\Infrastructure\Persistence\Repositories\EloquentUserAccountRepository; use App\Modules\Users\Infrastructure\SystemClock; use App\Modules\Users\Presentation\Console\CreateAdminCommand; +use App\Modules\Users\Presentation\Http\Listeners\StoreLocaleChosenBeforeLogin; use App\Modules\Users\Presentation\Http\Middleware\EnsureAccountIsUsable; use App\Modules\Users\Presentation\Http\Middleware\SetLocale; +use Illuminate\Auth\Events\Login; use Illuminate\Auth\Notifications\ResetPassword; use Illuminate\Contracts\Auth\CanResetPassword; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Routing\Router; use Illuminate\Session\Middleware\AuthenticateSession; +use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Gate; use Illuminate\Support\ServiceProvider; use Illuminate\Validation\Rules\Password; @@ -63,6 +66,7 @@ class UsersServiceProvider extends ServiceProvider // The language of the user interface; after the session has started. $router->pushMiddlewareToGroup('web', SetLocale::class); + Event::listen(Login::class, StoreLocaleChosenBeforeLogin::class); if ($this->app->runningInConsole()) { $this->commands([CreateAdminCommand::class]); diff --git a/app/Modules/Users/Presentation/Http/Controllers/SwitchLocaleController.php b/app/Modules/Users/Presentation/Http/Controllers/SwitchLocaleController.php index 5f262e4..e10279e 100644 --- a/app/Modules/Users/Presentation/Http/Controllers/SwitchLocaleController.php +++ b/app/Modules/Users/Presentation/Http/Controllers/SwitchLocaleController.php @@ -30,6 +30,8 @@ class SwitchLocaleController // The signed-in user object may outlive this request (e.g. Octane, in-process browser tests). $user->locale = $locale; + } else { + $request->session()->put(SetLocale::CHOSEN_SESSION_KEY, true); } $request->session()->put(SetLocale::SESSION_KEY, $locale->value); diff --git a/app/Modules/Users/Presentation/Http/Listeners/StoreLocaleChosenBeforeLogin.php b/app/Modules/Users/Presentation/Http/Listeners/StoreLocaleChosenBeforeLogin.php new file mode 100644 index 0000000..60bdc8c --- /dev/null +++ b/app/Modules/Users/Presentation/Http/Listeners/StoreLocaleChosenBeforeLogin.php @@ -0,0 +1,39 @@ +user instanceof User || ! $this->session->pull(SetLocale::CHOSEN_SESSION_KEY, false)) { + return; + } + + $locale = Locale::tryFrom((string) $this->session->get(SetLocale::SESSION_KEY)); + + if ($locale === null || $locale === $event->user->locale) { + return; + } + + $this->changeLocale->execute($event->user->id, $locale); + $event->user->locale = $locale; + } +} diff --git a/app/Modules/Users/Presentation/Http/Middleware/EnsureAccountIsUsable.php b/app/Modules/Users/Presentation/Http/Middleware/EnsureAccountIsUsable.php index 7168e61..75c7658 100644 --- a/app/Modules/Users/Presentation/Http/Middleware/EnsureAccountIsUsable.php +++ b/app/Modules/Users/Presentation/Http/Middleware/EnsureAccountIsUsable.php @@ -2,6 +2,7 @@ namespace App\Modules\Users\Presentation\Http\Middleware; +use App\Models\User; use App\Modules\Users\Application\UseCases\CheckLoginAllowed; use Closure; use Illuminate\Http\Request; @@ -21,10 +22,18 @@ class EnsureAccountIsUsable $userId = Auth::id(); if ($userId !== null && ! $this->checkLoginAllowed->execute((int) $userId)) { + $user = $request->user(); + $locale = $user instanceof User ? $user->locale->value : null; + Auth::guard('web')->logout(); $request->session()->invalidate(); $request->session()->regenerateToken(); + // The login page explains why, in the language the user had chosen. + if ($locale !== null) { + $request->session()->put(SetLocale::SESSION_KEY, $locale); + } + // The reason goes in the URL: Livewire follows this redirect with // fetch first, which would consume a flashed message. return redirect()->route('login', ['access' => 'ended']); diff --git a/app/Modules/Users/Presentation/Http/Middleware/SetLocale.php b/app/Modules/Users/Presentation/Http/Middleware/SetLocale.php index 023b4f2..1f0cb71 100644 --- a/app/Modules/Users/Presentation/Http/Middleware/SetLocale.php +++ b/app/Modules/Users/Presentation/Http/Middleware/SetLocale.php @@ -19,6 +19,12 @@ class SetLocale { public const SESSION_KEY = 'locale'; + /** + * Set when the user picked the language on a signed-out page: signing in + * then stores it on the account (StoreLocaleChosenBeforeLogin). + */ + public const CHOSEN_SESSION_KEY = 'locale_chosen'; + public function handle(Request $request, Closure $next): Response { $user = $request->user(); diff --git a/lang/en/layout.php b/lang/en/layout.php index 6c1a2d5..cfd5ac2 100644 --- a/lang/en/layout.php +++ b/lang/en/layout.php @@ -32,6 +32,11 @@ return [ 'workflow_templates' => 'Workflow templates', 'price_list_admin' => 'Price list maintenance', 'current_section' => '(current section)', + // Each language by its own name, the same in every language. + 'languages' => [ + 'fi' => 'Suomi', + 'en' => 'English', + ], 'profile' => [ 'menu' => 'Profile: :name', 'language' => 'Language', diff --git a/lang/fi/layout.php b/lang/fi/layout.php index 8f7ae59..e1ca17a 100644 --- a/lang/fi/layout.php +++ b/lang/fi/layout.php @@ -32,6 +32,11 @@ return [ 'workflow_templates' => 'Työnkulkupohjat', 'price_list_admin' => 'Hinnaston ylläpito', 'current_section' => '(nykyinen osio)', + // Each language by its own name, the same in every language. + 'languages' => [ + 'fi' => 'Suomi', + 'en' => 'English', + ], 'profile' => [ 'menu' => 'Profiili: :name', 'language' => 'Kieli', diff --git a/resources/views/components/profile-menu.blade.php b/resources/views/components/profile-menu.blade.php index 5a95686..a024024 100644 --- a/resources/views/components/profile-menu.blade.php +++ b/resources/views/components/profile-menu.blade.php @@ -66,7 +66,7 @@ @if ($isCurrent)