vendor/symfony/security-core/Authentication/Token/AbstractToken.php line 73

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Security\Core\Authentication\Token;
  11. use Symfony\Component\Security\Core\User\InMemoryUser;
  12. use Symfony\Component\Security\Core\User\UserInterface;
  13. /**
  14.  * Base class for Token instances.
  15.  *
  16.  * @author Fabien Potencier <fabien@symfony.com>
  17.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  18.  */
  19. abstract class AbstractToken implements TokenInterface, \Serializable
  20. {
  21.     private $user = null;
  22.     private array $roleNames = [];
  23.     private array $attributes = [];
  24.     /**
  25.      * @param string[] $roles An array of roles
  26.      *
  27.      * @throws \InvalidArgumentException
  28.      */
  29.     public function __construct(array $roles = [])
  30.     {
  31.         foreach ($roles as $role) {
  32.             $this->roleNames[] = $role;
  33.         }
  34.     }
  35.     /**
  36.      * {@inheritdoc}
  37.      */
  38.     public function getRoleNames(): array
  39.     {
  40.         return $this->roleNames;
  41.     }
  42.     public function getUserIdentifier(): string
  43.     {
  44.         return $this->user ? $this->user->getUserIdentifier() : '';
  45.     }
  46.     /**
  47.      * {@inheritdoc}
  48.      */
  49.     public function getUser(): ?UserInterface
  50.     {
  51.         return $this->user;
  52.     }
  53.     /**
  54.      * {@inheritdoc}
  55.      */
  56.     public function setUser(UserInterface $user)
  57.     {
  58.         $this->user = $user;
  59.     }
  60.     /**
  61.      * {@inheritdoc}
  62.      */
  63.     public function eraseCredentials()
  64.     {
  65.         if ($this->getUser() instanceof UserInterface) {
  66.             $this->getUser()->eraseCredentials();
  67.         }
  68.     }
  69.     /**
  70.      * Returns all the necessary state of the object for serialization purposes.
  71.      *
  72.      * There is no need to serialize any entry, they should be returned as-is.
  73.      * If you extend this method, keep in mind you MUST guarantee parent data is present in the state.
  74.      * Here is an example of how to extend this method:
  75.      * <code>
  76.      *     public function __serialize(): array
  77.      *     {
  78.      *         return [$this->childAttribute, parent::__serialize()];
  79.      *     }
  80.      * </code>
  81.      *
  82.      * @see __unserialize()
  83.      */
  84.     public function __serialize(): array
  85.     {
  86.         return [$this->user, true, null, $this->attributes, $this->roleNames];
  87.     }
  88.     /**
  89.      * Restores the object state from an array given by __serialize().
  90.      *
  91.      * There is no need to unserialize any entry in $data, they are already ready-to-use.
  92.      * If you extend this method, keep in mind you MUST pass the parent data to its respective class.
  93.      * Here is an example of how to extend this method:
  94.      * <code>
  95.      *     public function __unserialize(array $data): void
  96.      *     {
  97.      *         [$this->childAttribute, $parentData] = $data;
  98.      *         parent::__unserialize($parentData);
  99.      *     }
  100.      * </code>
  101.      *
  102.      * @see __serialize()
  103.      */
  104.     public function __unserialize(array $data): void
  105.     {
  106.         [$user, , , $this->attributes, $this->roleNames] = $data;
  107.         $this->user = \is_string($user) ? new InMemoryUser($user, '', $this->roleNames, false) : $user;
  108.     }
  109.     /**
  110.      * {@inheritdoc}
  111.      */
  112.     public function getAttributes(): array
  113.     {
  114.         return $this->attributes;
  115.     }
  116.     /**
  117.      * {@inheritdoc}
  118.      */
  119.     public function setAttributes(array $attributes)
  120.     {
  121.         $this->attributes = $attributes;
  122.     }
  123.     /**
  124.      * {@inheritdoc}
  125.      */
  126.     public function hasAttribute(string $name): bool
  127.     {
  128.         return \array_key_exists($name, $this->attributes);
  129.     }
  130.     /**
  131.      * {@inheritdoc}
  132.      */
  133.     public function getAttribute(string $name): mixed
  134.     {
  135.         if (!\array_key_exists($name, $this->attributes)) {
  136.             throw new \InvalidArgumentException(sprintf('This token has no "%s" attribute.', $name));
  137.         }
  138.         return $this->attributes[$name];
  139.     }
  140.     /**
  141.      * {@inheritdoc}
  142.      */
  143.     public function setAttribute(string $name, mixed $value)
  144.     {
  145.         $this->attributes[$name] = $value;
  146.     }
  147.     public function __toString(): string
  148.     {
  149.         $class = static::class;
  150.         $class = substr($class, strrpos($class, '\\') + 1);
  151.         $roles = [];
  152.         foreach ($this->roleNames as $role) {
  153.             $roles[] = $role;
  154.         }
  155.         return sprintf('%s(user="%s", roles="%s")', $class, $this->getUserIdentifier(), implode(', ', $roles));
  156.     }
  157.     /**
  158.      * @internal
  159.      */
  160.     final public function serialize(): string
  161.     {
  162.         throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
  163.     }
  164.     /**
  165.      * @internal
  166.      */
  167.     final public function unserialize(string $serialized)
  168.     {
  169.         $this->__unserialize(unserialize($serialized));
  170.     }
  171. }