Skip to content

Commit

Permalink
Update codebase to parse XML in a more efficient maneer.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Sep 1, 2020
1 parent 4be7a54 commit f9c247b
Showing 1 changed file with 68 additions and 4 deletions.
72 changes: 68 additions & 4 deletions src/Security/Core/User/EuLoginUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use EcPhp\CasBundle\Security\Core\User\CasUserInterface;

use function array_key_exists;

final class EuLoginUser implements EuLoginUserInterface
{
/**
Expand Down Expand Up @@ -56,7 +58,31 @@ public function getAttribute(string $key, $default = null)
public function getAttributes(): array
{
$attributes = $this->user->getAttributes();
$attributes['extendedAttributes'] = $this->getExtendedAttributes();

$propertyToMangle = [
['extendedAttributes', 'extendedAttribute'],
['groups', 'group'],
['strengths', 'strength'],
['authenticationFactors', 'authenticationFactor'],
];

foreach ($propertyToMangle as $properties) {
if (!array_key_exists($properties[0], $attributes)) {
continue;
}

if (!array_key_exists($properties[1], $attributes[$properties[0]])) {
continue;
}

$attributes[$properties[0]][$properties[1]] = (array) $attributes[$properties[0]][$properties[1]];

if (array_key_exists(0, $attributes[$properties[0]][$properties[1]])) {
continue;
}

$attributes[$properties[0]][$properties[1]] = [$attributes[$properties[0]][$properties[1]]];
}

return $attributes;
}
Expand Down Expand Up @@ -119,8 +145,22 @@ public function getEmployeeType(): ?string

public function getExtendedAttributes(): array
{
$attributes = $this->getAttributes();

if (!array_key_exists('extendedAttributes', $attributes)) {
return [];
}

$extendedAttributes = $attributes['extendedAttributes'];

if (!array_key_exists('extendedAttribute', $extendedAttributes)) {
return [];
}

$extendedAttributes = $attributes['extendedAttributes']['extendedAttribute'];

return array_reduce(
$this->user->getAttribute('extendedAttributes', []),
$extendedAttributes,
static function (array $carry, array $item): array {
$carry[$item['@attributes']['name']] = $item['attributeValue'];

Expand All @@ -143,7 +183,19 @@ public function getFirstName(): ?string
*/
public function getGroups(): array
{
return $this->user->getAttribute('groups', ['group' => []])['group'];
$attributes = $this->getAttributes();

if (!array_key_exists('groups', $attributes)) {
return [];
}

$groups = $attributes['groups'];

if (!array_key_exists('group', $groups)) {
return [];
}

return $groups['group'];
}

/**
Expand Down Expand Up @@ -225,7 +277,19 @@ public function getSso(): ?string
*/
public function getStrengths(): array
{
return $this->user->getAttribute('strengths', []);
$attributes = $this->getAttributes();

if (!array_key_exists('strengths', $attributes)) {
return [];
}

$strengths = $attributes['strengths'];

if (!array_key_exists('strength', $strengths)) {
return [];
}

return (array) $strengths['strength'];
}

/**
Expand Down

0 comments on commit f9c247b

Please sign in to comment.