Completed
Push — feature/fine-grained-authoriza... ( 47d334...f68bce )
by Michiel
16s
created

SamlToken::changeInstitutionScope()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.2408
c 0
b 0
f 0
cc 5
nc 3
nop 2
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 $schacHomeOrganization;
58
59
60
    public function __construct(
61
        Loa $loa,
62
        array $roles = [],
63
        InstitutionConfigurationOptions $institutionConfigurationOptions = null,
64
        $schacHomeOrganization = ''
65
    ) {
66
        parent::__construct($roles);
67
68
        $this->loa = $loa;
69
        $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...
70
        $this->institutionConfigurationOptions = $institutionConfigurationOptions;
71
        $this->schacHomeOrganization = $schacHomeOrganization;
72
    }
73
74
    /**
75
     * @return InstitutionConfigurationOptions
76
     */
77
    public function getInstitutionConfigurationOptions()
78
    {
79
        return $this->institutionConfigurationOptions;
80
    }
81
82
    /**
83
     * @param string $institution
84
     * @param InstitutionConfigurationOptions $institutionConfigurationOptions
85
     */
86
    public function changeInstitutionScope(
87
        $institution,
88
        InstitutionConfigurationOptions $institutionConfigurationOptions
89
    ) {
90
        if ($this->getUser() === null) {
91
            throw new LogicException('Cannot change institution scope: token does not contain a user');
92
        }
93
94
        $roles = array_map(function (RoleInterface $role) {
95
            return $role->getRole();
96
        }, $this->getRoles());
97
98
        if (!in_array('ROLE_SRAA', $roles) && !in_array('ROLE_RAA', $roles) && !in_array('ROLE_RA', $roles)) {
99
            throw new RuntimeException(sprintf(
100
                'Unauthorized to change institution scope to "%s": role (S)RA(A) required, found roles "%s"',
101
                $institution,
102
                implode(', ', $roles)
103
            ));
104
        }
105
106
        $this->getUser()->institution = $institution;
107
        $this->institutionConfigurationOptions = $institutionConfigurationOptions;
108
    }
109
110
    /**
111
     * Returns the user credentials.
112
     *
113
     * @return mixed The user credentials
114
     */
115
    public function getCredentials()
116
    {
117
        return '';
118
    }
119
120
    /**
121
     * @return Loa
122
     */
123
    public function getLoa()
124
    {
125
        return $this->loa;
126
    }
127
128
    public function serialize()
129
    {
130
        return serialize(
131
            [
132
                parent::serialize(),
133
                $this->loa,
134
                $this->institutionConfigurationOptions,
135
                $this->raManagementInstitution,
136
                $this->schacHomeOrganization,
137
            ]
138
        );
139
    }
140
141
    public function unserialize($serialized)
142
    {
143
        list(
144
            $parent,
145
            $this->loa,
146
            $this->institutionConfigurationOptions,
147
            $this->raManagementInstitution,
148
            $this->schacHomeOrganization
149
            ) = unserialize(
150
                $serialized
151
            );
152
153
        parent::unserialize($parent);
154
    }
155
156
    /**
157
     * @return string
158
     */
159
    public function getRaManagementInstitution()
160
    {
161
        if (!$this->raManagementInstitution) {
162
            return $this->getUser()->institution;
163
        }
164
        return $this->raManagementInstitution;
165
    }
166
167
    /**
168
     * @return string
169
     */
170
    public function getSchacHomeInstitution()
171
    {
172
        return $this->schacHomeOrganization;
173
    }
174
175
    /**
176
     * @return string
177
     */
178
    public function getIdentityOriginalInstitution()
179
    {
180
        if (!$this->isUserSraa()) {
181
            return $this->getSchacHomeInstitution();
182
        }
183
184
        return $this->getUser()->institution;
185
    }
186
187
    /**
188
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
189
     */
190
    private function isUserSraa()
191
    {
192
        /**
193
         * @var SamlToken $token
194
         */
195
        foreach ($this->getRoles() as $role) {
196
            if ($role->getRole() == 'ROLE_SRAA') {
197
                return true;
198
            }
199
        }
200
201
        return false;
202
    }
203
}
204