|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2016 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 OpenConext\EngineBlockApiClientBundle\Service; |
|
20
|
|
|
|
|
21
|
|
|
use OpenConext\EngineBlockApiClientBundle\Http\JsonApiClient; |
|
22
|
|
|
use OpenConext\Profile\Value\Consent; |
|
23
|
|
|
use OpenConext\Profile\Value\ConsentList; |
|
24
|
|
|
use OpenConext\Profile\Value\SpecifiedConsent; |
|
25
|
|
|
use OpenConext\Profile\Value\SpecifiedConsentList; |
|
26
|
|
|
use OpenConext\ProfileBundle\Attribute\AttributeSetWithFallbacks; |
|
27
|
|
|
use Surfnet\SamlBundle\SAML2\Attribute\Attribute; |
|
28
|
|
|
use Surfnet\SamlBundle\SAML2\Attribute\AttributeSetInterface; |
|
29
|
|
|
|
|
30
|
|
|
final class AttributeReleasePolicyService |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var JsonApiClient |
|
34
|
|
|
*/ |
|
35
|
|
|
private $jsonApiClient; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct(JsonApiClient $jsonApiClient) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->jsonApiClient = $jsonApiClient; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param ConsentList $consentList |
|
44
|
|
|
* @param AttributeSetInterface $attributeSet |
|
45
|
|
|
* @return SpecifiedConsentList |
|
46
|
|
|
*/ |
|
47
|
|
|
public function applyAttributeReleasePolicies(ConsentList $consentList, AttributeSetInterface $attributeSet) |
|
48
|
|
|
{ |
|
49
|
|
|
$entityIds = $consentList->map(function (Consent $consent) { |
|
50
|
|
|
return $consent->getServiceProvider()->getEntity()->getEntityId()->getEntityId(); |
|
51
|
|
|
}); |
|
52
|
|
|
|
|
53
|
|
|
$mappedAttributes = []; |
|
54
|
|
|
$definitions = []; |
|
55
|
|
|
foreach ($attributeSet as $attribute) { |
|
56
|
|
|
$name = $attribute->getAttributeDefinition()->getUrnMace(); |
|
57
|
|
|
|
|
58
|
|
|
if ($name === null) { |
|
59
|
|
|
$name = $attribute->getAttributeDefinition()->getUrnOid(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$mappedAttributes[$name] = $attribute->getValue(); |
|
63
|
|
|
$definitions[$name] = $attribute->getAttributeDefinition(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$response = $this->jsonApiClient->post('/arp', [ |
|
67
|
|
|
'entityIds' => $entityIds, |
|
68
|
|
|
'attributes' => $mappedAttributes |
|
69
|
|
|
]); |
|
70
|
|
|
|
|
71
|
|
|
return SpecifiedConsentList::createWith( |
|
72
|
|
|
$consentList->map( |
|
73
|
|
|
function (Consent $consent) use ($response, $definitions) { |
|
74
|
|
|
$entityId = $consent->getServiceProvider()->getEntity()->getEntityId()->getEntityId(); |
|
75
|
|
|
|
|
76
|
|
|
$attributes = []; |
|
77
|
|
|
foreach ($response[$entityId] as $attributeName => $attributeValue) { |
|
78
|
|
|
$attributes[] = new Attribute($definitions[$attributeName], $attributeValue); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return SpecifiedConsent::specifies($consent, AttributeSetWithFallbacks::create($attributes)); |
|
82
|
|
|
} |
|
83
|
|
|
) |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|