SwitchUserToken.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. /**
  12. * Token representing a user who temporarily impersonates another one.
  13. *
  14. * @author Christian Flothmann <christian.flothmann@sensiolabs.de>
  15. */
  16. class SwitchUserToken extends UsernamePasswordToken
  17. {
  18. private $originalToken;
  19. private $originatedFromUri;
  20. /**
  21. * @param string|object $user The username (like a nickname, email address, etc.), or a UserInterface instance or an object implementing a __toString method
  22. * @param mixed $credentials This usually is the password of the 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, $credentials, string $firewallName, array $roles, TokenInterface $originalToken, string $originatedFromUri = null)
  28. {
  29. parent::__construct($user, $credentials, $firewallName, $roles);
  30. $this->originalToken = $originalToken;
  31. $this->originatedFromUri = $originatedFromUri;
  32. }
  33. public function getOriginalToken(): TokenInterface
  34. {
  35. return $this->originalToken;
  36. }
  37. public function getOriginatedFromUri(): ?string
  38. {
  39. return $this->originatedFromUri;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function __serialize(): array
  45. {
  46. return [$this->originalToken, $this->originatedFromUri, parent::__serialize()];
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function __unserialize(array $data): void
  52. {
  53. if (3 > \count($data)) {
  54. // Support for tokens serialized with version 5.1 or lower of symfony/security-core.
  55. [$this->originalToken, $parentData] = $data;
  56. } else {
  57. [$this->originalToken, $this->originatedFromUri, $parentData] = $data;
  58. }
  59. $parentData = \is_array($parentData) ? $parentData : unserialize($parentData);
  60. parent::__unserialize($parentData);
  61. }
  62. }