RaListingController::search()   B
last analyzed

Complexity

Conditions 8
Paths 65

Size

Total Lines 49
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 27
nc 65
nop 1
dl 0
loc 49
rs 8.4444
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\Institution;
23
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...
24
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Service\AuthorizationContextService;
25
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaListing;
26
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\RaListingQuery;
27
use Surfnet\StepupMiddleware\ApiBundle\Identity\Service\RaListingService;
28
use Surfnet\StepupMiddleware\ApiBundle\Response\JsonCollectionResponse;
29
use Symfony\Component\HttpFoundation\JsonResponse;
30
use Symfony\Component\HttpFoundation\Request;
31
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
32
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
33
34
class RaListingController extends AbstractController
35
{
36
    public function __construct(
37
        private readonly RaListingService $raListingService,
38
        private readonly AuthorizationContextService $authorizationService,
39
    ) {
40
    }
41
42
    public function get(Request $request, string $identityId, string $institution): JsonResponse
43
    {
44
        $this->denyAccessUnlessGrantedOneOff(['ROLE_RA', 'ROLE_READ']);
45
46
        $actorIdString = $request->query->get('actorId');
47
        if (!is_string($actorIdString)) {
48
            throw new BadRequestHttpException(sprintf('Invalid actorId "%s"', $actorIdString));
49
        }
50
        $actorId = new IdentityId($actorIdString);
51
52
        $institutionObject = new Institution($institution);
53
54
        $authorizationContext = $this->authorizationService->buildInstitutionAuthorizationContext(
55
            $actorId,
56
            RegistrationAuthorityRole::raa(),
57
        );
58
59
        $raListing = $this->raListingService->findByIdentityIdAndRaInstitutionWithContext(
60
            new IdentityId($identityId),
61
            $institutionObject,
62
            $authorizationContext,
63
        );
64
65
        if (!$raListing instanceof RaListing) {
66
            throw new NotFoundHttpException(sprintf("RaListing '%s' does not exist", $identityId));
67
        }
68
69
        return new JsonResponse($raListing);
70
    }
71
72
    /**
73
     * @return JsonCollectionResponse
74
     */
75
    public function search(Request $request): JsonCollectionResponse
76
    {
77
        $this->denyAccessUnlessGrantedOneOff(['ROLE_RA', 'ROLE_READ']);
78
79
        $actorIdString = $request->query->get('actorId');
80
        if (!is_string($actorIdString)) {
81
            throw new BadRequestHttpException(sprintf('Invalid actorId "%s"', $actorIdString));
82
        }
83
        $actorId = new IdentityId($actorIdString);
84
85
        $query = new RaListingQuery();
86
87
        if ($request->query->get('identityId')) {
88
            $query->identityId = new IdentityId($request->query->get('identityId'));
89
        }
90
91
        if ($request->query->get('institution')) {
92
            $query->institution = $request->query->get('institution');
93
        }
94
95
        if ($request->query->get('name')) {
96
            $query->name = $request->query->get('name');
97
        }
98
99
        if ($request->query->get('email')) {
100
            $query->email = $request->query->get('email');
101
        }
102
103
        if ($request->query->get('role')) {
104
            $query->role = $request->query->get('role');
105
        }
106
107
        if ($request->query->get('raInstitution')) {
108
            $query->raInstitution = $request->query->get('raInstitution');
109
        }
110
111
        $query->pageNumber = $request->query->getInt('p', 1);
112
        $query->orderBy = $request->query->getString('orderBy');
113
        $query->orderDirection = $request->query->getString('orderDirection');
114
        $query->authorizationContext = $this->authorizationService->buildInstitutionAuthorizationContext(
115
            $actorId,
116
            RegistrationAuthorityRole::raa(),
117
        );
118
119
        $searchResults = $this->raListingService->search($query);
120
121
        $filters = $this->raListingService->getFilterOptions($query);
122
123
        return JsonCollectionResponse::fromPaginator($searchResults, $filters);
124
    }
125
}
126