|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2017 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\ProfileBundle\Service; |
|
20
|
|
|
|
|
21
|
|
|
use Exception; |
|
22
|
|
|
use OpenConext\Profile\Entity\AuthenticatedUser; |
|
23
|
|
|
use OpenConext\Profile\Repository\AttributeAggregationRepository; |
|
24
|
|
|
use OpenConext\Profile\Value\AttributeAggregation\AttributeAggregationAttribute; |
|
25
|
|
|
use OpenConext\Profile\Value\AttributeAggregation\AttributeAggregationAttributesList; |
|
26
|
|
|
use OpenConext\Profile\Value\AttributeAggregation\AttributeAggregationEnabledAttributes; |
|
27
|
|
|
use OpenConext\Profile\Value\SurfConextId; |
|
28
|
|
|
use Psr\Log\LoggerInterface; |
|
29
|
|
|
use Surfnet\SamlBundle\SAML2\Attribute\AttributeDefinition; |
|
30
|
|
|
|
|
31
|
|
|
final class AttributeAggregationService |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var AttributeDefinition |
|
35
|
|
|
*/ |
|
36
|
|
|
private $surfConextUserIdAttributeDefinition; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var AttributeAggregationRepository |
|
40
|
|
|
*/ |
|
41
|
|
|
private $repository; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var AttributeAggregationEnabledAttributes |
|
45
|
|
|
*/ |
|
46
|
|
|
private $attributeAggregationEnabledAttributes; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var LoggerInterface |
|
50
|
|
|
*/ |
|
51
|
|
|
private $logger; |
|
52
|
|
|
|
|
53
|
|
|
public function __construct( |
|
54
|
|
|
AttributeAggregationRepository $repository, |
|
55
|
|
|
AttributeDefinition $surfConextUserIdAttributeDefinition, |
|
|
|
|
|
|
56
|
|
|
AttributeAggregationEnabledAttributes $attributeAggregationEnabledAttributes, |
|
|
|
|
|
|
57
|
|
|
LoggerInterface $logger |
|
58
|
|
|
) { |
|
59
|
|
|
$this->repository = $repository; |
|
60
|
|
|
$this->surfConextUserIdAttributeDefinition = $surfConextUserIdAttributeDefinition; |
|
61
|
|
|
$this->attributeAggregationEnabledAttributes = $attributeAggregationEnabledAttributes; |
|
62
|
|
|
$this->logger = $logger; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param AuthenticatedUser $user |
|
67
|
|
|
* @return null|AttributeAggregationAttributesList |
|
68
|
|
|
*/ |
|
69
|
|
|
public function findByUser(AuthenticatedUser $user) |
|
70
|
|
|
{ |
|
71
|
|
|
$enabledAttributes = $this->attributeAggregationEnabledAttributes; |
|
72
|
|
|
|
|
73
|
|
|
try { |
|
74
|
|
|
$definition = $this->surfConextUserIdAttributeDefinition; |
|
75
|
|
|
$userAttributes = $user->getAttributes(); |
|
76
|
|
|
// Does the logged in user have the SurfConextUserId attribute? |
|
77
|
|
|
if ($userAttributes->containsAttributeDefinedBy($definition)) { |
|
78
|
|
|
$collection = []; |
|
79
|
|
|
|
|
80
|
|
|
$samlAttribute = $userAttributes->getAttributeByDefinition($definition); |
|
81
|
|
|
$surfConextIdValue = $samlAttribute->getValue()[0]; |
|
82
|
|
|
$surfConextId = new SurfConextId($surfConextIdValue); |
|
83
|
|
|
$attributeAggregationAttributes = $this->repository->findAllFor($surfConextId); |
|
84
|
|
|
|
|
85
|
|
|
foreach ($enabledAttributes->getAttributes() as $enabledAttribute) { |
|
86
|
|
|
$identifier = $enabledAttribute->getIdentifier(); |
|
87
|
|
|
if ($attributeAggregationAttributes->hasAttribute($identifier)) { |
|
88
|
|
|
$aaAttribute = $attributeAggregationAttributes->getAttribute($identifier); |
|
89
|
|
|
$collection[] = AttributeAggregationAttribute::fromConfig( |
|
90
|
|
|
$enabledAttribute, |
|
91
|
|
|
true, |
|
92
|
|
|
$aaAttribute->getValues(), |
|
|
|
|
|
|
93
|
|
|
$aaAttribute->getSource() |
|
94
|
|
|
); |
|
95
|
|
|
} else { |
|
96
|
|
|
$collection[] = AttributeAggregationAttribute::fromConfig($enabledAttribute, false); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return new AttributeAggregationAttributesList($collection); |
|
101
|
|
|
} |
|
102
|
|
|
} catch (Exception $e) { |
|
103
|
|
|
$this->logger->error( |
|
104
|
|
|
sprintf( |
|
105
|
|
|
'Error while finding AA attributes. Original error message: "%s"', |
|
106
|
|
|
$e->getMessage() |
|
107
|
|
|
) |
|
108
|
|
|
); |
|
109
|
|
|
return null; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$this->logger->notice('No enabled attribute aggregation attributes found.'); |
|
113
|
|
|
return null; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.