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