RaCandidateController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 79
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A search() 0 35 4
A __construct() 0 4 1
A get() 0 28 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\Identity\Value\IdentityId;
22
use Surfnet\Stepup\Identity\Value\RegistrationAuthorityRole;
0 ignored issues
show
Bug introduced by
The type Surfnet\Stepup\Identity\...gistrationAuthorityRole was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Service\AuthorizationContextService;
24
use Surfnet\StepupMiddleware\ApiBundle\Controller\AbstractController;
25
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\RaCandidateQuery;
26
use Surfnet\StepupMiddleware\ApiBundle\Identity\Service\RaCandidateService;
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;
0 ignored issues
show
introduced by
Header blocks must be separated by a single blank line
Loading history...
32
use function sprintf;
33
34
class RaCandidateController extends AbstractController
35
{
36
    public function __construct(
37
        private readonly RaCandidateService $raCandidateService,
38
        private readonly AuthorizationContextService $authorizationService,
39
    ) {
40
    }
41
42
    /**
43
     * @return JsonCollectionResponse
44
     */
45
    public function search(Request $request): JsonCollectionResponse
46
    {
47
        $this->denyAccessUnlessGrantedOneOff(['ROLE_RA', 'ROLE_READ']);
48
49
        $actorIdString = $request->query->get('actorId');
50
        if (!is_string($actorIdString)) {
51
            throw new BadRequestHttpException(sprintf('Invalid actorId "%s"', $actorIdString));
52
        }
53
        $actorId = new IdentityId($actorIdString);
54
55
        $secondFactorTypes = $request->query->all('secondFactorTypes');
56
        foreach ($secondFactorTypes as $type) {
57
            if (!is_string($type)) {
58
                throw new BadRequestHttpException(sprintf('Invalid secondFactorType "%s", string expected.', $type));
59
            }
60
        }
61
        /** @var array<string> $secondFactorTypes */
62
63
        $query = new RaCandidateQuery();
64
        $query->institution = $request->query->get('institution');
65
        $query->commonName = $request->query->get('commonName');
66
        $query->email = $request->query->get('email');
67
        $query->secondFactorTypes = $secondFactorTypes;
68
        $query->raInstitution = $request->query->get('raInstitution');
69
        $query->pageNumber = $request->query->getInt('p', 1);
70
71
        $query->authorizationContext = $this->authorizationService->buildSelectRaaInstitutionAuthorizationContext(
72
            $actorId,
73
        );
74
75
        $paginator = $this->raCandidateService->search($query);
76
77
        $filters = $this->raCandidateService->getFilterOptions($query);
78
79
        return JsonCollectionResponse::fromPaginator($paginator, $filters);
80
    }
81
82
    /**
83
     * @return JsonResponse
84
     */
85
    public function get(Request $request, string $identityId): JsonResponse
86
    {
87
        $this->denyAccessUnlessGrantedOneOff(['ROLE_RA', 'ROLE_READ']);
88
89
        $actorIdString = $request->query->get('actorId');
90
        if (!is_string($actorIdString)) {
91
            throw new BadRequestHttpException(sprintf('Invalid actorId "%s"', $actorIdString));
92
        }
93
94
        $actorId = new IdentityId($actorIdString);
95
96
97
        $authorizationContext = $this->authorizationService->buildInstitutionAuthorizationContext(
98
            $actorId,
99
            RegistrationAuthorityRole::ra(),
100
        );
101
102
        $raCandidate = $this->raCandidateService->findOneByIdentityId($identityId);
103
        if ($raCandidate === null) {
104
            throw new NotFoundHttpException(sprintf("RaCandidate with IdentityId '%s' does not exist", $identityId));
105
        }
106
107
        // In order to display the correct RA institutions for this ra candidate. We need to display the RA instituions
108
        // of the actor. But the identity data of the identity. This way we only show the institutions the actor is
109
        // allowed to make the identity RA(A) for.
110
        $merged = $this->raCandidateService->setUseRaInstitutionsOnRaCandidate($authorizationContext, $raCandidate);
111
112
        return new JsonResponse($merged);
113
    }
114
}
115