1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2014 SURFnet bv |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
|
|
|
|
20
|
|
|
|
21
|
|
|
namespace Surfnet\StepupSelfService\SelfServiceBundle\EventListener; |
22
|
|
|
|
23
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\Identity; |
24
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
25
|
|
|
use Symfony\Component\HttpKernel\Event\RequestEvent; |
26
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
27
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
28
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
29
|
|
|
|
30
|
|
|
final readonly class LocaleListener implements EventSubscriberInterface |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
public function __construct(private TokenStorageInterface $tokenStorage, private TranslatorInterface $translator) |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function setRequestLocale(RequestEvent $event): void |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
$token = $this->tokenStorage->getToken(); |
39
|
|
|
|
40
|
|
|
if (!$token instanceof \Symfony\Component\Security\Core\Authentication\Token\TokenInterface) { |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$identity = $token->getUser(); |
45
|
|
|
|
46
|
|
|
// Anonymous usage like /authentication/metadata has just "anonymous" as identity. |
47
|
|
|
if (!$identity instanceof Identity) { |
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$request = $event->getRequest(); |
52
|
|
|
$request->setLocale($identity->preferredLocale); |
53
|
|
|
|
54
|
|
|
// As per \Symfony\Component\HttpKernel\EventListener\TranslatorListener::setLocale() |
55
|
|
|
try { |
56
|
|
|
$this->translator->setLocale($request->getLocale()); |
57
|
|
|
} catch (\InvalidArgumentException) { |
58
|
|
|
$this->translator->setLocale($request->getDefaultLocale()); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public static function getSubscribedEvents(): array |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
return [ |
65
|
|
|
// Default locale listener listens at P16 |
66
|
|
|
// Translator listener, which sets the locale for the translator, listens at P10 |
67
|
|
|
// The firewall, which makes the token available, listens at P8 |
68
|
|
|
// We must jump in after the firewall, forcing us to overwrite the translator locale. |
69
|
|
|
KernelEvents::REQUEST => ['setRequestLocale', 7], |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|