Completed
Pull Request — develop (#261)
by Michiel
04:59 queued 02:35
created

applyYubikeySecondFactorBootstrappedEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A RaCandidateProjector::applyCompliedWithVettedSecondFactorRevocationEvent() 0 5 1
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\StepupMiddleware\ApiBundle\Identity\Projector;
20
21
use Broadway\ReadModel\Projector;
22
use Surfnet\Stepup\Configuration\Event\InstitutionConfigurationRemovedEvent;
23
use Surfnet\Stepup\Configuration\Event\SelectRaaOptionChangedEvent;
24
use Surfnet\Stepup\Configuration\Event\SraaUpdatedEvent;
25
use Surfnet\Stepup\Identity\Collection\InstitutionCollection;
26
use Surfnet\Stepup\Identity\Event\IdentityAccreditedAsRaaForInstitutionEvent;
27
use Surfnet\Stepup\Identity\Event\IdentityAccreditedAsRaForInstitutionEvent;
28
use Surfnet\Stepup\Identity\Event\RegistrationAuthorityRetractedForInstitutionEvent;
29
use Surfnet\Stepup\Identity\Value\CommonName;
30
use Surfnet\Stepup\Identity\Value\Email;
31
use Surfnet\Stepup\Identity\Value\IdentityId;
32
use Surfnet\Stepup\Identity\Value\Institution;
33
use Surfnet\Stepup\Configuration\Value\Institution as ConfigurationInstitution;
34
use Surfnet\Stepup\Identity\Event\CompliedWithVettedSecondFactorRevocationEvent;
35
use Surfnet\Stepup\Identity\Event\IdentityAccreditedAsRaaEvent;
36
use Surfnet\Stepup\Identity\Event\IdentityAccreditedAsRaEvent;
37
use Surfnet\Stepup\Identity\Event\IdentityForgottenEvent;
38
use Surfnet\Stepup\Identity\Event\RegistrationAuthorityRetractedEvent;
39
use Surfnet\Stepup\Identity\Event\SecondFactorVettedEvent;
40
use Surfnet\Stepup\Identity\Event\VettedSecondFactorRevokedEvent;
41
use Surfnet\Stepup\Identity\Event\YubikeySecondFactorBootstrappedEvent;
42
use Surfnet\Stepup\Identity\Value\NameId;
43
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Repository\InstitutionAuthorizationRepository;
44
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaCandidate;
45
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\IdentityRepository;
46
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\RaCandidateRepository;
47
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\RaListingRepository;
48
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\RaSecondFactorRepository;
49
50
/**
51
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
52
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
53
 */
54
class RaCandidateProjector extends Projector
55
{
56
    /**
57
     * @var RaCandidateRepository
58
     */
59
    private $raCandidateRepository;
60
61
    /**
62
     * @var RaListingRepository
63
     */
64
    private $raListingRepository;
65
66
    /**
67
     * @var institutionAuthorizationRepository
68
     */
69
    private $institutionAuthorizationRepository;
70
    /**
71
     * @var IdentityRepository
72
     */
73
    private $identityRepository;
74
    /**
75
     * @var RaSecondFactorRepository
76
     */
77
    private $raSecondFactorRepository;
78
79
    public function __construct(
80
        RaCandidateRepository $raCandidateRepository,
81
        RaListingRepository $raListingRepository,
82
        InstitutionAuthorizationRepository $institutionAuthorizationRepository,
83
        IdentityRepository $identityRepository,
84
        RaSecondFactorRepository $raSecondFactorRepository
85
    ) {
86
        $this->raCandidateRepository = $raCandidateRepository;
87
        $this->raListingRepository = $raListingRepository;
88
        $this->institutionAuthorizationRepository = $institutionAuthorizationRepository;
0 ignored issues
show
Documentation Bug introduced by
It seems like $institutionAuthorizationRepository of type object<Surfnet\StepupMid...uthorizationRepository> is incompatible with the declared type object<Surfnet\StepupMid...uthorizationRepository> of property $institutionAuthorizationRepository.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
89
        $this->identityRepository = $identityRepository;
90
        $this->raSecondFactorRepository = $raSecondFactorRepository;
91
    }
92
93
    /**
94
     * @param VettedSecondFactorRevokedEvent $event
95
     * @return void
96
     */
97
    public function applyVettedSecondFactorRevokedEvent(VettedSecondFactorRevokedEvent $event)
98
    {
99
        $this->raCandidateRepository->removeByIdentityId($event->identityId);
100
    }
101
102
    /**
103
     * @param CompliedWithVettedSecondFactorRevocationEvent $event
104
     * @return void
105
     */
106
    public function applyCompliedWithVettedSecondFactorRevocationEvent(
107
        CompliedWithVettedSecondFactorRevocationEvent $event
108
    ) {
109
        $this->raCandidateRepository->removeByIdentityId($event->identityId);
110
    }
111
112
    /**
113
     * @param SraaUpdatedEvent $event
114
     *
115
     * Removes all RaCandidates that have a nameId matching an SRAA, as they cannot be made RA(A) as they
116
     * already are SRAA.
117
     */
118
    public function applySraaUpdatedEvent(SraaUpdatedEvent $event)
119
    {
120
        $this->raCandidateRepository->removeByNameIds($event->sraaList);
121
    }
122
123
    /**
124
     * @param IdentityAccreditedAsRaForInstitutionEvent $event
125
     * @return void
126
     */
127
    public function applyIdentityAccreditedAsRaForInstitutionEvent(IdentityAccreditedAsRaForInstitutionEvent $event)
128
    {
129
        $this->raCandidateRepository->removeByIdentityIdAndRaInstitution($event->identityId, $event->raInstitution);
130
    }
131
132
    /**
133
     * @param IdentityAccreditedAsRaaForInstitutionEvent $event
134
     * @return void
135
     */
136
    public function applyIdentityAccreditedAsRaaForInstitutionEvent(IdentityAccreditedAsRaaForInstitutionEvent $event)
137
    {
138
        $this->raCandidateRepository->removeByIdentityIdAndRaInstitution($event->identityId, $event->raInstitution);
139
    }
140
141
    /**
142
     * @param RegistrationAuthorityRetractedForInstitutionEvent $event
143
     * @return void
144
     */
145
    public function applyRegistrationAuthorityRetractedForInstitutionEvent(RegistrationAuthorityRetractedForInstitutionEvent $event)
146
    {
147
        $this->addCandidateToProjection(
148
            $event->identityInstitution,
149
            $event->identityId,
150
            $event->nameId,
151
            $event->commonName,
152
            $event->email
153
        );
154
    }
155
156
    protected function applyIdentityForgottenEvent(IdentityForgottenEvent $event)
157
    {
158
        $this->raCandidateRepository->removeByIdentityId($event->identityId);
159
    }
160
161
    protected function applySelectRaaOptionChangedEvent(SelectRaaOptionChangedEvent $event)
162
    {
163
        $authorizedInstitutions = $event->selectRaaOption->getInstitutions($event->institution);
164
        $this->updateInstitutionCandidatesFromCollection(new Institution($event->institution->getInstitution()), $authorizedInstitutions);
165
    }
166
167
    protected function applyInstitutionConfigurationRemovedEvent(InstitutionConfigurationRemovedEvent $event)
168
    {
169
        $this->raCandidateRepository->removeByRaInstitution(new Institution($event->institution->getInstitution()));
170
    }
171
172
    /**
173
     * This method is kept to be backwards compatible for changes before FGA
174
     *
175
     * @param IdentityAccreditedAsRaEvent $event
176
     * @return void
177
     */
178
    public function applyIdentityAccreditedAsRaEvent(IdentityAccreditedAsRaEvent $event)
179
    {
180
        $this->raCandidateRepository->removeByIdentityIdAndRaInstitution($event->identityId, $event->identityInstitution);
181
    }
182
183
    /**
184
     * This method is kept to be backwards compatible for changes before FGA
185
     *
186
     * @param IdentityAccreditedAsRaaEvent $event
187
     * @return void
188
     */
189
    public function applyIdentityAccreditedAsRaaEvent(IdentityAccreditedAsRaaEvent $event)
190
    {
191
        $this->raCandidateRepository->removeByIdentityIdAndRaInstitution($event->identityId, $event->identityInstitution);
192
    }
193
194
    /**
195
     * This method is kept to be backwards compatible for changes before FGA
196
     *
197
     * @param RegistrationAuthorityRetractedEvent $event
198
     * @return void
199
     */
200
    public function applyRegistrationAuthorityRetractedEvent(RegistrationAuthorityRetractedEvent $event)
201
    {
202
        $candidate = RaCandidate::nominate(
203
            $event->identityId,
204
            $event->identityInstitution,
205
            $event->nameId,
206
            $event->commonName,
207
            $event->email,
208
            $event->identityInstitution
209
        );
210
211
        $this->raCandidateRepository->merge($candidate);
212
    }
213
214
    /**
215
     * @param Institution $institution
216
     * @param ConfigurationInstitution[] $authorizedInstitutions
217
     * @throws \Doctrine\ORM\NonUniqueResultException
218
     */
219
    private function updateInstitutionCandidatesFromCollection(Institution $institution, array $authorizedInstitutions)
220
    {
221
        // convert configuration to value institutions
222
        $raInstitutions = new InstitutionCollection();
223
        foreach ($authorizedInstitutions as $authorizedInstitution) {
224
            $raInstitutions->add(new Institution($authorizedInstitution->getInstitution()));
225
        }
226
227
        // Remove candidates from removed institutions
228
        $this->raCandidateRepository->removeInstitutionsNotInList($institution, $raInstitutions);
229
230
        // loop through authorized institutions
231
        foreach ($raInstitutions as $raInstitution) {
232
            // add new identities
233
            $raSecondFactors = $this->raSecondFactorRepository->findByInstitution($raInstitution->getIstitution());
234
            foreach ($raSecondFactors as $raSecondFactor) {
235
                $identity = $this->identityRepository->find($raSecondFactor->identityId);
236
                $identityId = new IdentityId($identity->id);
237
238
                // check if persistent in ra listing
239
                if ($this->raListingRepository->findByIdentityIdAndRaInstitution($identityId, $institution)) {
240
                    continue;
241
                }
242
243
                // create candidate if not exists
244
                $candidate = $this->raCandidateRepository->findByIdentityIdAndRaInstitution($identityId, $institution);
245
                if (!$candidate) {
246
                    $candidate = RaCandidate::nominate(
247
                        $identityId,
248
                        $identity->institution,
249
                        $identity->nameId,
250
                        $identity->commonName,
251
                        $identity->email,
252
                        $institution
253
                    );
254
                }
255
256
                // store
257
                $this->raCandidateRepository->merge($candidate);
258
            }
259
        }
260
    }
261
262
    /**
263
     * @param Institution $identityInstitution
264
     * @param IdentityId $identityId
265
     * @param NameId $identityNameId
266
     * @param CommonName $identityCommonName
267
     * @param Email $identityEmail
268
     * @throws \Doctrine\ORM\NonUniqueResultException
269
     */
270
    private function addCandidateToProjection(
271
        Institution $identityInstitution,
272
        IdentityId $identityId,
273
        NameId $identityNameId,
274
        CommonName $identityCommonName,
275
        Email $identityEmail
276
    ) {
277
        $institutionAuthorizations = $this->institutionAuthorizationRepository
278
            ->findAuthorizationOptionsForInstitution(new ConfigurationInstitution($identityInstitution->getInstitution()));
279
280
        $institutions = [];
281
        foreach ($institutionAuthorizations as $authorization) {
282
            $raInstitutionName = $authorization->institutionRelation->getInstitution();
283
            $institutions[$raInstitutionName] = new Institution($raInstitutionName);
284
        }
285
286
        foreach ($institutions as $institution) {
287
            if ($this->raListingRepository->findByIdentityIdAndInstitution($identityId, $institution)) {
288
                continue;
289
            }
290
291
            // create candidate if not exists
292
            $candidate = $this->raCandidateRepository->findByIdentityIdAndRaInstitution($identityId, $institution);
293
            if (!$candidate) {
294
                $candidate = RaCandidate::nominate(
295
                    $identityId,
296
                    $identityInstitution,
297
                    $identityNameId,
298
                    $identityCommonName,
299
                    $identityEmail,
300
                    $institution
301
                );
302
            }
303
304
            $this->raCandidateRepository->merge($candidate);
305
        }
306
    }
307
}
308