RunAsUserToken.php 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /*
  3. * Copyright 2010 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\SecurityExtraBundle\Security\Authentication\Token;
  18. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  19. use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
  20. /**
  21. * This token is automatically generated by the RunAsManager when an invocation
  22. * is supposed to be run with a different Token.
  23. *
  24. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  25. */
  26. class RunAsUserToken extends AbstractToken
  27. {
  28. private $originalToken;
  29. private $key;
  30. private $credentials;
  31. public function __construct($key, $user, $credentials, array $roles, TokenInterface $originalToken)
  32. {
  33. parent::__construct($roles);
  34. $this->originalToken = $originalToken;
  35. $this->credentials = $credentials;
  36. $this->key = $key;
  37. $this->setUser($user);
  38. $this->setAuthenticated(true);
  39. }
  40. public function getKey()
  41. {
  42. return $this->key;
  43. }
  44. public function getOriginalToken()
  45. {
  46. return $this->originalToken;
  47. }
  48. public function getCredentials()
  49. {
  50. return $this->credentials;
  51. }
  52. public function eraseCredentials()
  53. {
  54. parent::eraseCredentials();
  55. $this->credentials = null;
  56. }
  57. public function serialize()
  58. {
  59. return serialize(array(
  60. $this->originalToken,
  61. $this->key,
  62. $this->credentials,
  63. parent::serialize(),
  64. ));
  65. }
  66. public function unserialize($str)
  67. {
  68. list($this->originalToken, $this->key, $this->credentials, $parentStr) = unserialize($str);
  69. parent::unserialize($parentStr);
  70. }
  71. }