PostAuthenticationGuardToken.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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\Guard\Token;
  11. use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
  12. use Symfony\Component\Security\Core\Role\RoleInterface;
  13. use Symfony\Component\Security\Core\User\UserInterface;
  14. /**
  15. * Used as an "authenticated" token, though it could be set to not-authenticated later.
  16. *
  17. * If you're using Guard authentication, you *must* use a class that implements
  18. * GuardTokenInterface as your authenticated token (like this class).
  19. *
  20. * @author Ryan Weaver <ryan@knpuniversity.com>
  21. */
  22. class PostAuthenticationGuardToken extends AbstractToken implements GuardTokenInterface
  23. {
  24. private $providerKey;
  25. /**
  26. * @param UserInterface $user The user!
  27. * @param string $providerKey The provider (firewall) key
  28. * @param RoleInterface[]|string[] $roles An array of roles
  29. *
  30. * @throws \InvalidArgumentException
  31. */
  32. public function __construct(UserInterface $user, $providerKey, array $roles)
  33. {
  34. parent::__construct($roles);
  35. if (empty($providerKey)) {
  36. throw new \InvalidArgumentException('$providerKey (i.e. firewall key) must not be empty.');
  37. }
  38. $this->setUser($user);
  39. $this->providerKey = $providerKey;
  40. // this token is meant to be used after authentication success, so it is always authenticated
  41. // you could set it as non authenticated later if you need to
  42. parent::setAuthenticated(true);
  43. }
  44. /**
  45. * This is meant to be only an authenticated token, where credentials
  46. * have already been used and are thus cleared.
  47. *
  48. * {@inheritdoc}
  49. */
  50. public function getCredentials()
  51. {
  52. return array();
  53. }
  54. /**
  55. * Returns the provider (firewall) key.
  56. *
  57. * @return string
  58. */
  59. public function getProviderKey()
  60. {
  61. return $this->providerKey;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function serialize()
  67. {
  68. return serialize(array($this->providerKey, parent::serialize()));
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function unserialize($serialized)
  74. {
  75. list($this->providerKey, $parentStr) = unserialize($serialized);
  76. parent::unserialize($parentStr);
  77. }
  78. }