Completed
Push — feature/self-service-verified-... ( ae8c99 )
by Michiel
13:16
created

VerifiedSecondFactorController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 11
dl 0
loc 77
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getAction() 0 12 2
A collectionAction() 0 16 1
A collectionWithoutAuthorizationContextAction() 0 8 1
A buildVerifiedSecondFactorQueryFrom() 0 17 3
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\Configuration\Value\InstitutionRole;
22
use Surfnet\Stepup\Identity\Value\IdentityId;
23
use Surfnet\Stepup\Identity\Value\SecondFactorId;
24
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Service\InstitutionAuthorizationService;
25
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\VerifiedSecondFactorQuery;
26
use Surfnet\StepupMiddleware\ApiBundle\Identity\Service\SecondFactorService;
27
use Surfnet\StepupMiddleware\ApiBundle\Response\JsonCollectionResponse;
28
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
29
use Symfony\Component\HttpFoundation\JsonResponse;
30
use Symfony\Component\HttpFoundation\Request;
31
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
32
33
class VerifiedSecondFactorController extends Controller
34
{
35
    /**
36
     * @var SecondFactorService
37
     */
38
    private $secondFactorService;
39
40
    /**
41
     * @var InstitutionAuthorizationService
42
     */
43
    private $institutionAuthorizationService;
44
45
    public function __construct(
46
        SecondFactorService $secondFactorService,
47
        InstitutionAuthorizationService $authorizationService
48
    ) {
49
        $this->secondFactorService = $secondFactorService;
50
        $this->institutionAuthorizationService = $authorizationService;
51
    }
52
53
    public function getAction($id)
54
    {
55
        $this->denyAccessUnlessGranted(['ROLE_RA', 'ROLE_SS']);
56
57
        $secondFactor = $this->secondFactorService->findVerified(new SecondFactorId($id));
58
59
        if ($secondFactor === null) {
60
            throw new NotFoundHttpException(sprintf("Verified second factor '%s' does not exist", $id));
61
        }
62
63
        return new JsonResponse($secondFactor);
64
    }
65
66
    public function collectionAction(Request $request)
67
    {
68
        $this->denyAccessUnlessGranted(['ROLE_RA']);
69
70
        $actorId = new IdentityId($request->get('actorId'));
71
72
        $query = $this->buildVerifiedSecondFactorQueryFrom($request);
73
        $query->authorizationContext = $this->institutionAuthorizationService->buildInstitutionAuthorizationContext(
74
            $actorId,
75
            new InstitutionRole(InstitutionRole::ROLE_USE_RA)
76
        );
77
78
        $paginator = $this->secondFactorService->searchVerifiedSecondFactors($query);
79
80
        return JsonCollectionResponse::fromPaginator($paginator);
81
    }
82
83
    public function collectionWithoutAuthorizationContextAction(Request $request)
84
    {
85
        $this->denyAccessUnlessGranted(['ROLE_SS']);
86
        $query = $this->buildVerifiedSecondFactorQueryFrom($request);
87
        $paginator = $this->secondFactorService->searchVerifiedSecondFactors($query);
88
89
        return JsonCollectionResponse::fromPaginator($paginator);
90
    }
91
92
    private function buildVerifiedSecondFactorQueryFrom(Request $request)
93
    {
94
        $query = new VerifiedSecondFactorQuery();
95
96
        if ($request->get('identityId')) {
97
            $query->identityId = new IdentityId($request->get('identityId'));
98
        }
99
100
        if ($request->get('secondFactorId')) {
101
            $query->secondFactorId = new SecondFactorId($request->get('secondFactorId'));
102
        }
103
104
        $query->registrationCode = $request->get('registrationCode');
105
        $query->pageNumber       = (int) $request->get('p', 1);
106
107
        return $query;
108
    }
109
}
110