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\CommandHandlingBundle\Configuration\CommandHandler; |
20
|
|
|
|
21
|
|
|
use Broadway\CommandHandling\SimpleCommandHandler; |
22
|
|
|
use Broadway\Repository\AggregateNotFoundException; |
23
|
|
|
use Broadway\Repository\Repository as RepositoryInterface; |
24
|
|
|
use Surfnet\Stepup\Configuration\InstitutionConfiguration; |
25
|
|
|
use Surfnet\Stepup\Configuration\Value\AllowedSecondFactorList; |
26
|
|
|
use Surfnet\Stepup\Configuration\Value\ContactInformation; |
27
|
|
|
use Surfnet\Stepup\Configuration\Value\Institution; |
28
|
|
|
use Surfnet\Stepup\Configuration\Value\InstitutionAuthorizationOption; |
29
|
|
|
use Surfnet\Stepup\Configuration\Value\InstitutionConfigurationId; |
30
|
|
|
use Surfnet\Stepup\Configuration\Value\InstitutionRole; |
31
|
|
|
use Surfnet\Stepup\Configuration\Value\Location; |
32
|
|
|
use Surfnet\Stepup\Configuration\Value\NumberOfTokensPerIdentityOption; |
33
|
|
|
use Surfnet\Stepup\Configuration\Value\RaLocationId; |
34
|
|
|
use Surfnet\Stepup\Configuration\Value\RaLocationName; |
35
|
|
|
use Surfnet\Stepup\Configuration\Value\SelfAssertedTokensOption; |
|
|
|
|
36
|
|
|
use Surfnet\Stepup\Configuration\Value\SelfVetOption; |
|
|
|
|
37
|
|
|
use Surfnet\Stepup\Configuration\Value\ShowRaaContactInformationOption; |
38
|
|
|
use Surfnet\Stepup\Configuration\Value\SsoOn2faOption; |
|
|
|
|
39
|
|
|
use Surfnet\Stepup\Configuration\Value\SsoRegistrationBypassOption; |
|
|
|
|
40
|
|
|
use Surfnet\Stepup\Configuration\Value\UseRaLocationsOption; |
41
|
|
|
use Surfnet\Stepup\Configuration\Value\VerifyEmailOption; |
42
|
|
|
use Surfnet\StepupBundle\Value\SecondFactorType; |
43
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\AddRaLocationCommand; |
44
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\ChangeRaLocationCommand; |
45
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\CreateInstitutionConfigurationCommand; |
46
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\ReconfigureInstitutionConfigurationOptionsCommand; |
47
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\RemoveInstitutionConfigurationByUnnormalizedIdCommand; |
48
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\RemoveRaLocationCommand; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) Value objects |
52
|
|
|
*/ |
|
|
|
|
53
|
|
|
class InstitutionConfigurationCommandHandler extends SimpleCommandHandler |
54
|
|
|
{ |
55
|
|
|
public function __construct(private readonly RepositoryInterface $repository) |
56
|
|
|
{ |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function handleCreateInstitutionConfigurationCommand(CreateInstitutionConfigurationCommand $command): void |
60
|
|
|
{ |
61
|
|
|
$institution = new Institution($command->institution); |
62
|
|
|
$institutionConfigurationId = InstitutionConfigurationId::normalizedFrom($institution); |
63
|
|
|
|
64
|
|
|
try { |
65
|
|
|
/** @var InstitutionConfiguration $institutionConfiguration */ |
|
|
|
|
66
|
|
|
$institutionConfiguration = $this->repository->load( |
67
|
|
|
$institutionConfigurationId->getInstitutionConfigurationId(), |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$institutionConfiguration->rebuild(); |
71
|
|
|
} catch (AggregateNotFoundException) { |
72
|
|
|
$institutionConfiguration = InstitutionConfiguration::create($institutionConfigurationId, $institution); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->repository->save($institutionConfiguration); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function handleReconfigureInstitutionConfigurationOptionsCommand( |
79
|
|
|
ReconfigureInstitutionConfigurationOptionsCommand $command, |
80
|
|
|
): void { |
81
|
|
|
$institution = new Institution($command->institution); |
82
|
|
|
|
83
|
|
|
$allowedSecondFactors = array_map( |
84
|
|
|
fn($allowedSecondFactor): SecondFactorType => new SecondFactorType($allowedSecondFactor), |
85
|
|
|
$command->allowedSecondFactors, |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
$institutionConfiguration = $this->loadInstitutionConfigurationFor($institution); |
|
|
|
|
90
|
|
|
$institutionConfiguration->configureUseRaLocationsOption( |
91
|
|
|
new UseRaLocationsOption($command->useRaLocationsOption), |
92
|
|
|
); |
93
|
|
|
$institutionConfiguration->configureVerifyEmailOption( |
94
|
|
|
new VerifyEmailOption($command->verifyEmailOption), |
95
|
|
|
); |
96
|
|
|
$institutionConfiguration->configureNumberOfTokensPerIdentityOption( |
97
|
|
|
new NumberOfTokensPerIdentityOption($command->numberOfTokensPerIdentityOption), |
98
|
|
|
); |
99
|
|
|
$institutionConfiguration->configureShowRaaContactInformationOption( |
100
|
|
|
new ShowRaaContactInformationOption($command->showRaaContactInformationOption), |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
// Configure the authorization options on the aggregate |
104
|
|
|
$institutionConfiguration->updateUseRaOption( |
105
|
|
|
InstitutionAuthorizationOption::fromInstitutionConfig( |
106
|
|
|
InstitutionRole::useRa(), |
107
|
|
|
$command->useRaOption, |
108
|
|
|
), |
109
|
|
|
); |
110
|
|
|
$institutionConfiguration->updateUseRaaOption( |
111
|
|
|
InstitutionAuthorizationOption::fromInstitutionConfig( |
112
|
|
|
InstitutionRole::useRaa(), |
113
|
|
|
$command->useRaaOption, |
114
|
|
|
), |
115
|
|
|
); |
116
|
|
|
$institutionConfiguration->updateSelectRaaOption( |
117
|
|
|
InstitutionAuthorizationOption::fromInstitutionConfig( |
118
|
|
|
InstitutionRole::selectRaa(), |
119
|
|
|
$command->selectRaaOption, |
120
|
|
|
), |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
$institutionConfiguration->updateAllowedSecondFactorList( |
124
|
|
|
AllowedSecondFactorList::ofTypes($allowedSecondFactors), |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
// Handle optional options |
128
|
|
|
$selfVetOptionValue = $command->selfVetOption ?? SelfVetOption::getDefault()->isEnabled(); |
129
|
|
|
$institutionConfiguration->configureSelfVetOption(new SelfVetOption($selfVetOptionValue)); |
130
|
|
|
|
131
|
|
|
$ssoOn2faOptionValue = $command->ssoOn2faOption ?? SsoOn2faOption::getDefault()->isEnabled(); |
132
|
|
|
$institutionConfiguration->configureSsoOn2faOption(new SsoOn2faOption($ssoOn2faOptionValue)); |
133
|
|
|
|
134
|
|
|
$ssoRegistrationBypassOptionValue = $command->ssoRegistrationBypassOption ?? SsoRegistrationBypassOption::getDefault()->isEnabled(); |
135
|
|
|
$institutionConfiguration->configureSsoRegistrationBypassOption(new SsoRegistrationBypassOption($ssoRegistrationBypassOptionValue)); |
136
|
|
|
|
137
|
|
|
$satOption = $command->selfAssertedTokensOption ?? SelfAssertedTokensOption::getDefault()->isEnabled(); |
138
|
|
|
$institutionConfiguration->configureSelfAssertedTokensOption( |
139
|
|
|
new SelfAssertedTokensOption($satOption), |
140
|
|
|
); |
141
|
|
|
|
142
|
|
|
$this->repository->save($institutionConfiguration); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function handleAddRaLocationCommand(AddRaLocationCommand $command): void |
146
|
|
|
{ |
147
|
|
|
$institution = new Institution($command->institution); |
148
|
|
|
|
149
|
|
|
$institutionConfiguration = $this->loadInstitutionConfigurationFor($institution); |
|
|
|
|
150
|
|
|
$institutionConfiguration->addRaLocation( |
151
|
|
|
new RaLocationId($command->raLocationId), |
152
|
|
|
new RaLocationName($command->raLocationName), |
153
|
|
|
new Location($command->location), |
154
|
|
|
new ContactInformation($command->contactInformation), |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
$this->repository->save($institutionConfiguration); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function handleChangeRaLocationCommand(ChangeRaLocationCommand $command): void |
161
|
|
|
{ |
162
|
|
|
$institution = new Institution($command->institution); |
163
|
|
|
|
164
|
|
|
$institutionConfiguration = $this->loadInstitutionConfigurationFor($institution); |
|
|
|
|
165
|
|
|
$institutionConfiguration->changeRaLocation( |
166
|
|
|
new RaLocationId($command->raLocationId), |
167
|
|
|
new RaLocationName($command->raLocationName), |
168
|
|
|
new Location($command->location), |
169
|
|
|
new ContactInformation($command->contactInformation), |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
$this->repository->save($institutionConfiguration); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function handleRemoveRaLocationCommand(RemoveRaLocationCommand $command): void |
176
|
|
|
{ |
177
|
|
|
$institution = new Institution($command->institution); |
178
|
|
|
|
179
|
|
|
$institutionConfiguration = $this->loadInstitutionConfigurationFor($institution); |
|
|
|
|
180
|
|
|
$institutionConfiguration->removeRaLocation(new RaLocationId($command->raLocationId)); |
181
|
|
|
|
182
|
|
|
$this->repository->save($institutionConfiguration); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function handleRemoveInstitutionConfigurationByUnnormalizedIdCommand( |
186
|
|
|
RemoveInstitutionConfigurationByUnnormalizedIdCommand $command, |
187
|
|
|
): void { |
188
|
|
|
$institution = new Institution($command->institution); |
189
|
|
|
|
190
|
|
|
$institutionConfigurationId = InstitutionConfigurationId::normalizedFrom($institution); |
191
|
|
|
/** @var InstitutionConfiguration $institutionConfiguration */ |
|
|
|
|
192
|
|
|
$institutionConfiguration = $this->repository->load( |
193
|
|
|
$institutionConfigurationId->getInstitutionConfigurationId(), |
194
|
|
|
); |
195
|
|
|
$institutionConfiguration->destroy(); |
196
|
|
|
|
197
|
|
|
$this->repository->save($institutionConfiguration); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
|
|
|
|
201
|
|
|
* @return InstitutionConfiguration |
|
|
|
|
202
|
|
|
* @deprecated Should be used until existing institution configurations have been migrated to using normalized ids |
203
|
|
|
* |
204
|
|
|
*/ |
|
|
|
|
205
|
|
|
private function loadInstitutionConfigurationFor(Institution $institution): InstitutionConfiguration |
|
|
|
|
206
|
|
|
{ |
207
|
|
|
try { |
208
|
|
|
$institutionConfigurationId = InstitutionConfigurationId::normalizedFrom($institution); |
209
|
|
|
/** @var InstitutionConfiguration $institutionConfiguration */ |
|
|
|
|
210
|
|
|
$institutionConfiguration = $this->repository->load( |
211
|
|
|
$institutionConfigurationId->getInstitutionConfigurationId(), |
212
|
|
|
); |
213
|
|
|
} catch (AggregateNotFoundException) { |
214
|
|
|
$institutionConfigurationId = InstitutionConfigurationId::from($institution); |
|
|
|
|
215
|
|
|
/** @var InstitutionConfiguration $institutionConfiguration */ |
|
|
|
|
216
|
|
|
$institutionConfiguration = $this->repository->load( |
217
|
|
|
$institutionConfigurationId->getInstitutionConfigurationId(), |
218
|
|
|
); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return $institutionConfiguration; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|