|
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\Exception\RuntimeException; |
|
30
|
|
|
use Surfnet\SamlBundle\SAML2\Attribute\AttributeDefinition; |
|
31
|
|
|
|
|
32
|
|
|
final class AttributeAggregationService |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var AttributeDefinition |
|
36
|
|
|
*/ |
|
37
|
|
|
private $surfConextUserIdAttributeDefinition; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var AttributeAggregationRepository |
|
41
|
|
|
*/ |
|
42
|
|
|
private $repository; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var AttributeAggregationEnabledAttributes |
|
46
|
|
|
*/ |
|
47
|
|
|
private $attributeAggregationEnabledAttributes; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var LoggerInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
private $logger; |
|
53
|
|
|
|
|
54
|
|
|
public function __construct( |
|
55
|
|
|
AttributeAggregationRepository $repository, |
|
56
|
|
|
AttributeDefinition $surfConextUserIdAttributeDefinition, |
|
|
|
|
|
|
57
|
|
|
AttributeAggregationEnabledAttributes $attributeAggregationEnabledAttributes, |
|
|
|
|
|
|
58
|
|
|
LoggerInterface $logger |
|
59
|
|
|
) { |
|
60
|
|
|
$this->repository = $repository; |
|
61
|
|
|
$this->surfConextUserIdAttributeDefinition = $surfConextUserIdAttributeDefinition; |
|
62
|
|
|
$this->attributeAggregationEnabledAttributes = $attributeAggregationEnabledAttributes; |
|
63
|
|
|
$this->logger = $logger; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param AuthenticatedUser $user |
|
68
|
|
|
* @return null|AttributeAggregationAttributesList |
|
69
|
|
|
*/ |
|
70
|
|
|
public function findByUser(AuthenticatedUser $user) |
|
71
|
|
|
{ |
|
72
|
|
|
$enabledAttributes = $this->attributeAggregationEnabledAttributes; |
|
73
|
|
|
|
|
74
|
|
|
try { |
|
75
|
|
|
$definition = $this->surfConextUserIdAttributeDefinition; |
|
76
|
|
|
$userAttributes = $user->getAttributes(); |
|
77
|
|
|
// Does the logged in user have the SurfConextUserId attribute? |
|
78
|
|
|
if ($userAttributes->containsAttributeDefinedBy($definition)) { |
|
79
|
|
|
$collection = []; |
|
80
|
|
|
|
|
81
|
|
|
$samlAttribute = $userAttributes->getAttributeByDefinition($definition); |
|
82
|
|
|
$surfConextIdValue = $samlAttribute->getValue()[0]; |
|
83
|
|
|
$surfConextId = new SurfConextId($surfConextIdValue); |
|
84
|
|
|
$attributeAggregationAttributes = $this->repository->findAllFor($surfConextId); |
|
85
|
|
|
|
|
86
|
|
|
foreach ($enabledAttributes->getAttributes() as $enabledAttribute) { |
|
87
|
|
|
$accountType = $enabledAttribute->getAccountType(); |
|
88
|
|
|
if ($attributeAggregationAttributes->hasAttribute($accountType)) { |
|
89
|
|
|
$aaAttribute = $attributeAggregationAttributes->getAttribute($accountType); |
|
90
|
|
|
$collection[] = AttributeAggregationAttribute::fromConfig( |
|
91
|
|
|
$enabledAttribute, |
|
92
|
|
|
true, |
|
93
|
|
|
$aaAttribute->getId(), |
|
94
|
|
|
$aaAttribute->getSurfconextId(), |
|
95
|
|
|
$aaAttribute->getLinkedId() |
|
96
|
|
|
); |
|
97
|
|
|
} else { |
|
98
|
|
|
$collection[] = AttributeAggregationAttribute::fromConfig($enabledAttribute, false, -1, ''); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return new AttributeAggregationAttributesList($collection); |
|
103
|
|
|
} |
|
104
|
|
|
} catch (Exception $e) { |
|
105
|
|
|
$this->logger->error( |
|
106
|
|
|
sprintf( |
|
107
|
|
|
'Error while finding AA attributes. Original error message: "%s"', |
|
108
|
|
|
$e->getMessage() |
|
109
|
|
|
) |
|
110
|
|
|
); |
|
111
|
|
|
return null; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$this->logger->notice('No enabled attribute aggregation attributes found.'); |
|
115
|
|
|
return null; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param AuthenticatedUser $user |
|
120
|
|
|
* @param AttributeAggregationAttribute $orcidAttribute |
|
121
|
|
|
* |
|
122
|
|
|
* @return bool returns false when deletion failed |
|
123
|
|
|
*/ |
|
124
|
|
|
public function disconnectAttributeFor(AuthenticatedUser $user, AttributeAggregationAttribute $orcidAttribute) |
|
125
|
|
|
{ |
|
126
|
|
|
if ($this->isValidRequest($user, $orcidAttribute)) { |
|
127
|
|
|
$result = $this->repository->unsubscribeAccount($orcidAttribute->getId()); |
|
128
|
|
|
if (!$result) { |
|
129
|
|
|
$this->logger->error('Error while unsubscribing the AA attribute for the authenticating user.'); |
|
130
|
|
|
} |
|
131
|
|
|
return $result; |
|
132
|
|
|
} |
|
133
|
|
|
return false; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Validate the users identity matches that of the identity set on the ORCiD attribute retrieved from AA. |
|
138
|
|
|
* |
|
139
|
|
|
* @param AttributeAggregationAttribute $orcidAttribute |
|
140
|
|
|
* |
|
141
|
|
|
* @return bool |
|
142
|
|
|
*/ |
|
143
|
|
|
private function isValidRequest(AuthenticatedUser $user, AttributeAggregationAttribute $orcidAttribute) |
|
144
|
|
|
{ |
|
145
|
|
|
try { |
|
146
|
|
|
$surfConextId = $user->getAttributes()->getAttributeByDefinition( |
|
147
|
|
|
new AttributeDefinition('surfconextId', null, 'urn:oid:1.3.6.1.4.1.1076.20.40.40.1') |
|
148
|
|
|
); |
|
149
|
|
|
} catch (RuntimeException $e) { |
|
150
|
|
|
$this->logger->error('Attempted to find authenticated users surfconextId but was unable to find it.'); |
|
151
|
|
|
return false; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
if ($surfConextId->getValue()[0] !== $orcidAttribute->getSurfconextId()) { |
|
155
|
|
|
$this->logger->error( |
|
156
|
|
|
'The surfconextId associated with ORCiD ID account does not match the surfconextId of the |
|
157
|
|
|
authenticated user.' |
|
158
|
|
|
); |
|
159
|
|
|
return false; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return true; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.