|
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\StepupRa\RaBundle\Controller; |
|
20
|
|
|
|
|
21
|
|
|
use Psr\Log\LoggerInterface; |
|
22
|
|
|
use Surfnet\StepupBundle\Command\SwitchLocaleCommand; |
|
23
|
|
|
use Surfnet\StepupBundle\Form\Type\SwitchLocaleType; |
|
24
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
25
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
26
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
|
27
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
|
28
|
|
|
|
|
29
|
|
|
final class LocaleController extends Controller |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
public function switchLocaleAction(Request $request) |
|
32
|
|
|
{ |
|
33
|
|
|
$returnUrl = $request->query->get('return-url'); |
|
34
|
|
|
|
|
35
|
|
|
// Return URLs generated by us always include a path (ie. at least a forward slash) |
|
36
|
|
|
// @see https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Request.php#L878 |
|
37
|
|
|
$domain = $request->getSchemeAndHttpHost() . '/'; |
|
38
|
|
|
if (strpos($returnUrl, $domain) !== 0) { |
|
39
|
|
|
$this->get('logger')->error(sprintf( |
|
40
|
|
|
'Identity "%s" used illegal return-url for redirection after changing locale, aborting request', |
|
41
|
|
|
$this->getIdentity()->id |
|
42
|
|
|
)); |
|
43
|
|
|
|
|
44
|
|
|
throw new BadRequestHttpException('Invalid return-url given'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** @var LoggerInterface $logger */ |
|
48
|
|
|
$logger = $this->get('logger'); |
|
49
|
|
|
$logger->info('Switching locale...'); |
|
50
|
|
|
|
|
51
|
|
|
$identity = $this->getIdentity(); |
|
52
|
|
|
if (!$identity) { |
|
53
|
|
|
throw new AccessDeniedHttpException('Cannot switch locales when not authenticated'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$command = new SwitchLocaleCommand(); |
|
57
|
|
|
$command->identityId = $identity->id; |
|
58
|
|
|
|
|
59
|
|
|
$form = $this->createForm( |
|
60
|
|
|
SwitchLocaleType::class, |
|
61
|
|
|
$command, |
|
62
|
|
|
['route' => 'ra_switch_locale', 'route_parameters' => ['return_url' => $returnUrl]] |
|
63
|
|
|
); |
|
64
|
|
|
$form->handleRequest($request); |
|
65
|
|
|
|
|
66
|
|
|
if ($form->isSubmitted() && !$form->isValid()) { |
|
67
|
|
|
$this->addFlash('error', $this->get('translator')->trans('ra.flash.invalid_switch_locale_form')); |
|
68
|
|
|
$logger->error('The switch locale form unexpectedly contained invalid data'); |
|
69
|
|
|
return $this->redirect($returnUrl); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$service = $this->get('ra.service.identity'); |
|
73
|
|
|
if (!$service->switchLocale($command)) { |
|
74
|
|
|
$this->addFlash('error', $this->get('translator')->trans('ra.flash.error_while_switching_locale')); |
|
75
|
|
|
$logger->error('An error occurred while switching locales'); |
|
76
|
|
|
return $this->redirect($returnUrl); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$logger->info('Successfully switched locale'); |
|
80
|
|
|
|
|
81
|
|
|
return $this->redirect($returnUrl); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return \Surfnet\StepupMiddlewareClientBundle\Identity\Dto\Identity |
|
86
|
|
|
*/ |
|
87
|
|
|
private function getIdentity() |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->get('security.token_storage')->getToken()->getUser(); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.