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\Repository; |
20
|
|
|
|
21
|
|
|
use Doctrine\ORM\EntityManager; |
22
|
|
|
use Doctrine\ORM\Query\Expr\Join; |
23
|
|
|
use Surfnet\Stepup\Configuration\Value\InstitutionRole; |
24
|
|
|
use Surfnet\Stepup\Identity\Collection\InstitutionCollection; |
25
|
|
|
use Surfnet\Stepup\Identity\Value\IdentityId; |
26
|
|
|
use Surfnet\Stepup\Identity\Value\Institution; |
27
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\InstitutionAuthorization; |
28
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaListing; |
29
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\AuthorityRole; |
30
|
|
|
|
31
|
|
|
class AuthorizationRepository |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var EntityManager |
35
|
|
|
*/ |
36
|
|
|
private $entityManager; |
37
|
|
|
|
38
|
|
|
public function __construct(EntityManager $entityManager) |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$this->entityManager = $entityManager; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param InstitutionRole $role |
45
|
|
|
* @param IdentityId $actorId |
46
|
|
|
* @return InstitutionCollection |
47
|
|
|
*/ |
48
|
|
|
public function getInstitutionsForRole(InstitutionRole $role, IdentityId $actorId) |
49
|
|
|
{ |
50
|
|
|
$qb = $this->entityManager->createQueryBuilder() |
51
|
|
|
->select("a.institution") |
52
|
|
|
->from(RaListing::class, 'i') |
53
|
|
|
->innerJoin(RaListing::class, 'r', Join::WITH, "i.institution = r.raInstitution") |
54
|
|
|
->innerJoin( |
55
|
|
|
InstitutionAuthorization::class, |
56
|
|
|
'a', |
57
|
|
|
Join::WITH, |
58
|
|
|
"i.institution = a.institutionRelation AND a.institutionRole IN (:authorizationRoles)" |
59
|
|
|
) |
60
|
|
|
->where("r.identityId = :identityId AND r.role IN(:roles)") |
61
|
|
|
->groupBy("a.institution"); |
62
|
|
|
|
63
|
|
|
$qb->setParameter('identityId', (string)$actorId); |
64
|
|
|
$qb->setParameter( |
65
|
|
|
'authorizationRoles', |
66
|
|
|
$this->getAuthorizationRolesForAuthorization($role) |
67
|
|
|
); |
68
|
|
|
$qb->setParameter( |
69
|
|
|
'roles', |
70
|
|
|
$this->getAuthorizationRolesForRa($role) |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
$institutions = $qb->getQuery()->getArrayResult(); |
74
|
|
|
|
75
|
|
|
$result = new InstitutionCollection(); |
76
|
|
|
foreach ($institutions as $institution) { |
77
|
|
|
$result->add(new Institution((string)$institution['institution'])); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $result; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param InstitutionRole $role |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
View Code Duplication |
private function getAuthorizationRolesForAuthorization(InstitutionRole $role) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
switch (true) { |
90
|
|
|
case $role->equals(InstitutionRole::useRa()): |
91
|
|
|
return [InstitutionRole::ROLE_USE_RA]; |
92
|
|
|
case $role->equals(InstitutionRole::useRaa()): |
93
|
|
|
return [InstitutionRole::ROLE_USE_RAA]; |
94
|
|
|
default: |
95
|
|
|
return []; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param InstitutionRole $role |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
private function getAuthorizationRolesForRa(InstitutionRole $role) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
switch (true) { |
106
|
|
|
case $role->equals(InstitutionRole::useRa()): |
107
|
|
|
return [AuthorityRole::ROLE_RA, AuthorityRole::ROLE_RAA]; |
108
|
|
|
case $role->equals(InstitutionRole::useRaa()): |
109
|
|
|
return [AuthorityRole::ROLE_RAA]; |
110
|
|
|
default: |
111
|
|
|
return []; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.