1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2018 SURFnet B.V. |
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\Authorization\Service; |
20
|
|
|
|
21
|
|
|
use Surfnet\Stepup\Identity\Value\Institution; |
22
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Value\InstitutionAuthorizationContext; |
23
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Value\InstitutionRoleSetInterface; |
24
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Exception\InvalidArgumentException; |
25
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Service\IdentityService; |
26
|
|
|
use Symfony\Component\HttpFoundation\Request; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* InstitutionAuthorizationContextFactory |
30
|
|
|
* |
31
|
|
|
* Use this factory to build InstitutionAuthorizationContext instances |
32
|
|
|
* from a GET request with authorization context. |
33
|
|
|
*/ |
34
|
|
|
class InstitutionAuthorizationContextFactory implements InstitutionAuthorizationContextFactoryInterface |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var IdentityService |
38
|
|
|
*/ |
39
|
|
|
private $identityService; |
40
|
|
|
|
41
|
|
|
public function __construct(IdentityService $identityService) |
42
|
|
|
{ |
43
|
|
|
$this->identityService = $identityService; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function buildFrom(Request $request, InstitutionRoleSetInterface $roleRequirements) |
47
|
|
|
{ |
48
|
|
|
$actorId = $request->get('actorId'); |
49
|
|
|
$actorInstitution = $request->get('actorInstitution'); |
50
|
|
|
$institution = $request->get('institution'); |
51
|
|
|
|
52
|
|
|
if ($actorId && $actorInstitution) { |
53
|
|
|
|
54
|
|
|
// Retrieve the identity from the service |
55
|
|
|
$actorIdentity = $this->identityService->find($actorId); |
56
|
|
|
|
57
|
|
|
// The identity of the actor must exist |
58
|
|
|
if (!$actorIdentity) { |
59
|
|
|
throw new InvalidArgumentException('Identity with id "%s" could not be found.'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$institution = new Institution($actorInstitution); |
63
|
|
|
|
64
|
|
|
// The identity that was returned from the service should match the institution that was provided in the |
65
|
|
|
// actorInstitution request parameter. |
66
|
|
|
if (!$institution->equals($actorIdentity->institution)) { |
67
|
|
|
throw new InvalidArgumentException( |
68
|
|
|
'The institution of the actor did not match that of the institution found in the actor identity.' |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return new InstitutionAuthorizationContext( |
73
|
|
|
$actorIdentity, |
|
|
|
|
74
|
|
|
$institution, |
75
|
|
|
$roleRequirements |
76
|
|
|
); |
77
|
|
|
} else if ($institution) { |
78
|
|
|
|
79
|
|
|
// For backwards compatibility |
80
|
|
|
return new InstitutionAuthorizationContext( |
81
|
|
|
'', |
82
|
|
|
$institution, |
83
|
|
|
$roleRequirements |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
throw new InvalidArgumentException( |
88
|
|
|
'The actorId and actorInstitution were not given in the request.' |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: