Skip to content
Snippets Groups Projects
Commit 11e599bb authored by dev's avatar dev
Browse files

adds `flatten()` method to put also the nested elements in $userdata (e.g....

adds `flatten()` method to put also the nested elements in $userdata (e.g. location) which are retrieved from linkedIn into the resulting attributes array
parent abb3a2b0
No related branches found
No related tags found
No related merge requests found
...@@ -107,12 +107,7 @@ class sspmod_authlinkedin_Auth_Source_LinkedIn extends SimpleSAML_Auth_Source { ...@@ -107,12 +107,7 @@ class sspmod_authlinkedin_Auth_Source_LinkedIn extends SimpleSAML_Auth_Source {
$userdata = $consumer->getUserInfo('https://api.linkedin.com/v1/people/~:(' . $this->attributes . ')', $accessToken, array('http' => array('header' => 'x-li-format: json'))); $userdata = $consumer->getUserInfo('https://api.linkedin.com/v1/people/~:(' . $this->attributes . ')', $accessToken, array('http' => array('header' => 'x-li-format: json')));
$attributes = array(); $attributes = $this->flatten($userdata, 'linkedin.');
foreach($userdata AS $key => $value) {
if (is_string($value))
$attributes['linkedin.' . $key] = array((string)$value);
}
// TODO: pass accessToken: key, secret + expiry as attributes? // TODO: pass accessToken: key, secret + expiry as attributes?
...@@ -125,4 +120,43 @@ class sspmod_authlinkedin_Auth_Source_LinkedIn extends SimpleSAML_Auth_Source { ...@@ -125,4 +120,43 @@ class sspmod_authlinkedin_Auth_Source_LinkedIn extends SimpleSAML_Auth_Source {
$state['Attributes'] = $attributes; $state['Attributes'] = $attributes;
} }
/**
* takes an associative array, traverses it and returns the keys concatenated with a dot
*
* e.g.:
*
* [
* 'linkedin' => [
* 'location' => [
* 'id' => '123456'
* 'country' => [
* 'code' => 'de'
* ]
* ]
* ]
*
* become:
*
* [
* 'linkedin.location.id' => [0 => '123456'],
* 'linkedin.location.country.code' => [0 => 'de']
* ]
*
* @param array $array
* @param string $prefix
*
* @return array the array with the new concatenated keys
*/
protected function flatten($array, $prefix = '') {
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = $result + $this->flatten($value, $prefix . $key . '.');
} else {
$result[$prefix . $key] = array($value);
}
}
return $result;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment