Completed
Pull Request — develop (#315)
by Michiel
02:48
created

getInstitutionsForSelectRaaRole()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 9.44
c 0
b 0
f 0
cc 2
nc 2
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\Repository;
20
21
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
22
use Doctrine\Common\Persistence\ManagerRegistry;
23
use Doctrine\ORM\EntityManager;
24
use Doctrine\ORM\Query\Expr\Join;
25
use Surfnet\Stepup\Configuration\Value\InstitutionRole;
26
use Surfnet\Stepup\Identity\Collection\InstitutionCollection;
27
use Surfnet\Stepup\Identity\Value\IdentityId;
28
use Surfnet\Stepup\Identity\Value\Institution;
29
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\ConfiguredInstitution;
30
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\InstitutionAuthorization;
31
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\AuditLogEntry;
32
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\Identity;
33
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaListing;
34
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\AuthorityRole;
35
36
class AuthorizationRepository extends ServiceEntityRepository
37
{
38
    public function __construct(ManagerRegistry $registry)
39
    {
40
        parent::__construct($registry, AuditLogEntry::class);
41
    }
42
43
    /**
44
     * Return all institutions were the actor has the specified role for
45
     * The returned institutions are used to filter query results on
46
     *
47
     * @param InstitutionRole $role
48
     * @param IdentityId $actorId
49
     * @return InstitutionCollection
50
     */
51
    public function getInstitutionsForRole(InstitutionRole $role, IdentityId $actorId)
52
    {
53
        $qb = $this->_em->createQueryBuilder()
54
            ->select("a.institution")
55
            ->from(ConfiguredInstitution::class, 'i')
56
            ->innerJoin(RaListing::class, 'r', Join::WITH, "i.institution = r.raInstitution")
57
            ->innerJoin(
58
                InstitutionAuthorization::class,
59
                'a',
60
                Join::WITH,
61
                "i.institution = a.institutionRelation AND a.institutionRole IN (:authorizationRoles)"
62
            )
63
            ->where("r.identityId = :identityId AND r.role IN(:roles)")
64
            ->groupBy("a.institution");
65
66
        $qb->setParameter('identityId', (string)$actorId);
67
        $qb->setParameter(
68
            'authorizationRoles',
69
            $this->getAllowedInstitutionRoles($role)
70
        );
71
        $qb->setParameter(
72
            'roles',
73
            $this->getAllowedIdentityRoles($role)
74
        );
75
76
        $institutions = $qb->getQuery()->getArrayResult();
77
78
        $result = new InstitutionCollection();
79
        foreach ($institutions as $institution) {
80
            $result->add(new Institution((string)$institution['institution']));
81
        }
82
83
        return $result;
84
    }
85
86
    /**
87
     * @param IdentityId $actorId
88
     * @return InstitutionCollection
89
     */
90
    public function getInstitutionsForSelectRaaRole(IdentityId $actorId)
91
    {
92
        $qb = $this->_em->createQueryBuilder()
93
            ->select("ci.institution")
94
            ->from(InstitutionAuthorization::class, 'ia')
95
            ->innerJoin(ConfiguredInstitution::class, 'ci', Join::WITH, 'ia.institutionRelation = ci.institution')
96
            ->innerJoin(Identity::class, 'i', Join::WITH,'ia.institution = i.institution AND i.id = :identityId')
97
            ->innerJoin(RaListing::class, 'ra', Join::WITH,'i.id = ra.identityId AND ra.role = :authorizationRole')
98
            ->where('ia.institutionRole = :institutionRole AND ra.role = :authorizationRole')
99
            ->groupBy("ci.institution");
100
101
        $qb->setParameter('identityId', (string)$actorId);
102
        $qb->setParameter(
103
            'authorizationRole',
104
            AuthorityRole::ROLE_RAA
105
        );
106
        $qb->setParameter(
107
            'institutionRole',
108
            InstitutionRole::ROLE_SELECT_RAA
109
        );
110
111
        $institutions = $qb->getQuery()->getArrayResult();
112
113
        $result = new InstitutionCollection();
114
        foreach ($institutions as $institution) {
115
            $result->add(new Institution((string)$institution['institution']));
116
        }
117
118
        return $result;
119
    }
120
121
    /**
122
     * This is the mapping to look up allowed institution roles
123
     * - if the institution role is RA we should look if the configured institution has RA role
124
     * - if the institution role is RAA we should look if the configured institution has RAA role
125
     *
126
     * @param InstitutionRole $role
127
     * @return array
128
     */
129 View Code Duplication
    private function getAllowedInstitutionRoles(InstitutionRole $role)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
    {
131
        switch (true) {
132
            case $role->equals(InstitutionRole::useRa()):
133
                return [InstitutionRole::ROLE_USE_RA];
134
            case $role->equals(InstitutionRole::useRaa()):
135
                return [InstitutionRole::ROLE_USE_RAA];
136
            default:
137
                return [];
138
        }
139
    }
140
141
    /**
142
     * This is the mapping to look up allowed identity roles for a specific institution role
143
     * - if the institution role is RA we should look if the identity has a RA or RAA role
144
     * - if the institution role is RAA we should look if the identity has a RAA role
145
     *
146
     * @param InstitutionRole $role
147
     * @return array
148
     */
149 View Code Duplication
    private function getAllowedIdentityRoles(InstitutionRole $role)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
    {
151
        switch (true) {
152
            case $role->equals(InstitutionRole::useRa()):
153
                return [AuthorityRole::ROLE_RA, AuthorityRole::ROLE_RAA];
154
            case $role->equals(InstitutionRole::useRaa()):
155
                return [AuthorityRole::ROLE_RAA];
156
            default:
157
                return [];
158
        }
159
    }
160
}
161