Passed
Pull Request — main (#308)
by Michiel
16:18 queued 10:22
created

assertSecondFactorEnabled()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * Copyright 2023 SURFnet bv
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
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...
20
21
22
namespace Surfnet\StepupSelfService\SelfServiceBundle\Service;
23
24
use Psr\Log\LoggerInterface;
25
use Surfnet\StepupMiddlewareClientBundle\Configuration\Dto\InstitutionConfigurationOptions;
26
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
27
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
28
use Symfony\Bundle\SecurityBundle\Security;
29
30
/**
31
 * This class is a generic checker service to replace the two methods
32
 * before in the Controller class.
33
 * This makes the functions injectable instead of extendable.
34
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
35
36
class ControllerCheckerService
37
{
38
    final public const DEFAULT_VERIFY_EMAIL_OPTION = true;
39
40
    public function __construct(
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
41
        private readonly LoggerInterface $logger,
42
        private readonly InstitutionConfigurationOptionsService $configurationOptionsService,
43
        private readonly ParameterBagInterface $parameters,
44
        private readonly Security $security
45
    ) {
46
    }
47
48
    public function assertSecondFactorEnabled(string $type): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function assertSecondFactorEnabled()
Loading history...
49
    {
50
        $enabledSecondFactors = $this->parameters->get('ss.enabled_second_factors');
51
52
        if (!in_array($type, $enabledSecondFactors)) {
53
            $this->logger->warning('A controller action was called for a disabled second factor');
54
55
            throw new NotFoundHttpException();
56
        }
57
    }
58
59
    public function emailVerificationIsRequired(): bool
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function emailVerificationIsRequired()
Loading history...
60
    {
61
        $config = $this->configurationOptionsService
62
            ->getInstitutionConfigurationOptionsFor(
63
                $this->security->getUser()->getIdentity()->institution
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

63
                $this->security->getUser()->/** @scrutinizer ignore-call */ getIdentity()->institution
Loading history...
64
            );
65
66
        if (!$config instanceof InstitutionConfigurationOptions) {
67
            return self::DEFAULT_VERIFY_EMAIL_OPTION;
68
        }
69
70
        return $config->verifyEmail;
71
    }
72
}
73