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