1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2018 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\ApiBundle\Configuration\Repository; |
20
|
|
|
|
21
|
|
|
use Doctrine\ORM\EntityManager; |
22
|
|
|
use Doctrine\ORM\EntityRepository; |
23
|
|
|
use Surfnet\Stepup\Configuration\Value\Institution; |
24
|
|
|
use Surfnet\Stepup\Configuration\Value\InstitutionAuthorizationOption; |
25
|
|
|
use Surfnet\Stepup\Configuration\Value\InstitutionRole; |
26
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\InstitutionAuthorization; |
27
|
|
|
|
28
|
|
|
class InstitutionAuthorizationRepository extends EntityRepository |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param Institution $institution |
33
|
|
|
* @param InstitutionRole $role |
34
|
|
|
* @return InstitutionAuthorization[] |
35
|
|
|
*/ |
36
|
|
|
public function findAuthorizationOptionsForInstitutionByRole(Institution $institution, InstitutionRole $role) |
37
|
|
|
{ |
38
|
|
|
return $this->createQueryBuilder('ia') |
39
|
|
|
->where('ia.institution = :institution') |
40
|
|
|
->andWhere('ia.role = :role') |
41
|
|
|
->setParameter('institution', $institution->getInstitution()) |
42
|
|
|
->setParameter('institutionRole', $role) |
43
|
|
|
->getQuery() |
44
|
|
|
->getResult(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param Institution $institution |
49
|
|
|
* @return InstitutionAuthorization[] |
50
|
|
|
*/ |
51
|
|
|
public function findAuthorizationOptionsForInstitution(Institution $institution) |
52
|
|
|
{ |
53
|
|
|
return $this->createQueryBuilder('ia') |
54
|
|
|
->where('ia.institution = :institution') |
55
|
|
|
->setParameter('institution', $institution->getInstitution()) |
56
|
|
|
->getQuery() |
57
|
|
|
->getResult(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param Institution $institution |
62
|
|
|
* @param InstitutionAuthorizationOption $institutionOption |
63
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
64
|
|
|
*/ |
65
|
|
|
public function saveInstitutionOption(Institution $institution, InstitutionAuthorizationOption $institutionOption) |
66
|
|
|
{ |
67
|
|
|
$institutionAuthorizations = []; |
68
|
|
|
|
69
|
|
|
foreach ($institutionOption->getInstitutions($institution) as $relatedInstitution) { |
70
|
|
|
$institutionAuthorizations[] = InstitutionAuthorization::create( |
71
|
|
|
$institution, |
72
|
|
|
$relatedInstitution, |
73
|
|
|
$institutionOption->getInstitutionRole() |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->save($institution, $institutionOption->getInstitutionRole(), $institutionAuthorizations); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param Institution $institution |
82
|
|
|
* @param InstitutionAuthorizationOption $institutionOption |
|
|
|
|
83
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
84
|
|
|
*/ |
85
|
|
|
public function clearInstitutionOption(Institution $institution) |
86
|
|
|
{ |
87
|
|
|
$entityManager = $this->getEntityManager(); |
88
|
|
|
|
89
|
|
|
$entityManager->createQuery( |
90
|
|
|
'DELETE '.InstitutionAuthorization::class.' ia |
91
|
|
|
WHERE ia.institution = :institution' |
92
|
|
|
) |
93
|
|
|
->setParameter('institution', $institution->getInstitution()) |
94
|
|
|
->execute(); |
95
|
|
|
|
96
|
|
|
$entityManager->flush(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param Institution $institution |
102
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
103
|
|
|
*/ |
104
|
|
|
public function setDefaultInstitutionOption(Institution $institution) |
105
|
|
|
{ |
106
|
|
|
$this->saveInstitutionOption( |
107
|
|
|
$institution, |
108
|
|
|
InstitutionAuthorizationOption::getDefault(InstitutionRole::useRa()) |
109
|
|
|
); |
110
|
|
|
$this->saveInstitutionOption( |
111
|
|
|
$institution, |
112
|
|
|
InstitutionAuthorizationOption::getDefault(InstitutionRole::useRaa()) |
113
|
|
|
); |
114
|
|
|
$this->saveInstitutionOption( |
115
|
|
|
$institution, |
116
|
|
|
InstitutionAuthorizationOption::getDefault(InstitutionRole::selectRaa()) |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param Institution $institution |
122
|
|
|
* @param InstitutionRole $role |
123
|
|
|
* @param InstitutionAuthorization[] $institutionAuthorizations |
124
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
125
|
|
|
*/ |
126
|
|
|
private function save(Institution $institution, InstitutionRole $role, array $institutionAuthorizations) |
127
|
|
|
{ |
128
|
|
|
$entityManager = $this->getEntityManager(); |
129
|
|
|
|
130
|
|
|
$this->clearOldAuthorizations($entityManager, $institution, $role); |
131
|
|
|
$this->addNewAuthorizations($entityManager, $role, $institutionAuthorizations); |
132
|
|
|
|
133
|
|
|
$entityManager->flush(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param EntityManager $entityManager |
138
|
|
|
* @param Institution $institution |
139
|
|
|
* @param InstitutionRole $role |
140
|
|
|
*/ |
141
|
|
|
private function clearOldAuthorizations(EntityManager $entityManager, Institution $institution, InstitutionRole $role) |
142
|
|
|
{ |
143
|
|
|
$entityManager->createQuery( |
144
|
|
|
'DELETE '.InstitutionAuthorization::class.' ia |
145
|
|
|
WHERE ia.institutionRole = :role AND ia.institution = :institution' |
146
|
|
|
) |
147
|
|
|
->setParameter('role', $role) |
148
|
|
|
->setParameter('institution', $institution->getInstitution()) |
149
|
|
|
->execute(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param EntityManager $entityManager |
154
|
|
|
* @param InstitutionRole $role |
155
|
|
|
* @param InstitutionAuthorization[] $institutionAuthorizations |
156
|
|
|
*/ |
157
|
|
|
private function addNewAuthorizations(EntityManager $entityManager, InstitutionRole $role, array $institutionAuthorizations) |
158
|
|
|
{ |
159
|
|
|
foreach ($institutionAuthorizations as $institutionAuthorization) { |
160
|
|
|
if ($institutionAuthorization->institutionRole === $role) { |
161
|
|
|
$entityManager->persist($institutionAuthorization); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.