Completed
Pull Request — feature/fine-grained-authoriza... (#246)
by
unknown
440:42 queued 435:58
created

applyUseRaOptionChangedEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 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\Configuration\Event\UseRaaOptionChangedEvent;
26
use Surfnet\Stepup\Configuration\Event\UseRaOptionChangedEvent;
27
use Surfnet\Stepup\Identity\Collection\InstitutionCollection;
28
use Surfnet\Stepup\Identity\Value\IdentityId;
29
use Surfnet\Stepup\Identity\Value\Institution;
30
use Surfnet\Stepup\Configuration\Value\Institution as ConfigurationInstitution;
31
use Surfnet\Stepup\Identity\Event\CompliedWithVettedSecondFactorRevocationEvent;
32
use Surfnet\Stepup\Identity\Event\IdentityAccreditedAsRaaEvent;
33
use Surfnet\Stepup\Identity\Event\IdentityAccreditedAsRaEvent;
34
use Surfnet\Stepup\Identity\Event\IdentityForgottenEvent;
35
use Surfnet\Stepup\Identity\Event\RegistrationAuthorityRetractedEvent;
36
use Surfnet\Stepup\Identity\Event\SecondFactorVettedEvent;
37
use Surfnet\Stepup\Identity\Event\VettedSecondFactorRevokedEvent;
38
use Surfnet\Stepup\Identity\Event\YubikeySecondFactorBootstrappedEvent;
39
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Repository\InstitutionAuthorizationRepository;
40
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaCandidate;
41
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\IdentityRepository;
42
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\RaCandidateRepository;
43
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\RaListingRepository;
44
45
/**
46
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
47
 */
48
class RaCandidateProjector extends Projector
49
{
50
    /**
51
     * @var RaCandidateRepository
52
     */
53
    private $raCandidateRepository;
54
55
    /**
56
     * @var RaListingRepository
57
     */
58
    private $raListingRepository;
59
60
    /**
61
     * @var institutionAuthorizationRepository
62
     */
63
    private $institutionAuthorizationRepository;
64
    /**
65
     * @var IdentityRepository
66
     */
67
    private $identityRepository;
68
69
    public function __construct(
70
        RaCandidateRepository $raCandidateRepository,
71
        RaListingRepository $raListingRepository,
72
        InstitutionAuthorizationRepository $institutionAuthorizationRepository,
73
        IdentityRepository $identityRepository
74
    ) {
75
        $this->raCandidateRepository = $raCandidateRepository;
76
        $this->raListingRepository = $raListingRepository;
77
        $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...
78
        $this->identityRepository = $identityRepository;
79
    }
80
81
    /**
82
     * @param SecondFactorVettedEvent $event
83
     * @return void
84
     */
85
    public function applySecondFactorVettedEvent(SecondFactorVettedEvent $event)
86
    {
87
        $institutionAuthorizations = $this->institutionAuthorizationRepository
88
            ->findAuthorizationOptionsForInstitution(new ConfigurationInstitution($event->identityInstitution));
89
90
        foreach ($institutionAuthorizations as $authorization) {
91
92
            $institution = new Institution($authorization->institution);
93
94
            if ($this->raListingRepository->findByIdentityIdAndInstitution($event->identityId, $institution)) {
95
                continue;
96
            }
97
98
            $candidate = RaCandidate::nominate(
99
                $event->identityId,
100
                $event->identityInstitution,
101
                $event->nameId,
102
                $event->commonName,
103
                $event->email,
104
                $institution
105
            );
106
107
            $this->raCandidateRepository->merge($candidate);
108
        }
109
    }
110
111
    /**
112
     * @param YubikeySecondFactorBootstrappedEvent $event
113
     * @return void
114
     */
115
    public function applyYubikeySecondFactorBootstrappedEvent(YubikeySecondFactorBootstrappedEvent $event)
116
    {
117
        $institutionAuthorizations = $this->institutionAuthorizationRepository
118
            ->findAuthorizationOptionsForInstitution(new ConfigurationInstitution($event->identityInstitution));
119
120
        foreach ($institutionAuthorizations as $authorization) {
121
122
            $institution = new Institution($authorization->institution);
123
124
            $candidate = RaCandidate::nominate(
125
                $event->identityId,
126
                $event->identityInstitution,
127
                $event->nameId,
128
                $event->commonName,
129
                $event->email,
130
                $institution
131
            );
132
133
            $this->raCandidateRepository->merge($candidate);
134
        }
135
    }
136
137
    /**
138
     * @param VettedSecondFactorRevokedEvent $event
139
     * @return void
140
     */
141
    public function applyVettedSecondFactorRevokedEvent(VettedSecondFactorRevokedEvent $event)
142
    {
143
        $this->raCandidateRepository->removeByIdentityId($event->identityId);
144
    }
145
146
    /**
147
     * @param CompliedWithVettedSecondFactorRevocationEvent $event
148
     * @return void
149
     */
150
    public function applyCompliedWithVettedSecondFactorRevocationEvent(
151
        CompliedWithVettedSecondFactorRevocationEvent $event
152
    ) {
153
        $this->raCandidateRepository->removeByIdentityId($event->identityId);
154
    }
155
156
    /**
157
     * @param SraaUpdatedEvent $event
158
     *
159
     * Removes all RaCandidates that have a nameId matching an SRAA, as they cannot be made RA(A) as they
160
     * already are SRAA.
161
     */
162
    public function applySraaUpdatedEvent(SraaUpdatedEvent $event)
163
    {
164
        $this->raCandidateRepository->removeByNameIds($event->sraaList);
165
    }
166
167
    /**
168
     * @param IdentityAccreditedAsRaEvent $event
169
     * @return void
170
     */
171
    public function applyIdentityAccreditedAsRaEvent(IdentityAccreditedAsRaEvent $event)
172
    {
173
        $this->raCandidateRepository->removeByIdentityIdAndRaInstitution($event->identityId, $event->raInstitution);
174
    }
175
176
    /**
177
     * @param IdentityAccreditedAsRaaEvent $event
178
     * @return void
179
     */
180
    public function applyIdentityAccreditedAsRaaEvent(IdentityAccreditedAsRaaEvent $event)
181
    {
182
        $this->raCandidateRepository->removeByIdentityIdAndRaInstitution($event->identityId, $event->raInstitution);
183
    }
184
185
    /**
186
     * @param RegistrationAuthorityRetractedEvent $event
187
     * @return void
188
     */
189
    public function applyRegistrationAuthorityRetractedEvent(RegistrationAuthorityRetractedEvent $event)
190
    {
191
        $candidate = RaCandidate::nominate(
192
            $event->identityId,
193
            $event->identityInstitution,
194
            $event->nameId,
195
            $event->commonName,
196
            $event->email,
197
            $event->raInstitution
198
        );
199
200
        $this->raCandidateRepository->merge($candidate);
201
    }
202
203
    protected function applyIdentityForgottenEvent(IdentityForgottenEvent $event)
204
    {
205
        $this->raCandidateRepository->removeByIdentityId($event->identityId);
206
    }
207
208
    protected function applyUseRaOptionChangedEvent(UseRaOptionChangedEvent $event)
209
    {
210
        $authorizedInstitutions = $event->useRaOption->getInstitutions($event->institution);
211
        $this->updateInstitutionCandidatesFromCollection(new Institution($event->institution->getInstitution()), $authorizedInstitutions);
212
    }
213
214
    protected function applyUseRaaOptionChangedEvent(UseRaaOptionChangedEvent $event)
215
    {
216
        $authorizedInstitutions = $event->useRaaOption->getInstitutions($event->institution);
217
        $this->updateInstitutionCandidatesFromCollection(new Institution($event->institution->getInstitution()), $authorizedInstitutions);
218
    }
219
220
    protected function applySelectRaaOptionChangedEvent(SelectRaaOptionChangedEvent $event)
221
    {
222
        $authorizedInstitutions = $event->selectRaaOption->getInstitutions($event->institution);
223
        $this->updateInstitutionCandidatesFromCollection(new Institution($event->institution->getInstitution()), $authorizedInstitutions);
224
    }
225
226
    protected function applyInstitutionConfigurationRemovedEvent(InstitutionConfigurationRemovedEvent $event)
227
    {
228
        $this->raCandidateRepository->removeByRaInstitution(new Institution($event->institution->getInstitution()));
229
    }
230
231
    /**
232
     * @param Institution $institution
233
     * @param ConfigurationInstitution[] $authorizedInstitutions
234
     * @throws \Doctrine\ORM\NonUniqueResultException
235
     */
236
    private function updateInstitutionCandidatesFromCollection(Institution $institution, array $authorizedInstitutions)
237
    {
238
239
        $raInstitutions = new InstitutionCollection();
240
        foreach ($authorizedInstitutions as $authorizedInstitution) {
241
            $raInstitutions->add(new Institution($authorizedInstitution->getInstitution()));
242
        }
243
244
        $this->raCandidateRepository->removeInstitutionsNotInList($institution, $raInstitutions);
245
246
        // loop through authorized institutions
247
        foreach ($raInstitutions as $raInstitution) {
248
249
            // add new identities
250
            $identities = $this->identityRepository->findByInstitution($raInstitution);
251
            foreach ($identities as $identity) {
252
                $identityId = new IdentityId($identity->id);
253
254
                // check if persistent in ra listing
255
                if ($this->raListingRepository->findByIdentityIdAndInstitution($identityId, $raInstitution)) {
256
                    continue;
257
                }
258
259
                // create candidate if not existing
260
                $candidate = $this->raCandidateRepository->findByIdentityId($identityId);
261
                if (!$candidate) {
262
                    $candidate = RaCandidate::nominate(
263
                        $identityId,
264
                        $identity->institution,
265
                        $identity->nameId,
266
                        $identity->commonName,
267
                        $identity->email,
268
                        $raInstitution
269
                    );
270
                }
271
272
                // store
273
                $this->raCandidateRepository->merge($candidate);
274
            }
275
        }
276
    }
277
}
278