|
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\Common\Collections\ArrayCollection; |
|
22
|
|
|
use Doctrine\ORM\EntityManager; |
|
23
|
|
|
use Doctrine\ORM\EntityRepository; |
|
24
|
|
|
use Doctrine\ORM\Mapping; |
|
25
|
|
|
use Surfnet\Stepup\Identity\Value\IdentityId; |
|
26
|
|
|
use Surfnet\Stepup\Identity\Value\Institution; |
|
27
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Filter\InstitutionAuthorizationRepositoryFilter; |
|
28
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Exception\RuntimeException; |
|
29
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaListing; |
|
30
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\RaListingQuery; |
|
31
|
|
|
|
|
32
|
|
|
class RaListingRepository extends EntityRepository |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var InstitutionAuthorizationRepositoryFilter |
|
36
|
|
|
*/ |
|
37
|
|
|
private $authorizationRepositoryFilter; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct( |
|
40
|
|
|
EntityManager $em, |
|
41
|
|
|
Mapping\ClassMetadata $class, |
|
42
|
|
|
InstitutionAuthorizationRepositoryFilter $authorizationRepositoryFilter |
|
43
|
|
|
) { |
|
44
|
|
|
parent::__construct($em, $class); |
|
45
|
|
|
$this->authorizationRepositoryFilter = $authorizationRepositoryFilter; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param IdentityId $identityId The RA's identity id. |
|
50
|
|
|
* @return null|RaListing |
|
|
|
|
|
|
51
|
|
|
*/ |
|
52
|
|
|
public function findByIdentityId(IdentityId $identityId) |
|
53
|
|
|
{ |
|
54
|
|
|
return parent::findOneBy(['identityId' => (string) $identityId]); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param IdentityId $identityId The RA's identity id. |
|
59
|
|
|
* @param Institution $raInstitution |
|
60
|
|
|
* @return null|RaListing |
|
|
|
|
|
|
61
|
|
|
*/ |
|
62
|
|
|
public function findByIdentityIdAndInstitution(IdentityId $identityId, Institution $raInstitution) |
|
63
|
|
|
{ |
|
64
|
|
|
return parent::findOneBy([ |
|
|
|
|
|
|
65
|
|
|
'identityId' => (string) $identityId, |
|
66
|
|
|
'raInstitution' => (string) $raInstitution, |
|
67
|
|
|
]); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function save(RaListing $raListingEntry) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->getEntityManager()->persist($raListingEntry); |
|
73
|
|
|
$this->getEntityManager()->flush(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param RaListingQuery $query |
|
78
|
|
|
* @return \Doctrine\ORM\Query |
|
79
|
|
|
*/ |
|
80
|
|
|
public function createSearchQuery(RaListingQuery $query) |
|
81
|
|
|
{ |
|
82
|
|
|
$queryBuilder = $this->createQueryBuilder('r'); |
|
83
|
|
|
|
|
84
|
|
|
// Modify query to filter on authorization |
|
85
|
|
|
$this->authorizationRepositoryFilter->filter( |
|
86
|
|
|
$queryBuilder, |
|
87
|
|
|
$query->authorizationContext, |
|
88
|
|
|
['r.identityId', 'r.institution'], |
|
89
|
|
|
'r.institution', |
|
90
|
|
|
'iac' |
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
|
|
if ($query->institution) { |
|
94
|
|
|
$queryBuilder |
|
95
|
|
|
->andWhere('r.institution = :institution') |
|
96
|
|
|
->setParameter('institution', $query->institution); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if (!$query->orderBy) { |
|
100
|
|
|
return $queryBuilder->getQuery(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$orderDirection = $query->orderDirection === 'asc' ? 'ASC' : 'DESC'; |
|
104
|
|
|
|
|
105
|
|
|
switch ($query->orderBy) { |
|
106
|
|
|
case 'commonName': |
|
107
|
|
|
$queryBuilder->orderBy('r.commonName', $orderDirection); |
|
108
|
|
|
break; |
|
109
|
|
|
default: |
|
110
|
|
|
throw new RuntimeException(sprintf('Unknown order by column "%s"', $query->orderBy)); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $queryBuilder->getQuery(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param Institution $raInstitution |
|
118
|
|
|
* @return ArrayCollection |
|
119
|
|
|
*/ |
|
120
|
|
|
public function listRasFor(Institution $raInstitution) |
|
121
|
|
|
{ |
|
122
|
|
|
$listings = $this->createQueryBuilder('rl') |
|
123
|
|
|
->where('rl.raInstitution = :institution') |
|
124
|
|
|
->setParameter('institution', $raInstitution) |
|
125
|
|
|
->getQuery() |
|
126
|
|
|
->getResult(); |
|
127
|
|
|
|
|
128
|
|
|
return new ArrayCollection($listings); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param IdentityId $identityId |
|
133
|
|
|
* @return void |
|
134
|
|
|
*/ |
|
135
|
|
View Code Duplication |
public function removeByIdentityId(IdentityId $identityId) |
|
|
|
|
|
|
136
|
|
|
{ |
|
137
|
|
|
$this->getEntityManager()->createQueryBuilder() |
|
138
|
|
|
->delete($this->_entityName, 'ral') |
|
139
|
|
|
->where('ral.identityId = :identityId') |
|
140
|
|
|
->andWhere('ral.raInstitution = :institution') |
|
141
|
|
|
->setParameter('identityId', $identityId->getIdentityId()) |
|
142
|
|
|
->getQuery() |
|
143
|
|
|
->execute(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param IdentityId $identityId |
|
148
|
|
|
* @param Institution $raInstitution |
|
149
|
|
|
* @return void |
|
150
|
|
|
*/ |
|
151
|
|
|
public function removeByIdentityIdAndInstitution(IdentityId $identityId, Institution $raInstitution) |
|
152
|
|
|
{ |
|
153
|
|
|
$this->getEntityManager()->createQueryBuilder() |
|
154
|
|
|
->delete($this->_entityName, 'ral') |
|
155
|
|
|
->where('ral.identityId = :identityId') |
|
156
|
|
|
->andWhere('ral.raInstitution = :institution') |
|
157
|
|
|
->setParameter('identityId', $identityId->getIdentityId()) |
|
158
|
|
|
->setParameter('institution', $raInstitution) |
|
159
|
|
|
->getQuery() |
|
160
|
|
|
->execute(); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.