Completed
Push — feature/self-service-verified-... ( ae8c99...efc3c4 )
by Michiel
03:50
created

VerifiedSecondFactorController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getAction() 0 12 2
A collectionAction() 0 27 3
A collectionOfIdentityAction() 0 14 2
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 = new VerifiedSecondFactorQuery();
73
74
        if ($request->get('identityId')) {
75
            $query->identityId = new IdentityId($request->get('identityId'));
76
        }
77
78
        if ($request->get('secondFactorId')) {
79
            $query->secondFactorId = new SecondFactorId($request->get('secondFactorId'));
80
        }
81
82
        $query->registrationCode = $request->get('registrationCode');
83
        $query->pageNumber = (int) $request->get('p', 1);
84
        $query->authorizationContext = $this->institutionAuthorizationService->buildInstitutionAuthorizationContext(
85
            $actorId,
86
            new InstitutionRole(InstitutionRole::ROLE_USE_RA)
87
        );
88
89
        $paginator = $this->secondFactorService->searchVerifiedSecondFactors($query);
90
91
        return JsonCollectionResponse::fromPaginator($paginator);
92
    }
93
94
    public function collectionOfIdentityAction(Request $request)
95
    {
96
        $this->denyAccessUnlessGranted(['ROLE_SS']);
97
        $query = new VerifiedSecondFactorQuery();
98
99
        if ($request->get('identityId')) {
100
            $query->identityId = new IdentityId($request->get('identityId'));
101
        }
102
        $query->pageNumber = (int) $request->get('p', 1);
103
104
        $paginator = $this->secondFactorService->searchVerifiedSecondFactors($query);
105
106
        return JsonCollectionResponse::fromPaginator($paginator);
107
    }
108
}
109