Completed
Push — master ( 62b783...8939c8 )
by A.
04:49 queued 01:58
created

SamlToken::changeInstitutionScope()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 3
eloc 15
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
    public function __construct(
46
        Loa $loa,
47
        array $roles = [],
48
        InstitutionConfigurationOptions $institutionConfigurationOptions = null
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $institutionConfigurationOptions exceeds the maximum configured length of 30.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
49
    ) {
50
        parent::__construct($roles);
51
52
        $this->loa = $loa;
53
        $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...
54
        $this->institutionConfigurationOptions = $institutionConfigurationOptions;
55
    }
56
57
    /**
58
     * @return InstitutionConfigurationOptions
59
     */
60
    public function getInstitutionConfigurationOptions()
61
    {
62
        return $this->institutionConfigurationOptions;
63
    }
64
65
    /**
66
     * @param string $institution
67
     * @param InstitutionConfigurationOptions $institutionConfigurationOptions
68
     */
69
    public function changeInstitutionScope(
70
        $institution,
71
        InstitutionConfigurationOptions $institutionConfigurationOptions
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $institutionConfigurationOptions exceeds the maximum configured length of 30.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
72
    ) {
73
        if ($this->getUser() === null) {
74
            throw new LogicException('Cannot change institution scope: token does not contain a user');
75
        }
76
77
        $roles = array_map(function (RoleInterface $role) {
78
            return $role->getRole();
79
        }, $this->getRoles());
80
81
        if (!in_array('ROLE_SRAA', $roles)) {
82
            throw new RuntimeException(sprintf(
83
                'Unauthorized to change institution scope to "%s": role SRAA required, found roles "%s"',
84
                $institution,
85
                implode(', ', $roles)
86
            ));
87
        }
88
89
        $this->getUser()->institution = $institution;
90
        $this->institutionConfigurationOptions = $institutionConfigurationOptions;
91
    }
92
93
    /**
94
     * Returns the user credentials.
95
     *
96
     * @return mixed The user credentials
97
     */
98
    public function getCredentials()
99
    {
100
        return '';
101
    }
102
103
    /**
104
     * @return Loa
105
     */
106
    public function getLoa()
107
    {
108
        return $this->loa;
109
    }
110
111
    public function serialize()
112
    {
113
        return serialize([parent::serialize(), $this->loa, $this->institutionConfigurationOptions]);
114
    }
115
116
    public function unserialize($serialized)
117
    {
118
        list($parent, $this->loa, $this->institutionConfigurationOptions) = unserialize($serialized);
119
120
        parent::unserialize($parent);
121
    }
122
}
123