| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Copyright 2016 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\StepupMiddleware\ManagementBundle\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use DateTime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Liip\FunctionalTestBundle\Validator\DataCollectingValidator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Rhumsaa\Uuid\Uuid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Surfnet\Stepup\Configuration\Value\Institution; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use Surfnet\Stepup\Helper\JsonHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use Surfnet\StepupMiddleware\ApiBundle\Configuration\Service\AllowedSecondFactorListService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use Surfnet\StepupMiddleware\ApiBundle\Configuration\Service\InstitutionConfigurationOptionsService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use Surfnet\StepupMiddleware\ApiBundle\Exception\BadCommandRequestException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use Surfnet\StepupMiddleware\CommandHandlingBundle\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\ReconfigureInstitutionConfigurationOptionsCommand; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | use Surfnet\StepupMiddleware\CommandHandlingBundle\Exception\ForbiddenException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | use Surfnet\StepupMiddleware\CommandHandlingBundle\Pipeline\Pipeline; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | use Surfnet\StepupMiddleware\ManagementBundle\Service\DBALConnectionHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | use Surfnet\StepupMiddleware\ManagementBundle\Validator\Constraints\ValidReconfigureInstitutionsRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | use Symfony\Bridge\Monolog\Logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | use Symfony\Component\HttpFoundation\JsonResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  * @SuppressWarnings(PHPMD.CouplingBetweenObjects) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | final class InstitutionConfigurationController extends Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     public function showAction() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->denyAccessUnlessGranted(['ROLE_MANAGEMENT']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $institutionConfigurationOptions = $this->getInstitutionConfigurationOptionsService() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             ->findAllInstitutionConfigurationOptions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $allowedSecondFactorMap = $this->getAllowedSecondFactorListService()->getAllowedSecondFactorMap(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $overview = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         foreach ($institutionConfigurationOptions as $options) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             // Load the numberOfTokensPerIdentity from the institution config options service | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $numberOfTokensPerIdentity = $this->getInstitutionConfigurationOptionsService() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 ->getMaxNumberOfTokensFor(new Institution($options->institution->getInstitution())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $overview[$options->institution->getInstitution()] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 'use_ra_locations' => $options->useRaLocationsOption, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 'show_raa_contact_information' => $options->showRaaContactInformationOption, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 'verify_email' => $options->verifyEmailOption, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 'number_of_tokens_per_identity' => $numberOfTokensPerIdentity, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 'allowed_second_factors' => $allowedSecondFactorMap->getAllowedSecondFactorListFor( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                     $options->institution | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         return new JsonResponse($overview); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     public function reconfigureAction(Request $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $this->denyAccessUnlessGranted(['ROLE_MANAGEMENT']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $configuration = JsonHelper::decode($request->getContent()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $violations = $this->getValidator()->validate($configuration, new ValidReconfigureInstitutionsRequest()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         if ($violations->count() > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             throw BadCommandRequestException::withViolations('Invalid reconfigure institutions request', $violations); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         if (empty($configuration)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             $this->getLogger()->notice(sprintf('No institutions to reconfigure: empty configuration received')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             return new JsonResponse([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 'status'       => 'OK', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 'processed_by' =>  $request->server->get('SERVER_NAME') ?: $request->server->get('SERVER_ADDR'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 'applied_at'   => (new DateTime())->format(DateTime::ISO8601), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $commands = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         foreach ($configuration as $institution => $options) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             $command                                  = new ReconfigureInstitutionConfigurationOptionsCommand(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             $command->UUID                            = (string) Uuid::uuid4(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             $command->institution                     = $institution; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             $command->useRaLocationsOption            = $options['use_ra_locations']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $command->showRaaContactInformationOption = $options['show_raa_contact_information']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $command->verifyEmailOption               = $options['verify_email']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             $command->numberOfTokensPerIdentityOption = $options['number_of_tokens_per_identity']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             $command->allowedSecondFactors            = $options['allowed_second_factors']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             $commands[] = $command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $this->getLogger()->notice( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             sprintf('Executing %s reconfigure institution configuration options commands', count($commands)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $this->handleCommands($commands); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         return new JsonResponse([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             'status'       => 'OK', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             'processed_by' =>  $request->server->get('SERVER_NAME') ?: $request->server->get('SERVER_ADDR'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             'applied_at'   => (new DateTime())->format(DateTime::ISO8601), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @param Command[] $commands | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     private function handleCommands(array $commands) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $pipeline         = $this->getPipeline(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $connectionHelper = $this->getConnectionHelper(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         $connectionHelper->beginTransaction(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         foreach ($commands as $command) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                 $pipeline->process($command); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             } catch (ForbiddenException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                 $connectionHelper->rollBack(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                 throw new AccessDeniedHttpException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                     sprintf('Processing of command "%s" is forbidden for this client', $command), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                     $e | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             } catch (Exception $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |                 $connectionHelper->rollBack(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |                 throw $exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $connectionHelper->commit(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * @return InstitutionConfigurationOptionsService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     private function getInstitutionConfigurationOptionsService() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         return $this->get('surfnet_stepup_middleware_api.service.institution_configuration_options'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * @return AllowedSecondFactorListService | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 165 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 166 |  |  |     private function getAllowedSecondFactorListService() | 
            
                                                                        
                            
            
                                    
            
            
                | 167 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 168 |  |  |         return $this->get('surfnet_stepup_middleware_api.service.allowed_second_factor_list'); | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      * @return DataCollectingValidator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     private function getValidator() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         return $this->get('validator'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |      * @return Logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     private function getLogger() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         return $this->get('logger'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |      * @return Pipeline | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |     private function getPipeline() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |         return $this->get('pipeline'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |      * @return DBALConnectionHelper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |     private function getConnectionHelper() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |         return $this->get('surfnet_stepup_middleware_management.dbal_connection_helper'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 202 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 203 |  |  |  | 
            
                        
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.