Completed
Push — feature/provide-ra-listing-sea... ( 25358b )
by Michiel
03:06
created

RaListingController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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\Identity\Value\IdentityId;
22
use Surfnet\Stepup\Identity\Value\Institution;
23
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Service\InstitutionAuthorizationService;
24
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\RaListingQuery;
25
use Surfnet\StepupMiddleware\ApiBundle\Identity\Service\RaListingService;
26
use Surfnet\StepupMiddleware\ApiBundle\Response\JsonCollectionResponse;
27
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
28
use Symfony\Component\HttpFoundation\JsonResponse;
29
use Symfony\Component\HttpFoundation\Request;
30
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
31
32
class RaListingController extends Controller
33
{
34
    /**
35
     * @var RaListingService
36
     */
37
    private $raListingService;
38
39
    /**
40
     * @var InstitutionAuthorizationService
41
     */
42
    private $authorizationService;
43
44
    public function __construct(
45
        RaListingService $raListingService,
46
        InstitutionAuthorizationService $authorizationService
47
    ) {
48
        $this->raListingService = $raListingService;
49
        $this->authorizationService = $authorizationService;
50
    }
51
52
    public function getAction(Request $request, $identityId)
53
    {
54
        $this->denyAccessUnlessGranted(['ROLE_RA']);
55
56
        $institution = $request->get('institution');
57
        $raListing = $this->raListingService->findByIdentityIdAndRaInstitution(new IdentityId($identityId), new Institution($institution));
58
59
        if ($raListing === null) {
60
            throw new NotFoundHttpException(sprintf("RaListing '%s' does not exist", $identityId));
61
        }
62
63
        return new JsonResponse($raListing);
64
    }
65
66
    /**
67
     * @param Request $request
68
     * @param Institution $actorInstitution
69
     * @return JsonCollectionResponse
70
     */
71
    public function searchAction(Request $request, Institution $actorInstitution)
72
    {
73
        $this->denyAccessUnlessGranted(['ROLE_RA']);
74
75
        $actorId = new IdentityId($request->get('actorId'));
76
77
        $query = new RaListingQuery();
78
79
        if ($request->get('identityId')) {
80
            $query->identityId = new IdentityId($request->get('identityId'));
81
        }
82
83
        if ($request->get('institution')) {
84
            $query->institution = $request->get('institution');
85
        }
86
87
        if ($request->get('name')) {
88
            $query->name = $request->get('name');
89
        }
90
91
        if ($request->get('email')) {
92
            $query->email = $request->get('email');
93
        }
94
95
        if ($request->get('role')) {
96
            $query->role = $request->get('role');
97
        }
98
99
        if ($request->get('raInstitution')) {
100
            $query->raInstitution = $request->get('raInstitution');
101
        }
102
103
        $query->pageNumber = (int)$request->get('p', 1);
104
        $query->orderBy = $request->get('orderBy');
105
        $query->orderDirection = $request->get('orderDirection');
106
        $query->authorizationContext = $this->authorizationService->buildInstitutionAuthorizationContextForManagement(
107
            $actorId,
108
            $actorInstitution
109
        );
110
111
        $searchResults = $this->raListingService->search($query);
112
113
        return JsonCollectionResponse::fromPaginator($searchResults);
114
    }
115
}
116