Completed
Push — develop ( 784583...9a84aa )
by Michiel
02:05 queued 10s
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) && !in_array('ROLE_RA', $roles)) {
96
            throw new RuntimeException(sprintf(
97
                'Unauthorized to change institution scope to "%s": role (S)RA(A) 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
     * Returns the user credentials.
109
     *
110
     * @return mixed The user credentials
111
     */
112
    public function getCredentials()
113
    {
114
        return '';
115
    }
116
117
    /**
118
     * @return Loa
119
     */
120
    public function getLoa()
121
    {
122
        return $this->loa;
123
    }
124
125
    public function serialize()
126
    {
127
        return serialize(
128
            [
129
                parent::serialize(),
130
                $this->loa,
131
                $this->institutionConfigurationOptions,
132
                $this->raManagementInstitution,
133
                $this->identityInstitution,
134
            ]
135
        );
136
    }
137
138
    public function unserialize($serialized)
139
    {
140
        list($parent, $this->loa, $this->institutionConfigurationOptions, $this->raManagementInstitution, $this->identityInstitution) = unserialize(
141
            $serialized
142
        );
143
144
        parent::unserialize($parent);
145
    }
146
147
    /**
148
     * @return string
149
     */
150
    public function getIdentityInstitution()
151
    {
152
        // If the identityInstitution is not yet set, fill it with the institution of the identity.
153
        if (!$this->identityInstitution) {
154
            $this->identityInstitution = $this->getUser()->institution;
155
        }
156
        return $this->identityInstitution;
157
    }
158
159
160
    /**
161
     * @return string
162
     */
163
    public function getRaManagementInstitution()
164
    {
165
        if (!$this->raManagementInstitution) {
166
            return $this->getUser()->institution;
167
        }
168
        return $this->raManagementInstitution;
169
    }
170
}
171