Completed
Push — feature/fine-grained-authoriza... ( 048037...d683d5 )
by Michiel
10s
created

SamlToken::getRaManagementInstitution()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
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
    public function __construct(
51
        Loa $loa,
52
        array $roles = [],
53
        InstitutionConfigurationOptions $institutionConfigurationOptions = null
54
    ) {
55
        parent::__construct($roles);
56
57
        $this->loa = $loa;
58
        $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...
59
        $this->institutionConfigurationOptions = $institutionConfigurationOptions;
60
    }
61
62
    /**
63
     * @return InstitutionConfigurationOptions
64
     */
65
    public function getInstitutionConfigurationOptions()
66
    {
67
        return $this->institutionConfigurationOptions;
68
    }
69
70
    /**
71
     * @param string $institution
72
     * @param InstitutionConfigurationOptions $institutionConfigurationOptions
73
     */
74 View Code Duplication
    public function changeInstitutionScope(
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...
75
        $institution,
76
        InstitutionConfigurationOptions $institutionConfigurationOptions
77
    ) {
78
        if ($this->getUser() === null) {
79
            throw new LogicException('Cannot change institution scope: token does not contain a user');
80
        }
81
82
        $roles = array_map(function (RoleInterface $role) {
83
            return $role->getRole();
84
        }, $this->getRoles());
85
86
        if (!in_array('ROLE_SRAA', $roles)) {
87
            throw new RuntimeException(sprintf(
88
                'Unauthorized to change institution scope to "%s": role SRAA required, found roles "%s"',
89
                $institution,
90
                implode(', ', $roles)
91
            ));
92
        }
93
94
        $this->getUser()->institution = $institution;
95
        $this->institutionConfigurationOptions = $institutionConfigurationOptions;
96
    }
97
98
    /**
99
     * @param string $institution
100
     */
101 View Code Duplication
    public function changeRaaInstitutionScope($institution)
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...
102
    {
103
        if ($this->getUser() === null) {
104
            throw new LogicException('Cannot change RAA institution scope: token does not contain a user');
105
        }
106
107
        $roles = array_map(function (RoleInterface $role) {
108
            return $role->getRole();
109
        }, $this->getRoles());
110
111
        if (!in_array('ROLE_RAA', $roles)) {
112
            throw new RuntimeException(sprintf(
113
                'Unauthorized to change institution scope to "%s": role SRAA required, found roles "%s"',
114
                $institution,
115
                implode(', ', $roles)
116
            ));
117
        }
118
119
        $this->raManagementInstitution = $institution;
120
    }
121
122
    /**
123
     * Returns the user credentials.
124
     *
125
     * @return mixed The user credentials
126
     */
127
    public function getCredentials()
128
    {
129
        return '';
130
    }
131
132
    /**
133
     * @return Loa
134
     */
135
    public function getLoa()
136
    {
137
        return $this->loa;
138
    }
139
140
    public function serialize()
141
    {
142
        return serialize([parent::serialize(), $this->loa, $this->institutionConfigurationOptions, $this->raManagementInstitution]);
143
    }
144
145
    public function unserialize($serialized)
146
    {
147
        list($parent, $this->loa, $this->institutionConfigurationOptions, $this->raManagementInstitution) = unserialize($serialized);
148
149
        parent::unserialize($parent);
150
    }
151
152
    /**
153
     * @return string
154
     */
155
    public function getRaManagementInstitution()
156
    {
157
        if (!$this->raManagementInstitution) {
158
            return $this->getUser()->institution;
159
        }
160
        return $this->raManagementInstitution;
161
    }
162
}
163