3737const ATTRIBUTES = 'attributes ' ;
3838const ATTR_MAP = 'attrMap ' ;
3939const ATTR_TO_CONVERSION = 'attrsToConversion ' ;
40+ const APPEND_ONLY_ATTRS = 'appendOnlyAttrs ' ;
4041const PERUN_USER_ID = 'perunUserId ' ;
4142
4243const 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