Skip to content

Commit

Permalink
Use decorator pattern in EuLoginUser.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Mar 12, 2020
1 parent 96416ef commit 6e68297
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 35 deletions.
38 changes: 7 additions & 31 deletions src/Security/Core/User/EuLoginUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace EcPhp\EuLoginBundle\Security\Core\User;

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

use function array_key_exists;
use function is_array;
Expand All @@ -15,18 +15,18 @@
final class EuLoginUser implements EuLoginUserInterface
{
/**
* @var \EcPhp\CasBundle\Security\Core\User\CasUser
* @var CasUserInterface
*/
private $user;

/**
* EuLoginUser constructor.
*
* @param array<mixed> $data
* @param \EcPhp\CasBundle\Security\Core\User\CasUserInterface $user
*/
public function __construct(array $data)
public function __construct(CasUserInterface $user)
{
$this->user = new CasUser($this->normalizeUserData($data));
$this->user = $user;
}

/**
Expand Down Expand Up @@ -196,10 +196,8 @@ public function getRoles()
{
$default = ['ROLE_CAS_AUTHENTICATED'];

if ([] !== $roles = $this->getGroups()) {
if (true === array_key_exists('group', $roles) && true === is_array($roles['group'])) {
return array_merge($roles['group'], $default);
}
if (([] !== $roles = $this->getGroups()) && (array_key_exists('group', $roles) && is_array($roles['group']))) {
return array_merge($roles['group'], $default);
}

return $default;
Expand Down Expand Up @@ -276,26 +274,4 @@ public function getUsername()
{
return $this->getUser();
}

/**
* Normalize user data from EU Login to standard CAS user data.
*
* @param array<array|string> $data
* The data from EU Login
*
* @return array<array|string>
* The normalized data.
*/
private function normalizeUserData(array $data): array
{
$storage = [];
$rootAttributes = ['user', 'proxyGrantingTicket', 'proxies'];

foreach ($rootAttributes as $rootAttribute) {
$storage[$rootAttribute] = $data[$rootAttribute] ?? null;
}
$storage['attributes'] = array_diff_key($data, array_flip($rootAttributes));

return array_filter($storage) + ['attributes' => []];
}
}
34 changes: 32 additions & 2 deletions src/Security/Core/User/EuLoginUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace EcPhp\EuLoginBundle\Security\Core\User;

use EcPhp\CasBundle\Security\Core\User\CasUser;
use EcPhp\CasBundle\Security\Core\User\CasUserInterface;
use EcPhp\CasLib\Introspection\Contract\ServiceValidate;
use EcPhp\CasLib\Introspection\Introspector;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
Expand All @@ -22,10 +24,16 @@ class EuLoginUserProvider implements EuLoginUserProviderInterface
*/
public function loadUserByResponse(ResponseInterface $response): CasUserInterface
{
/** @var \EcPhp\CasLib\Introspection\Contract\ServiceValidate $introspect */
/** @var ServiceValidate $introspect */
$introspect = Introspector::detect($response);

return new EuLoginUser($introspect->getParsedResponse()['serviceResponse']['authenticationSuccess']);
return new EuLoginUser(
new CasUser(
$this->normalizeUserData(
$introspect->getParsedResponse()['serviceResponse']['authenticationSuccess']
)
)
);
}

/**
Expand Down Expand Up @@ -55,4 +63,26 @@ public function supportsClass($class)
{
return EuLoginUser::class === $class;
}

/**
* Normalize user data from EU Login to standard CAS user data.
*
* @param array<array|string> $data
* The data from EU Login
*
* @return array<array|string>
* The normalized data.
*/
private function normalizeUserData(array $data): array
{
$storage = [];
$rootAttributes = ['user', 'proxyGrantingTicket', 'proxies'];

foreach ($rootAttributes as $rootAttribute) {
$storage[$rootAttribute] = $data[$rootAttribute] ?? null;
}
$storage['attributes'] = array_diff_key($data, array_flip($rootAttributes));

return array_filter($storage) + ['attributes' => []];
}
}
4 changes: 2 additions & 2 deletions src/Security/Core/User/EuLoginUserProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
interface EuLoginUserProviderInterface extends CasUserProviderInterface
{
/**
* @param \Psr\Http\Message\ResponseInterface $response
* @param ResponseInterface $response
*
* @return \EcPhp\CasBundle\Security\Core\User\CasUserInterface|\EcPhp\EuLoginBundle\Security\Core\User\EuLoginUserInterface
* @return CasUserInterface|EuLoginUserInterface
*/
public function loadUserByResponse(ResponseInterface $response): CasUserInterface;
}

0 comments on commit 6e68297

Please sign in to comment.