Completed
Push — develop ( f4ab9c...0f4f19 )
by
unknown
27s queued 10s
created

Controller/RegistrationController.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 DateInterval;
22
use Mpdf\Mpdf;
23
use Mpdf\Output\Destination as MpdfDestination;
24
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
25
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SecondFactorService;
26
use Surfnet\StepupSelfService\SelfServiceBundle\Value\AvailableTokenCollection;
27
use Symfony\Component\HttpFoundation\Request;
28
use Symfony\Component\HttpFoundation\Response;
29
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
30
31
class RegistrationController extends Controller
32
{
33
    /**
34
     * @Template
35
     */
36
    public function displaySecondFactorTypesAction()
37
    {
38
        $institutionConfigurationOptions = $this->get('self_service.service.institution_configuration_options')
39
            ->getInstitutionConfigurationOptionsFor($this->getIdentity()->institution);
40
41
        $identity = $this->getIdentity();
42
43
        /** @var SecondFactorService $service */
44
        $service = $this->get('surfnet_stepup_self_service_self_service.service.second_factor');
45
46
        // Get all available second factors from the config.
47
        $allSecondFactors = $this->getParameter('ss.enabled_second_factors');
48
49
        $secondFactors = $service->getSecondFactorsForIdentity(
50
            $identity,
51
            $allSecondFactors,
52
            $institutionConfigurationOptions->allowedSecondFactors,
53
            $institutionConfigurationOptions->numberOfTokensPerIdentity
54
        );
55
56
        if ($secondFactors->getRegistrationsLeft() <= 0) {
57
            $this->get('logger')->notice(
58
                'User tried to register a new token but maximum number of tokens is reached. Redirecting to overview'
59
            );
60
            return $this->forward('SurfnetStepupSelfServiceSelfServiceBundle:SecondFactor:list');
61
        }
62
63
64
        $availableGsspSecondFactors = [];
65
        foreach ($secondFactors->available as $index => $secondFactor) {
66
            if ($this->has("gssp.view_config.{$secondFactor}")) {
67
                /** @var ViewConfig $secondFactorConfig */
68
                $secondFactorConfig = $this->get("gssp.view_config.{$secondFactor}");
69
                $availableGsspSecondFactors[$index] = $secondFactorConfig;
70
                // Remove the gssp second factors from the regular second factors.
71
                unset($secondFactors->available[$index]);
72
            }
73
        }
74
75
        $availableTokens = AvailableTokenCollection::from($secondFactors->available, $availableGsspSecondFactors);
76
77
        return [
78
            'commonName' => $this->getIdentity()->commonName,
79
            'availableSecondFactors' => $availableTokens,
80
            'verifyEmail' => $this->emailVerificationIsRequired(),
81
        ];
82
    }
83
84
    /**
85
     * @Template
86
     */
87
    public function emailVerificationEmailSentAction()
88
    {
89
        return ['email' => $this->getIdentity()->email];
90
    }
91
92
    /**
93
     * @Template
94
     *
95
     * @param Request $request
96
     * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
97
     */
98
    public function verifyEmailAction(Request $request)
99
    {
100
        $nonce = $request->query->get('n', '');
101
        $identityId = $this->getIdentity()->id;
102
103
        /** @var SecondFactorService $service */
104
        $service = $this->get('surfnet_stepup_self_service_self_service.service.second_factor');
105
106
        $secondFactor = $service->findUnverifiedByVerificationNonce($identityId, $nonce);
107
108
        if ($secondFactor === null) {
109
            throw new NotFoundHttpException('No second factor can be verified using this URL.');
110
        }
111
112
        if ($service->verifyEmail($identityId, $nonce)) {
113
            return $this->redirectToRoute(
114
                'ss_registration_registration_email_sent',
115
                ['secondFactorId' => $secondFactor->id]
116
            );
117
        }
118
119
        return [];
120
    }
121
122
    /**
123
     * @param $secondFactorId
124
     * @return Response
125
     */
126
    public function registrationEmailSentAction($secondFactorId)
127
    {
128
        $identity = $this->getIdentity();
129
130
        /** @var \Surfnet\StepupMiddlewareClientBundle\Identity\Dto\VerifiedSecondFactor $secondFactor */
131
        $secondFactor = $this->get('surfnet_stepup_self_service_self_service.service.second_factor')
132
            ->findOneVerified($secondFactorId);
133
134
        $parameters = [
135
            'email'            => $identity->email,
136
            'secondFactorId'   => $secondFactor->id,
137
            'registrationCode' => $secondFactor->registrationCode,
138
            'expirationDate'   => $secondFactor->registrationRequestedAt->add(
139
                new DateInterval('P14D')
140
            ),
141
            'locale'           => $identity->preferredLocale,
142
            'verifyEmail'      => $this->emailVerificationIsRequired(),
143
        ];
144
145
        $raService         = $this->get('self_service.service.ra');
146
        $raLocationService = $this->get('self_service.service.ra_location');
147
148
        $institutionConfigurationOptions = $this->get('self_service.service.institution_configuration_options')
149
            ->getInstitutionConfigurationOptionsFor($identity->institution);
150
151
        if ($institutionConfigurationOptions->useRaLocations) {
152
            $parameters['raLocations'] = $raLocationService->listRaLocationsFor($identity->institution);
153
        } elseif (!$institutionConfigurationOptions->showRaaContactInformation) {
154
            $parameters['ras'] = $raService->listRasWithoutRaas($identity->institution);
155
        } else {
156
            $parameters['ras'] = $raService->listRas($identity->institution);
157
        }
158
159
        return $this->render(
160
            'SurfnetStepupSelfServiceSelfServiceBundle:Registration:registrationEmailSent.html.twig',
161
            $parameters
162
        );
163
    }
164
165
    /**
166
     * @param $secondFactorId
167
     * @return Response
0 ignored issues
show
Should the return type not be Response|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
168
     *
169
     * @SuppressWarnings(PHPMD.ExitExpression) MPDF requires bypassing Symfony, so we exit() when MPDF is done.
170
     */
171
    public function registrationPdfAction($secondFactorId)
172
    {
173
        $content = $this->registrationEmailSentAction($secondFactorId)
174
            ->getContent();
175
176
        $mpdf = new Mpdf(
177
            array(
178
                'tempDir' => sys_get_temp_dir(),
179
            )
180
        );
181
        $mpdf->WriteHTML($content);
182
        $mpdf->Output('registration-code.pdf', MpdfDestination::DOWNLOAD);
183
184
        exit;
185
    }
186
}
187