Completed
Pull Request — feature/fine-grained-authoriza... (#177)
by Michiel
49:36 queued 44:44
created

SamlToken::getIdentityInstitution()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 0
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\StepupRa\RaBundle\Security\Authentication\Token;
20
21
use Surfnet\StepupBundle\Value\Loa;
22
use Surfnet\StepupMiddlewareClientBundle\Configuration\Dto\InstitutionConfigurationOptions;
23
use Surfnet\StepupRa\RaBundle\Exception\LogicException;
24
use Surfnet\StepupRa\RaBundle\Exception\RuntimeException;
25
use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
26
use Symfony\Component\Security\Core\Role\RoleInterface;
27
28
class SamlToken extends AbstractToken
29
{
30
    /**
31
     * @var \SAML2\Assertion
32
     */
33
    public $assertion;
34
35
    /**
36
     * @var \Surfnet\StepupBundle\Value\Loa
37
     */
38
    private $loa;
39
40
    /**
41
     * @var InstitutionConfigurationOptions
42
     */
43
    private $institutionConfigurationOptions;
44
45
    /**
46
     * @var string
47
     */
48
    private $raManagementInstitution;
49
50
    /**
51
     * The identity institution is set with the SHO of the identity. This value is not overridden like the user
52
     * institution can be. This value can be used to get the identities institution regardless of the scope it
53
     * is performing RAA tasks for at this moment.
54
     *
55
     * @var string
56
     */
57
    private $identityInstitution;
58
59
    public function __construct(
60
        Loa $loa,
61
        array $roles = [],
62
        InstitutionConfigurationOptions $institutionConfigurationOptions = null
63
    ) {
64
        parent::__construct($roles);
65
66
        $this->loa = $loa;
67
        $this->setAuthenticated(count($roles));
0 ignored issues
show
Documentation introduced by
count($roles) is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
68
        $this->institutionConfigurationOptions = $institutionConfigurationOptions;
69
    }
70
71
    /**
72
     * @return InstitutionConfigurationOptions
73
     */
74
    public function getInstitutionConfigurationOptions()
75
    {
76
        return $this->institutionConfigurationOptions;
77
    }
78
79
    /**
80
     * @param string $institution
81
     * @param InstitutionConfigurationOptions $institutionConfigurationOptions
82
     */
83
    public function changeInstitutionScope(
84
        $institution,
85
        InstitutionConfigurationOptions $institutionConfigurationOptions
86
    ) {
87
        if ($this->getUser() === null) {
88
            throw new LogicException('Cannot change institution scope: token does not contain a user');
89
        }
90
91
        $roles = array_map(function (RoleInterface $role) {
92
            return $role->getRole();
93
        }, $this->getRoles());
94
95
        if (!in_array('ROLE_SRAA', $roles) && !in_array('ROLE_RAA', $roles)) {
96
            throw new RuntimeException(sprintf(
97
                'Unauthorized to change institution scope to "%s": role (S)RAA required, found roles "%s"',
98
                $institution,
99
                implode(', ', $roles)
100
            ));
101
        }
102
103
        $this->getUser()->institution = $institution;
104
        $this->institutionConfigurationOptions = $institutionConfigurationOptions;
105
    }
106
107
    /**
108
     * @param string $institution
109
     */
110
    public function changeRaaInstitutionScope($institution)
111
    {
112
        if ($this->getUser() === null) {
113
            throw new LogicException('Cannot change RAA institution scope: token does not contain a user');
114
        }
115
116
        $roles = array_map(function (RoleInterface $role) {
117
            return $role->getRole();
118
        }, $this->getRoles());
119
120
        if (!in_array('ROLE_RAA', $roles)) {
121
            throw new RuntimeException(sprintf(
122
                'Unauthorized to change institution scope to "%s": role SRAA required, found roles "%s"',
123
                $institution,
124
                implode(', ', $roles)
125
            ));
126
        }
127
128
        $this->raManagementInstitution = $institution;
129
    }
130
131
    /**
132
     * Returns the user credentials.
133
     *
134
     * @return mixed The user credentials
135
     */
136
    public function getCredentials()
137
    {
138
        return '';
139
    }
140
141
    /**
142
     * @return Loa
143
     */
144
    public function getLoa()
145
    {
146
        return $this->loa;
147
    }
148
149
    public function serialize()
150
    {
151
        return serialize(
152
            [
153
                parent::serialize(),
154
                $this->loa,
155
                $this->institutionConfigurationOptions,
156
                $this->raManagementInstitution,
157
                $this->identityInstitution,
158
            ]
159
        );
160
    }
161
162
    public function unserialize($serialized)
163
    {
164
        list($parent, $this->loa, $this->institutionConfigurationOptions, $this->raManagementInstitution, $this->identityInstitution) = unserialize(
165
            $serialized
166
        );
167
168
        parent::unserialize($parent);
169
    }
170
171
    /**
172
     * @return string
173
     */
174
    public function getIdentityInstitution()
175
    {
176
        // If the identityInstitution is not yet set, fill it with the institution of the identity.
177
        if (!$this->identityInstitution) {
178
            $this->identityInstitution = $this->getUser()->institution;
179
        }
180
        return $this->identityInstitution;
181
    }
182
183
184
    /**
185
     * @return string
186
     */
187
    public function getRaManagementInstitution()
188
    {
189
        if (!$this->raManagementInstitution) {
190
            return $this->getUser()->institution;
191
        }
192
        return $this->raManagementInstitution;
193
    }
194
}
195