Completed
Pull Request — develop (#120)
by Nic
05:32 queued 02:41
created

Controller/SecondFactorController.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\StepupSelfService\SelfServiceBundle\Command\RevokeCommand;
23
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SecondFactorService;
24
use Symfony\Component\HttpFoundation\Request;
25
use Symfony\Component\HttpFoundation\Response;
26
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
27
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
28
29
class SecondFactorController extends Controller
30
{
31
    /**
32
     * @Template
33
     */
34
    public function listAction()
35
    {
36
        $identity = $this->getIdentity();
37
38
        /** @var SecondFactorService $service */
39
        $service = $this->get('surfnet_stepup_self_service_self_service.service.second_factor');
40
41
        return [
42
            'email' => $identity->email,
43
            'unverifiedSecondFactors' => $service->findUnverifiedByIdentity($identity->id),
44
            'verifiedSecondFactors' => $service->findVerifiedByIdentity($identity->id),
45
            'vettedSecondFactors' => $service->findVettedByIdentity($identity->id),
46
        ];
47
    }
48
49
    /**
50
     * @Template
51
     * @param Request $request
52
     * @param string $state
53
     * @param string $secondFactorId
54
     * @return array|Response
55
     */
56
    public function revokeAction(Request $request, $state, $secondFactorId)
57
    {
58
        $identity = $this->getIdentity();
59
60
        /** @var SecondFactorService $service */
61
        $service = $this->get('surfnet_stepup_self_service_self_service.service.second_factor');
62 View Code Duplication
        if (!$service->identityHasSecondFactorOfStateWithId($identity->id, $state, $secondFactorId)) {
0 ignored issues
show
This code seems to be duplicated across 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...
63
            $this->get('logger')->error(sprintf(
64
                'Identity "%s" tried to revoke "%s" second factor "%s", but does not own that second factor',
65
                $identity->id,
66
                $state,
67
                $secondFactorId
68
            ));
69
            throw new NotFoundHttpException();
70
        }
71
72
        switch ($state) {
73
            case 'unverified':
74
                $secondFactor = $service->findOneUnverified($secondFactorId);
75
                break;
76
            case 'verified':
77
                $secondFactor = $service->findOneVerified($secondFactorId);
78
                break;
79
            case 'vetted':
80
                $secondFactor = $service->findOneVetted($secondFactorId);
81
                break;
82
            default:
83
                throw new LogicException('There are no other types of second factor.');
84
        }
85
86
        if ($secondFactor === null) {
87
            throw new NotFoundHttpException(
88
                sprintf("No %s second factor with id '%s' exists.", $state, $secondFactorId)
89
            );
90
        }
91
92
        $command = new RevokeCommand();
93
        $command->identity = $identity;
94
        $command->secondFactor = $secondFactor;
95
96
        $form = $this->createForm('ss_revoke_second_factor', $command)->handleRequest($request);
97
98
        if ($form->isValid()) {
99
            /** @var FlashBagInterface $flashBag */
100
            $flashBag = $this->get('session')->getFlashBag();
101
102
            if ($service->revoke($command)) {
103
                $flashBag->add('success', 'ss.second_factor.revoke.alert.revocation_successful');
104
            } else {
105
                $flashBag->add('error', 'ss.second_factor.revoke.alert.revocation_failed');
106
            }
107
108
            return $this->redirectToRoute('ss_second_factor_list');
109
        }
110
111
        return [
112
            'form'         => $form->createView(),
113
            'secondFactor' => $secondFactor,
114
        ];
115
    }
116
}
117