Completed
Pull Request — feature/fine-grained-authoriza... (#246)
by
unknown
40:56 queued 21:52
created

applyIdentityForgottenEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 applySelectRaaOptionChangedEvent(SelectRaaOptionChangedEvent $event)
209
    {
210
        $authorizedInstitutions = $event->selectRaaOption->getInstitutions($event->institution);
211
        $this->updateInstitutionCandidatesFromCollection(new Institution($event->institution->getInstitution()), $authorizedInstitutions);
212
    }
213
214
    protected function applyInstitutionConfigurationRemovedEvent(InstitutionConfigurationRemovedEvent $event)
215
    {
216
        $this->raCandidateRepository->removeByRaInstitution(new Institution($event->institution->getInstitution()));
217
    }
218
219
    /**
220
     * @param Institution $institution
221
     * @param ConfigurationInstitution[] $authorizedInstitutions
222
     * @throws \Doctrine\ORM\NonUniqueResultException
223
     */
224
    private function updateInstitutionCandidatesFromCollection(Institution $institution, array $authorizedInstitutions)
225
    {
226
227
        $raInstitutions = new InstitutionCollection();
228
        foreach ($authorizedInstitutions as $authorizedInstitution) {
229
            $raInstitutions->add(new Institution($authorizedInstitution->getInstitution()));
230
        }
231
232
        $this->raCandidateRepository->removeInstitutionsNotInList($institution, $raInstitutions);
233
234
        // loop through authorized institutions
235
        foreach ($raInstitutions as $raInstitution) {
236
237
            // add new identities
238
            $identities = $this->identityRepository->findByInstitution($raInstitution);
239
            foreach ($identities as $identity) {
240
                $identityId = new IdentityId($identity->id);
241
242
                // check if persistent in ra listing
243
                if ($this->raListingRepository->findByIdentityIdAndInstitution($identityId, $institution)) {
244
                    continue;
245
                }
246
247
                // create candidate if not exists
248
                $candidate = $this->raCandidateRepository->findByIdentityIdAndRaInstitution($identityId, $institution);
249
                if (!$candidate) {
250
                    $candidate = RaCandidate::nominate(
251
                        $identityId,
252
                        $identity->institution,
253
                        $identity->nameId,
254
                        $identity->commonName,
255
                        $identity->email,
256
                        $institution
257
                    );
258
                }
259
260
                // store
261
                $this->raCandidateRepository->merge($candidate);
262
            }
263
        }
264
    }
265
}
266