Passed
Push — feature/symfony6-upgrade ( 949a66...647b90 )
by Paul
07:17
created

Controller::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 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
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
18
19
namespace Surfnet\StepupSelfService\SelfServiceBundle\Controller;
20
21
use Psr\Log\LoggerInterface;
22
use Surfnet\StepupMiddlewareClientBundle\Configuration\Dto\InstitutionConfigurationOptions;
23
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\Identity;
24
use Surfnet\StepupSelfService\SelfServiceBundle\Service\InstitutionConfigurationOptionsService;
25
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
26
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
27
use UnexpectedValueException;
28
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
29
30
class Controller extends AbstractController
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Controller
Loading history...
31
{
32
    /**
33
     * Default verify email option as defined by middleware.
34
     */
35
    final public const DEFAULT_VERIFY_EMAIL_OPTION = true;
36
37
    public function __construct(
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
38
        private readonly LoggerInterface $logger,
39
        private readonly InstitutionConfigurationOptionsService $configurationOptionsService
40
    ) {
41
    }
42
43
    protected function has(string $id): bool
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function has()
Loading history...
44
    {
45
        return $this->container->has($id);
46
    }
47
48
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
49
     * @throws AccessDeniedException When the registrant isn't registered using a SAML token.
50
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
51
    protected function getIdentity(): Identity
52
    {
53
        $authenticatedIdentity = $this->getUser();
54
        // During authentication, an AuthenticatedIdentity is created, a decorated Identity.
55
        // The app wants to work with the 'regular' Identity DTO from Middleware (client bundle)
56
        // So we extract the entity here
57
        $user = $authenticatedIdentity->getIdentity();
0 ignored issues
show
Bug introduced by
The method getIdentity() does not exist on Symfony\Component\Security\Core\User\UserInterface. It seems like you code against a sub-type of Symfony\Component\Security\Core\User\UserInterface such as Surfnet\StepupSelfServic...n\AuthenticatedIdentity. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
        /** @scrutinizer ignore-call */ 
58
        $user = $authenticatedIdentity->getIdentity();
Loading history...
58
        if (!$user instanceof Identity) {
59
            $actualType = get_debug_type($user);
60
61
            throw new UnexpectedValueException(
62
                sprintf(
63
                    "Token did not contain user of type '%s', but one of type '%s'",
64
                    Identity::class,
65
                    $actualType
66
                )
67
            );
68
        }
69
70
        return $user;
71
    }
72
73
    protected function assertSecondFactorEnabled(string $type): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function assertSecondFactorEnabled()
Loading history...
74
    {
75
        if (!in_array($type, $this->getParameter('ss.enabled_second_factors'))) {
76
            $this->logger->warning('A controller action was called for a disabled second factor');
77
78
            throw $this->createNotFoundException();
79
        }
80
    }
81
82
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
83
     * @return bool
84
     */
85
    protected function emailVerificationIsRequired(): bool
86
    {
87
        $config = $this->configurationOptionsService
88
            ->getInstitutionConfigurationOptionsFor($this->getIdentity()->institution);
89
90
        if (!$config instanceof InstitutionConfigurationOptions) {
91
            return self::DEFAULT_VERIFY_EMAIL_OPTION;
92
        }
93
94
        return $config->verifyEmail;
95
    }
96
}
97