Skip to content
This repository was archived by the owner on Sep 19, 2022. It is now read-only.

Commit b241135

Browse files
committed
feat: UpdateUserExtSource - introduces appendOnlyAttrs, fixes the way how attrsToUpdate are created
1 parent 48c6949 commit b241135

File tree

2 files changed

+49
-33
lines changed

2 files changed

+49
-33
lines changed

lib/Auth/Process/UpdateUserExtSource.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class UpdateUserExtSource extends ProcessingFilter
2222

2323
private $attrsToConversion;
2424

25+
private $appendOnlyAttrs = [];
26+
2527
public function __construct($config, $reserved)
2628
{
2729
parent::__construct($config, $reserved);
@@ -38,6 +40,10 @@ public function __construct($config, $reserved)
3840
$this->attrsToConversion = [];
3941
}
4042

43+
if (isset($config['appendOnlyAttrs'])) {
44+
$this->appendOnlyAttrs = (array) $config['appendOnlyAttrs'];
45+
}
46+
4147
$this->attrMap = (array) $config['attrMap'];
4248
}
4349

@@ -50,6 +56,7 @@ public function process(&$request)
5056
'attributes' => $request['Attributes'],
5157
'attrMap' => $this->attrMap,
5258
'attrsToConversion' => $this->attrsToConversion,
59+
'appendOnlyAttrs' => $this->appendOnlyAttrs,
5360
'perunUserId' => $request['perun']['user']->getId(),
5461
];
5562
$token = $challengeManager->generateToken($id, self::SCRIPT_NAME, $data);

www/updateUes.php

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
const ATTRIBUTES = 'attributes';
3838
const ATTR_MAP = 'attrMap';
3939
const ATTR_TO_CONVERSION = 'attrsToConversion';
40+
const APPEND_ONLY_ATTRS = 'appendOnlyAttrs';
4041
const PERUN_USER_ID = 'perunUserId';
4142

4243
const EDU_PERSON_UNIQUE_ID = 'eduPersonUniqueId';
@@ -82,7 +83,8 @@ function getConfiguration()
8283

8384
$attributesFromIdP = null;
8485
$attrMap = null;
85-
$serializedAttributes = null;
86+
$serializedAttributes = [];
87+
$appendOnlyAttrs = [];
8688
$perunUserId = null;
8789
$id = null;
8890
$sourceIdpAttribute = null;
@@ -91,9 +93,12 @@ function getConfiguration()
9193
$challengeManager = new ChallengeManager();
9294
$claims = $challengeManager->decodeToken($token);
9395

96+
Logger::debug('updateUes attributes ' . print_r($claims[DATA][ATTRIBUTES], true));
97+
9498
$attributesFromIdP = $claims[DATA][ATTRIBUTES];
9599
$attrMap = $claims[DATA][ATTR_MAP];
96100
$serializedAttributes = $claims[DATA][ATTR_TO_CONVERSION];
101+
$appendOnlyAttrs = $claims[DATA][APPEND_ONLY_ATTRS];
97102
$perunUserId = $claims[DATA][PERUN_USER_ID];
98103
$id = $claims[ID];
99104
} catch (Exception $ex) {
@@ -129,6 +134,7 @@ function getConfiguration()
129134
$attributesFromPerun,
130135
$attrMap,
131136
$serializedAttributes,
137+
$appendOnlyAttrs,
132138
$attributesFromIdP
133139
);
134140

@@ -146,7 +152,6 @@ function findUserExtSource($adapter, $extSourceName, $attributesFromIdp, $identi
146152
{
147153
foreach ($attributesFromIdp as $attrName => $attrValue) {
148154
if (!in_array($attrName, $identifierAttributes, true)) {
149-
Logger::debug(DEBUG_PREFIX . 'Identifier \'' . $attrName . '\' not listed in userIdentifiers. Skipping');
150155
continue;
151156
}
152157

@@ -176,9 +181,6 @@ function getUserExtSource($adapter, $extSourceName, $extLogin)
176181
try {
177182
return $adapter->getUserExtSource($extSourceName, $extLogin);
178183
} catch (SimpleSAML\Module\perun\Exception $ex) {
179-
Logger::debug(DEBUG_PREFIX . 'Caught exception when fetching user ext source, probably does not exist.');
180-
Logger::debug(DEBUG_PREFIX . $ex->getMessage());
181-
182184
return null;
183185
}
184186
}
@@ -204,36 +206,46 @@ function getAttributesFromPerun($adapter, $attrMap, $userExtSource): array
204206
return $attributesFromPerun;
205207
}
206208

207-
function getAttributesToUpdate($attributesFromPerun, $attrMap, $serializedAttributes, $attributesFromIdP): array
209+
function getAttributesToUpdate($attributesFromPerun, $attrMap, $serializedAttributes, $appendOnlyAttrs, $attributesFromIdP): array
208210
{
209211
$attributesToUpdate = [];
210212

211213
foreach ($attributesFromPerun as $attribute) {
212214
$attrName = $attribute[NAME];
213215

214-
$mappedAttributeName = !empty($attrMap[$attrName]) ? $attrMap[$attrName] : null;
215-
$idpAttribute = !empty($attributesFromIdP[$attrMap[$attrName]]) ?
216+
$attr = !empty($attributesFromIdP[$attrMap[$attrName]]) ?
216217
$attributesFromIdP[$attrMap[$attrName]] : null;
217218

218-
if (null !== $mappedAttributeName && null !== $idpAttribute) {
219-
if (in_array($attrName, $serializedAttributes, true)) {
220-
$idpAttribute = serializeAsString($idpAttribute);
221-
}
219+
// appendOnly && has value && (complex || serialized)
220+
if (in_array($attrName, $appendOnlyAttrs, true) &&
221+
!empty($attribute[VALUE]) &&
222+
(isComplexType($attribute[TYPE]) || in_array($attrName, $serializedAttributes, true))
223+
) {
224+
$attr = in_array($attrName, $serializedAttributes, true) ?
225+
array_merge($attr, explode(';', $attribute[VALUE])) : array_merge($attr, $attribute[VALUE]);
226+
}
227+
222228

223-
if (isSimpleType($attribute[TYPE])) {
224-
$valueFromIdP = $idpAttribute[0];
225-
} elseif (isComplexType($attribute[TYPE])) {
226-
$valueFromIdP = $idpAttribute;
229+
if (isSimpleType($attribute[TYPE])) {
230+
$newValue = convertToString($attr);
231+
} elseif (isComplexType($attribute[TYPE])) {
232+
if (!empty($attr)) {
233+
$newValue = array_values(array_unique($attr));
227234
} else {
228-
Logger::debug(DEBUG_PREFIX . 'Unsupported type of attribute.');
229-
continue;
235+
$newValue = [];
230236
}
231-
232-
if ($valueFromIdP !== $attribute[VALUE]) {
233-
$attribute[VALUE] = $valueFromIdP;
234-
$attribute[NAMESPACE_KEY] = UES_ATTR_NMS;
235-
$attributesToUpdate[] = $attribute;
237+
if (in_array($attrName, $serializedAttributes, true)) {
238+
$newValue = convertToString($newValue);
236239
}
240+
} else {
241+
Logger::debug(DEBUG_PREFIX . 'Unsupported type of attribute.');
242+
continue;
243+
}
244+
245+
if ($newValue !== $attribute[VALUE]) {
246+
$attribute[VALUE] = $newValue;
247+
$attribute[NAMESPACE_KEY] = UES_ATTR_NMS;
248+
$attributesToUpdate[] = $attribute;
237249
}
238250
}
239251

@@ -271,17 +283,14 @@ function isComplexType($attributeType): bool
271283
strpos($attributeType, MAP_TYPE);
272284
}
273285

274-
function serializeAsString($idpAttribute): array
286+
function convertToString($newValue)
275287
{
276-
$arrayAsString = [''];
277-
278-
foreach ($idpAttribute as $value) {
279-
$arrayAsString[0] .= $value . ';';
280-
}
281-
282-
if (!empty($arrayAsString[0])) {
283-
$arrayAsString[0] = substr($arrayAsString[0], 0, -1);
288+
if (!empty($newValue)) {
289+
$newValue = array_unique($newValue);
290+
$attrValueAsString = implode(';', $newValue);
291+
} else {
292+
$attrValueAsString = '';
284293
}
285294

286-
return $arrayAsString;
295+
return $attrValueAsString;
287296
}

0 commit comments

Comments
 (0)