1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2018 SURFnet B.V. |
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 Surfnet\StepupMiddlewareClientBundle\Identity\Dto\Identity; |
22
|
|
|
use Surfnet\StepupRa\RaBundle\Command\ChangeRaaInstitutionCommand; |
23
|
|
|
use Surfnet\StepupRa\RaBundle\Form\Type\RaaInstitutionSelectionType; |
24
|
|
|
use Surfnet\StepupRa\RaBundle\Security\Authentication\Token\SamlToken; |
25
|
|
|
use Surfnet\StepupRa\RaBundle\Security\Authorization\Voter\AllowedToSwitchInstitutionVoter; |
26
|
|
|
use Surfnet\StepupRa\RaBundle\Service\InstitutionConfigurationOptionsService; |
27
|
|
|
use Surfnet\StepupRa\RaBundle\Service\RaListingService; |
28
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
29
|
|
|
use Symfony\Component\HttpFoundation\Request; |
30
|
|
|
|
31
|
|
|
class RaaController extends Controller |
32
|
|
|
{ |
33
|
|
|
public function selectInstitutionAction(Request $request) |
34
|
|
|
{ |
35
|
|
|
$this->denyAccessUnlessGranted([AllowedToSwitchInstitutionVoter::RAA_SWITCHING]); |
36
|
|
|
|
37
|
|
|
/** @var SamlToken $token */ |
38
|
|
|
$token = $this->get('security.token_storage')->getToken(); |
39
|
|
|
$logger = $this->get('logger'); |
40
|
|
|
|
41
|
|
|
/** @var Identity $identity */ |
42
|
|
|
$identity = $token->getUser(); |
43
|
|
|
$institution = $identity->institution; |
44
|
|
|
|
45
|
|
|
$logger->notice(sprintf('Select Institution for RAA "%s"', $identity->id)); |
46
|
|
|
|
47
|
|
|
$raaSwitcherOptions = $this |
48
|
|
|
->getRaListingService() |
49
|
|
|
->createChoiceListFor($identity->id, $token->getIdentityInstitution()); |
50
|
|
|
|
51
|
|
|
$command = new ChangeRaaInstitutionCommand(); |
52
|
|
|
$command->institution = $institution; |
53
|
|
|
$command->availableInstitutions = $raaSwitcherOptions; |
54
|
|
|
|
55
|
|
|
$form = $this->createForm(RaaInstitutionSelectionType::class, $command); |
56
|
|
|
$form->handleRequest($request); |
57
|
|
|
|
58
|
|
View Code Duplication |
if ($form->isSubmitted() && $form->isValid()) { |
|
|
|
|
59
|
|
|
$institutionConfigurationOptions = $this->getInstitutionConfigurationOptionsService() |
60
|
|
|
->getInstitutionConfigurationOptionsFor($command->institution); |
61
|
|
|
$token->changeInstitutionScope($command->institution, $institutionConfigurationOptions); |
|
|
|
|
62
|
|
|
|
63
|
|
|
$flashMessage = $this->get('translator') |
64
|
|
|
->trans('ra.sraa.changed_institution', ['%institution%' => $command->institution]); |
65
|
|
|
$this->get('session')->getFlashBag()->add('success', $flashMessage); |
66
|
|
|
|
67
|
|
|
$logger->notice(sprintf( |
68
|
|
|
'RAA "%s" successfully switched to institution "%s"', |
69
|
|
|
$identity->id, |
70
|
|
|
$command->institution |
71
|
|
|
)); |
72
|
|
|
|
73
|
|
|
return $this->redirect($this->generateUrl('ra_vetting_search')); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$logger->notice(sprintf('Showing select institution form for RAA "%s"', $identity->id)); |
77
|
|
|
|
78
|
|
|
return $this->render( |
79
|
|
|
// Reuse the SRAA switcher twig template |
80
|
|
|
'SurfnetStepupRaRaBundle:Sraa:selectInstitution.html.twig', |
81
|
|
|
['form' => $form->createView()] |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return RaListingService |
87
|
|
|
*/ |
88
|
|
|
private function getRaListingService() |
89
|
|
|
{ |
90
|
|
|
return $this->get('ra.service.ra_listing'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return InstitutionConfigurationOptionsService |
95
|
|
|
*/ |
96
|
|
|
private function getInstitutionConfigurationOptionsService() |
97
|
|
|
{ |
98
|
|
|
return $this->get('ra.service.institution_configuration_options'); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.