Skip to content

Commit

Permalink
fix: update User provider
Browse files Browse the repository at this point in the history
Also fix missing services declaration
  • Loading branch information
drupol committed May 8, 2023
1 parent f84ef73 commit 7459400
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 126 deletions.
24 changes: 16 additions & 8 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use EcPhp\CasBundle\Cas\SymfonyCasInterface;
use EcPhp\CasBundle\Security\Core\User\CasUserProviderInterface;
use EcPhp\CasLib\Contract\CasInterface;
use EcPhp\CasLib\Contract\Configuration\PropertiesInterface;
use EcPhp\CasLib\Contract\Response\CasResponseBuilderInterface;
use EcPhp\Ecas\Ecas;
use EcPhp\Ecas\EcasProperties;
use EcPhp\EuLoginBundle\Cas\SymfonyECas;
use EcPhp\Ecas\Response\EcasResponseBuilder;
use EcPhp\Ecas\Service\Fingerprint\DefaultFingerprint;
use EcPhp\Ecas\Service\Fingerprint\Fingerprint;
use EcPhp\EuLoginBundle\Security\Core\User\EuLoginUserProvider;

return static function (ContainerConfigurator $container): void {
Expand All @@ -37,12 +41,16 @@
->arg('$casProperties', service('.inner'));

$services
->set('ecas.configuration', EcasProperties::class)
->decorate('cas.configuration')
->arg('$casProperties', service('ecas.configuration.inner'));
->set(Ecas::class)
->decorate(CasInterface::class)
->arg('$cas', service('.inner'));

$services
->set('ecas', Ecas::class)
->decorate('cas')
->arg('$cas', service('ecas.inner'));
->set(EcasResponseBuilder::class)
->decorate(CasResponseBuilderInterface::class)
->arg('$casResponseBuilder', service('.inner'));

$services
->set(DefaultFingerprint::class)
->alias(Fingerprint::class, DefaultFingerprint::class);
};
146 changes: 33 additions & 113 deletions src/Security/Core/User/EuLoginUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,13 @@
namespace EcPhp\EuLoginBundle\Security\Core\User;

use EcPhp\CasBundle\Security\Core\User\CasUserInterface;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\UserInterface;

use function array_key_exists;

final class EuLoginUser implements EuLoginUserInterface
{
private CasUserInterface $user;

public function __construct(CasUserInterface $user)
{
$this->user = $user;
public function __construct(
private readonly CasUserInterface $user
) {
}

public function __toString(): string
Expand All @@ -33,7 +28,6 @@ public function __toString(): string

public function eraseCredentials(): void
{
throw new UnsupportedUserException('Unsupported method.');
}

public function get(string $key, mixed $default = null): mixed
Expand All @@ -43,149 +37,87 @@ public function get(string $key, mixed $default = null): mixed

public function getAssuranceLevel(): ?string
{
return $this->user->getAttribute('assuranceLevel');
return $this->get('assuranceLevel');
}

public function getAttribute(string $key, mixed $default = null): mixed
{
return $this->user->getAttribute($key, $default);
}

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

/** @Todo Ugly. Refactor this when JSON format will be available. */
$propertyToMangle = [
['extendedAttributes', 'extendedAttribute'],
['groups', 'group'],
['strengths', 'strength'],
['authenticationFactors', 'authenticationFactor'],
];

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

if (!array_key_exists($child, $attributes[$parent])) {
continue;
}

$attributes[$parent][$child] = (array) $attributes[$parent][$child];

if (array_key_exists(0, $attributes[$parent][$child])) {
continue;
}

$attributes[$parent][$child] = [$attributes[$parent][$child]];
}

return $attributes;
return $this->get($key, $default);
}

public function getAuthenticationFactors(): array
{
return $this->user->getAttribute('authenticationFactors', []);
return $this->get('authenticationFactors', []);
}

public function getDepartmentNumber(): ?string
{
return $this->user->getAttribute('departmentNumber');
return $this->get('departmentNumber');
}

public function getDomain(): ?string
{
return $this->user->getAttribute('domain');
return $this->get('domain');
}

public function getDomainUsername(): ?string
{
return $this->user->getAttribute('domainUsername');
return $this->get('domainUsername');
}

public function getEmail(): ?string
{
return $this->user->getAttribute('email');
return $this->get('email');
}

public function getEmployeeNumber(): ?string
{
return $this->user->getAttribute('employeeNumber');
return $this->get('employeeNumber');
}

public function getEmployeeType(): ?string
{
return $this->user->getAttribute('employeeType');
return $this->get('employeeType');
}

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(
$extendedAttributes,
static function (array $carry, array $item): array {
$carry[$item['@attributes']['name']] = $item['attributeValue'];

return $carry;
},
[]
);
return $this->get('extendedAttributes', []);
}

public function getFirstName(): ?string
{
return $this->user->getAttribute('firstName');
return $this->get('firstName');
}

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

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

$groups = $attributes['groups'];

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

return $groups['group'];
return $this->get('groups', []);
}

public function getLastName(): ?string
{
return $this->user->getAttribute('lastName');
return $this->get('lastName');
}

public function getLocale(): ?string
{
return $this->user->getAttribute('locale');
return $this->get('locale');
}

public function getLoginDate(): ?string
{
return $this->user->getAttribute('loginDate');
return $this->get('loginDate');
}

public function getOrgId(): ?string
{
return $this->user->getAttribute('orgId');
return $this->get('orgId');
}

public function getPayload(): array
{
return $this->user->getPayload();
}

public function getPgt(): ?string
Expand All @@ -195,7 +127,7 @@ public function getPgt(): ?string

public function getProxyGrantingProtocol(): ?string
{
return $this->user->getAttribute('proxyGrantingProtocol');
return $this->get('proxyGrantingProtocol');
}

public function getRoles(): array
Expand All @@ -207,49 +139,37 @@ public function getRoles(): array

public function getSso(): ?string
{
return $this->user->getAttribute('sso');
return $this->get('sso');
}

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

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

$strengths = $attributes['strengths'];

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

return (array) $strengths['strength'];
return $this->get('strengths', []);
}

public function getTelephoneNumber(): ?string
{
return $this->user->getAttribute('telephoneNumber');
return $this->get('telephoneNumber');
}

public function getTeleworkingPriority(): ?string
{
return $this->user->getAttribute('teleworkingPriority');
return $this->get('teleworkingPriority');
}

public function getTicketType(): ?string
{
return $this->user->getAttribute('ticketType');
return $this->get('ticketType');
}

public function getTimeZone(): ?string
{
return $this->user->getAttribute('timeZone');
return $this->get('timeZone');
}

public function getUid(): ?string
{
return $this->user->getAttribute('uid');
return $this->get('uid');
}

public function getUserIdentifier(): string
Expand All @@ -259,7 +179,7 @@ public function getUserIdentifier(): string

public function getUserManager(): ?string
{
return $this->user->getAttribute('userManager');
return $this->get('userManager');
}

public function isEqualTo(UserInterface $user): bool
Expand Down
8 changes: 3 additions & 5 deletions src/Security/Core/User/EuLoginUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@

final class EuLoginUserProvider implements CasUserProviderInterface
{
private CasUserProviderInterface $casUserProvider;

public function __construct(CasUserProviderInterface $casUserProvider)
{
$this->casUserProvider = $casUserProvider;
public function __construct(
private readonly CasUserProviderInterface $casUserProvider
) {
}

public function loadUserByIdentifier(string $identifier): UserInterface
Expand Down

0 comments on commit 7459400

Please sign in to comment.