vendor/symfony/security-core/Authentication/Token/SwitchUserToken.php line 21

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\UserInterface;
  12. /**
  13. * Token representing a user who temporarily impersonates another one.
  14. *
  15. * @author Christian Flothmann <christian.flothmann@sensiolabs.de>
  16. */
  17. class SwitchUserToken extends UsernamePasswordToken
  18. {
  19. private $originalToken;
  20. private $originatedFromUri;
  21. /**
  22. * @param UserInterface $user
  23. * @param string|null $originatedFromUri The URI where was the user at the switch
  24. *
  25. * @throws \InvalidArgumentException
  26. */
  27. public function __construct($user, /* string */ $firewallName, /* array */ $roles, /* TokenInterface */ $originalToken, /* string */ $originatedFromUri = null)
  28. {
  29. if (\is_string($roles)) {
  30. // @deprecated since 5.4, deprecation is triggered by UsernamePasswordToken::__construct()
  31. $credentials = $firewallName;
  32. $firewallName = $roles;
  33. $roles = $originalToken;
  34. $originalToken = $originatedFromUri;
  35. $originatedFromUri = \func_num_args() > 5 ? func_get_arg(5) : null;
  36. parent::__construct($user, $credentials, $firewallName, $roles);
  37. } else {
  38. parent::__construct($user, $firewallName, $roles);
  39. }
  40. if (!$originalToken instanceof TokenInterface) {
  41. throw new \TypeError(sprintf('Argument $originalToken of "%s" must be an instance of "%s", "%s" given.', __METHOD__, TokenInterface::class, get_debug_type($originalToken)));
  42. }
  43. $this->originalToken = $originalToken;
  44. $this->originatedFromUri = $originatedFromUri;
  45. }
  46. public function getOriginalToken(): TokenInterface
  47. {
  48. return $this->originalToken;
  49. }
  50. public function getOriginatedFromUri(): ?string
  51. {
  52. return $this->originatedFromUri;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function __serialize(): array
  58. {
  59. return [$this->originalToken, $this->originatedFromUri, parent::__serialize()];
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function __unserialize(array $data): void
  65. {
  66. if (3 > \count($data)) {
  67. // Support for tokens serialized with version 5.1 or lower of symfony/security-core.
  68. [$this->originalToken, $parentData] = $data;
  69. } else {
  70. [$this->originalToken, $this->originatedFromUri, $parentData] = $data;
  71. }
  72. $parentData = \is_array($parentData) ? $parentData : unserialize($parentData);
  73. parent::__unserialize($parentData);
  74. }
  75. }