|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Copyright 2015 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\Controller; |
|
22
|
|
|
|
|
23
|
|
|
use Psr\Log\LoggerInterface; |
|
24
|
|
|
use Surfnet\StepupBundle\Command\SwitchLocaleCommand; |
|
25
|
|
|
use Surfnet\StepupBundle\Form\Type\SwitchLocaleType; |
|
26
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\IdentityService; |
|
27
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
28
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
29
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
30
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
|
31
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
|
32
|
|
|
use Symfony\Component\Routing\Attribute\Route; |
|
33
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
34
|
|
|
|
|
35
|
|
|
final class LocaleController extends AbstractController |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
public function __construct( |
|
|
|
|
|
|
38
|
|
|
private readonly LoggerInterface $logger, |
|
39
|
|
|
private readonly TranslatorInterface $translator, |
|
40
|
|
|
private readonly IdentityService $identityService, |
|
41
|
|
|
) { |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
#[Route( |
|
45
|
|
|
path: '/switch-locale', |
|
46
|
|
|
name: 'ss_switch_locale', |
|
47
|
|
|
requirements: ['return-url' => '.+'], |
|
48
|
|
|
methods: ['POST'] |
|
49
|
|
|
)] |
|
50
|
|
|
public function switchLocale(Request $request): RedirectResponse |
|
|
|
|
|
|
51
|
|
|
{ |
|
52
|
|
|
$returnUrl = $request->query->get('return-url'); |
|
53
|
|
|
|
|
54
|
|
|
// Return URLs generated by us always include a path (ie. at least a forward slash) |
|
55
|
|
|
// @see https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Request.php#L878 |
|
56
|
|
|
$domain = $request->getSchemeAndHttpHost() . '/'; |
|
57
|
|
|
if (!str_starts_with($returnUrl, $domain)) { |
|
58
|
|
|
$this->logger->error(sprintf( |
|
|
|
|
|
|
59
|
|
|
'Identity "%s" used illegal return-url for redirection after changing locale, aborting request', |
|
60
|
|
|
$this->getUser()->getIdentity()->id |
|
|
|
|
|
|
61
|
|
|
)); |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
throw new BadRequestHttpException('Invalid return-url given'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$this->logger->info('Switching locale...'); |
|
67
|
|
|
|
|
68
|
|
|
$identity = $this->getUser()->getIdentity(); |
|
69
|
|
|
if (!$identity) { |
|
70
|
|
|
throw new AccessDeniedHttpException('Cannot switch locales when not authenticated'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$command = new SwitchLocaleCommand(); |
|
74
|
|
|
$command->identityId = $identity->id; |
|
75
|
|
|
|
|
76
|
|
|
$form = $this->createForm( |
|
77
|
|
|
SwitchLocaleType::class, |
|
78
|
|
|
$command, |
|
79
|
|
|
['route' => 'ss_switch_locale', 'route_parameters' => ['return_url' => $returnUrl]] |
|
80
|
|
|
); |
|
81
|
|
|
$form->handleRequest($request); |
|
82
|
|
|
|
|
83
|
|
|
if (!$form->isSubmitted() || !$form->isValid()) { |
|
84
|
|
|
$this->addFlash('error', $this->translator->trans('ss.flash.invalid_switch_locale_form')); |
|
85
|
|
|
$this->logger->error('The switch locale form unexpectedly contained invalid data'); |
|
86
|
|
|
return $this->redirect($returnUrl); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if (!$this->identityService->switchLocale($command)) { |
|
90
|
|
|
$this->addFlash('error', $this->translator->trans('ss.flash.error_while_switching_locale')); |
|
91
|
|
|
$this->logger->error('An error occurred while switching locales'); |
|
92
|
|
|
return $this->redirect($returnUrl); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$this->logger->info('Successfully switched locale'); |
|
96
|
|
|
|
|
97
|
|
|
return $this->redirect($returnUrl); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|