Skip to content
Snippets Groups Projects
Select Git revision
  • 4265ff52f8c6c863788c39ff1bdffc2ab488d651
  • main default
  • keycloak-deprecate
  • remove-jwt-easy
  • ci-update
  • v0.1.15
  • v0.1.14
  • v0.1.13
  • v0.1.12
  • v0.1.11
  • v0.1.10
  • v0.1.9
  • v0.1.8
  • v0.1.7
  • v0.1.6
  • v0.1.5
  • v0.1.4
  • v0.1.3
  • v0.1.2
  • v0.1.1
  • v0.1.0
21 results

BearerUser.php

Blame
  • user avatar
    Tobias Gross-Vogt authored
    4265ff52
    History
    BearerUser.php 3.59 KiB
    <?php
    
    declare(strict_types=1);
    
    namespace Dbp\Relay\AuthBundle\Authenticator;
    
    use Dbp\Relay\AuthBundle\API\AuthorizationDataProviderInterface;
    use Dbp\Relay\CoreBundle\API\UserInterface as DbpUserInterface;
    use Symfony\Component\Security\Core\User\UserInterface as SymfonyUserInterface;
    
    class BearerUser implements DbpUserInterface, SymfonyUserInterface
    {
        /** @var string[] */
        private $rolesDeprecated;
    
        /** @var string|null */
        private $identifier;
    
        /** @var array */
        private $roles;
    
        /** @var array */
        private $attributes;
    
        /** @var iterable */
        private $authorizationDataProviders;
    
        public function __construct(?string $identifier, array $rolesDeprecated)
        {
            $this->rolesDeprecated = $rolesDeprecated;
            $this->identifier = $identifier;
    
            $this->roles = [];
            $this->attributes = [];
            $this->authorizationDataProviders = [];
        }
    
        public function getRoles(): array
        {
            return $this->rolesDeprecated;
        }
    
        public function getPassword(): ?string
        {
            return null;
        }
    
        public function getSalt(): ?string
        {
            return null;
        }
    
        public function getUsername(): string
        {
            return $this->getUserIdentifier();
        }
    
        public function getUserIdentifier(): string
        {
            return $this->identifier ?? '';
        }
    
        public function eraseCredentials()
        {
        }
    
        public function setAuthorizationDataProviders(iterable $authorizationDataProviders)
        {
            $this->authorizationDataProviders = $authorizationDataProviders;
        }
    
        public function hasRole(string $roleName): bool
        {
            if (array_key_exists($roleName, $this->roles) === false) {
                $this->loadRole($roleName);
            }
    
            return $this->roles[$roleName] ?? false;
        }
    
        /**
         * @return mixed|null
         */
        public function getAttribute(string $attributeName)
        {
            if (array_key_exists($attributeName, $this->attributes) === false) {
                $this->loadAttributes($attributeName);
            }
    
            return $this->attributes[$attributeName] ?? null;
        }
    
        private function loadRole(string $roleName)
        {
            foreach ($this->authorizationDataProviders as $authorizationDataProvider) {
                $availableRoles = $authorizationDataProvider->getAvailableRoles();
                if (in_array($roleName, $availableRoles, true)) {
                    $this->loadUserDataFromAuthorizationProvider($authorizationDataProvider);
                    break;
                }
            }
        }
    
        private function loadAttributes(string $attributeName)
        {
            foreach ($this->authorizationDataProviders as $authorizationDataProvider) {
                $availableAttributes = $authorizationDataProvider->getAvailableAttributes();
                if (in_array($attributeName, $availableAttributes, true)) {
                    $this->loadUserDataFromAuthorizationProvider($authorizationDataProvider);
                    break;
                }
            }
        }
    
        private function loadUserDataFromAuthorizationProvider(AuthorizationDataProviderInterface $authorizationDataProvider)
        {
            $userRoles = [];
            $userAttributes = [];
            $authorizationDataProvider->getUserData($this->identifier, $userRoles, $userAttributes);
    
            foreach ($authorizationDataProvider->getAvailableAttributes() as $availableAttribute) {
                $this->attributes[$availableAttribute] = $userAttributes[$availableAttribute] ?? null;
            }
    
            foreach ($authorizationDataProvider->getAvailableRoles() as $availableRole) {
                $this->roles[$availableRole] = in_array($availableRole, $userRoles, true);
            }
        }
    }