SecondFactorController::revokeAction()   B
last analyzed

Complexity

Conditions 9
Paths 14

Size

Total Lines 58
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 38
nc 14
nop 3
dl 0
loc 58
rs 7.7564
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\StepupSelfService\SelfServiceBundle\Controller;
20
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
22
use Surfnet\StepupBundle\Service\SecondFactorTypeService;
23
use Surfnet\StepupSelfService\SelfServiceBundle\Command\RevokeCommand;
24
use Surfnet\StepupSelfService\SelfServiceBundle\Form\Type\RevokeSecondFactorType;
25
use Surfnet\StepupSelfService\SelfServiceBundle\Service\AuthorizationService;
26
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SecondFactorService;
27
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SelfAssertedTokens\RecoveryTokenService;
28
use Symfony\Component\HttpFoundation\Request;
29
use Symfony\Component\HttpFoundation\Response;
30
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
31
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
32
33
class SecondFactorController extends Controller
34
{
35
    /**
36
     * @Template
37
     */
38
    public function listAction()
39
    {
40
        $identity = $this->getIdentity();
41
        $institution = $this->getIdentity()->institution;
42
        $options = $this->get('self_service.service.institution_configuration_options')
43
            ->getInstitutionConfigurationOptionsFor($institution);
44
        /** @var SecondFactorService $service */
45
        $service = $this->get('surfnet_stepup_self_service_self_service.service.second_factor');
46
        // Get all available second factors from the config.
47
        $allSecondFactors = $this->getParameter('ss.enabled_second_factors');
48
49
        $expirationHelper = $this->get('surfnet_stepup.registration_expiration_helper');
50
51
        $secondFactors = $service->getSecondFactorsForIdentity(
52
            $identity,
53
            $allSecondFactors,
54
            $options->allowedSecondFactors,
55
            $options->numberOfTokensPerIdentity
56
        );
57
58
        /** @var RecoveryTokenService $recoveryTokenService */
59
        $recoveryTokenService = $this->get(RecoveryTokenService::class);
60
        /** @var AuthorizationService $authorizationService */
61
        $authorizationService = $this->get(AuthorizationService::class);
62
        $recoveryTokensAllowed = $authorizationService->mayRegisterRecoveryTokens($identity);
63
        $selfAssertedTokenRegistration = $options->allowSelfAssertedTokens === true && $recoveryTokensAllowed;
64
        $hasRemainingTokenTypes = count($recoveryTokenService->getRemainingTokenTypes($identity)) > 0;
65
        $recoveryTokens = [];
66
        if ($selfAssertedTokenRegistration && $recoveryTokensAllowed) {
67
            $recoveryTokens = $recoveryTokenService->getRecoveryTokensForIdentity($identity);
68
        }
69
        $loaService = $this->get(SecondFactorTypeService::class);
70
71
        return [
72
            'loaService' => $loaService,
73
            'email' => $identity->email,
74
            'maxNumberOfTokens' => $secondFactors->getMaximumNumberOfRegistrations(),
75
            'registrationsLeft' => $secondFactors->getRegistrationsLeft(),
76
            'unverifiedSecondFactors' => $secondFactors->unverified,
77
            'verifiedSecondFactors' => $secondFactors->verified,
78
            'vettedSecondFactors' => $secondFactors->vetted,
79
            'availableSecondFactors' => $secondFactors->available,
80
            'expirationHelper' => $expirationHelper,
81
            'selfAssertedTokenRegistration' => $selfAssertedTokenRegistration,
82
            'recoveryTokens' => $recoveryTokens,
83
            'hasRemainingRecoveryTokens' => $hasRemainingTokenTypes,
84
        ];
85
    }
86
87
    /**
88
     * @Template
89
     * @param Request $request
90
     * @param string $state
91
     * @param string $secondFactorId
92
     * @return array|Response
93
     */
94
    public function revokeAction(Request $request, $state, $secondFactorId)
95
    {
96
        $identity = $this->getIdentity();
97
98
        /** @var SecondFactorService $service */
99
        $service = $this->get('surfnet_stepup_self_service_self_service.service.second_factor');
100
        if (!$service->identityHasSecondFactorOfStateWithId($identity->id, $state, $secondFactorId)) {
101
            $this->get('logger')->error(sprintf(
102
                'Identity "%s" tried to revoke "%s" second factor "%s", but does not own that second factor',
103
                $identity->id,
104
                $state,
105
                $secondFactorId
106
            ));
107
            throw new NotFoundHttpException();
108
        }
109
110
        switch ($state) {
111
            case 'unverified':
112
                $secondFactor = $service->findOneUnverified($secondFactorId);
113
                break;
114
            case 'verified':
115
                $secondFactor = $service->findOneVerified($secondFactorId);
116
                break;
117
            case 'vetted':
118
                $secondFactor = $service->findOneVetted($secondFactorId);
119
                break;
120
            default:
121
                throw new LogicException('There are no other types of second factor.');
0 ignored issues
show
Bug introduced by
The type Surfnet\StepupSelfServic...ntroller\LogicException was not found. Did you mean LogicException? If so, make sure to prefix the type with \.
Loading history...
122
        }
123
124
        if ($secondFactor === null) {
125
            throw new NotFoundHttpException(
126
                sprintf("No %s second factor with id '%s' exists.", $state, $secondFactorId)
127
            );
128
        }
129
130
        $command = new RevokeCommand();
131
        $command->identity = $identity;
132
        $command->secondFactor = $secondFactor;
133
134
        $form = $this->createForm(RevokeSecondFactorType::class, $command)->handleRequest($request);
135
136
        if ($form->isSubmitted() && $form->isValid()) {
137
            /** @var FlashBagInterface $flashBag */
138
            $flashBag = $this->get('session')->getFlashBag();
139
140
            if ($service->revoke($command)) {
141
                $flashBag->add('success', 'ss.second_factor.revoke.alert.revocation_successful');
142
            } else {
143
                $flashBag->add('error', 'ss.second_factor.revoke.alert.revocation_failed');
144
            }
145
146
            return $this->redirectToRoute('ss_second_factor_list');
147
        }
148
149
        return [
150
            'form'         => $form->createView(),
151
            'secondFactor' => $secondFactor,
152
        ];
153
    }
154
}
155