UnverifiedSecondFactorController::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 11
rs 10
c 0
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
 */
18
19
namespace Surfnet\StepupMiddleware\ApiBundle\Controller;
20
21
use Surfnet\Stepup\Identity\Value\IdentityId;
22
use Surfnet\Stepup\Identity\Value\SecondFactorId;
23
use Surfnet\StepupMiddleware\ApiBundle\Controller\AbstractController;
24
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\UnverifiedSecondFactor;
25
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\UnverifiedSecondFactorQuery;
26
use Surfnet\StepupMiddleware\ApiBundle\Identity\Service\SecondFactorService;
27
use Surfnet\StepupMiddleware\ApiBundle\Response\JsonCollectionResponse;
28
use Symfony\Component\HttpFoundation\JsonResponse;
29
use Symfony\Component\HttpFoundation\Request;
30
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
31
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
32
33
class UnverifiedSecondFactorController extends AbstractController
34
{
35
    public function __construct(
36
        private readonly SecondFactorService $secondFactorService,
37
    ) {
38
    }
39
40
    public function get(string $id): JsonResponse
41
    {
42
        $this->denyAccessUnlessGrantedOneOff(['ROLE_RA', 'ROLE_SS', 'ROLE_READ']);
43
44
        $secondFactor = $this->secondFactorService->findUnverified(new SecondFactorId($id));
45
46
        if (!$secondFactor instanceof UnverifiedSecondFactor) {
47
            throw new NotFoundHttpException(sprintf("Unverified second factor '%s' does not exist", $id));
48
        }
49
50
        return new JsonResponse($secondFactor);
51
    }
52
53
    public function collection(Request $request): JsonCollectionResponse
54
    {
55
        $this->denyAccessUnlessGrantedOneOff(['ROLE_RA', 'ROLE_SS', 'ROLE_READ']);
56
57
        $query = new UnverifiedSecondFactorQuery();
58
59
        $identityIdString = $request->query->get('identityId');
60
        if (!is_string($identityIdString)) {
61
            throw new BadRequestHttpException((sprintf('Invalid identityId "%s"', $identityIdString)));
62
        }
63
64
        $query->identityId = new IdentityId($identityIdString);
65
66
        $query->verificationNonce = $request->query->get('verificationNonce');
67
        $query->pageNumber = $request->query->getInt('p', 1);
68
69
        $paginator = $this->secondFactorService->searchUnverifiedSecondFactors($query);
70
71
        return JsonCollectionResponse::fromPaginator($paginator);
72
    }
73
}
74