PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/extensions/doctrine/vendors/Symfony/Component/Security/Core/Authentication/Token/AbstractToken.php

https://bitbucket.org/NordLabs/yiidoctrine
PHP | 223 lines | 121 code | 27 blank | 75 comment | 13 complexity | 320abf2020d6b3be9e3f0ccc436a2db7 MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.1, BSD-3-Clause
  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\Role\RoleInterface;
  12. use Symfony\Component\Security\Core\Role\Role;
  13. use Symfony\Component\Security\Core\User\UserInterface;
  14. /**
  15. * Base class for Token instances.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  19. */
  20. abstract class AbstractToken implements TokenInterface
  21. {
  22. private $user;
  23. private $roles;
  24. private $authenticated;
  25. private $attributes;
  26. /**
  27. * Constructor.
  28. *
  29. * @param Role[] $roles An array of roles
  30. */
  31. public function __construct(array $roles = array())
  32. {
  33. $this->authenticated = false;
  34. $this->attributes = array();
  35. $this->roles = array();
  36. foreach ($roles as $role) {
  37. if (is_string($role)) {
  38. $role = new Role($role);
  39. } elseif (!$role instanceof RoleInterface) {
  40. throw new \InvalidArgumentException(sprintf('$roles must be an array of strings, or RoleInterface instances, but got %s.', gettype($role)));
  41. }
  42. $this->roles[] = $role;
  43. }
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function getRoles()
  49. {
  50. return $this->roles;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function getUsername()
  56. {
  57. if ($this->user instanceof UserInterface) {
  58. return $this->user->getUsername();
  59. }
  60. return (string) $this->user;
  61. }
  62. public function getUser()
  63. {
  64. return $this->user;
  65. }
  66. public function setUser($user)
  67. {
  68. if (!($user instanceof UserInterface || (is_object($user) && method_exists($user, '__toString')) || is_string($user))) {
  69. throw new \InvalidArgumentException('$user must be an instanceof of UserInterface, an object implementing a __toString method, or a primitive string.');
  70. }
  71. if (null === $this->user) {
  72. $changed = false;
  73. } elseif ($this->user instanceof UserInterface) {
  74. if (!$user instanceof UserInterface) {
  75. $changed = true;
  76. } else {
  77. $changed = !$this->user->equals($user);
  78. }
  79. } elseif ($user instanceof UserInterface) {
  80. $changed = true;
  81. } else {
  82. $changed = (string) $this->user !== (string) $user;
  83. }
  84. if ($changed) {
  85. $this->setAuthenticated(false);
  86. }
  87. $this->user = $user;
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function isAuthenticated()
  93. {
  94. return $this->authenticated;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function setAuthenticated($authenticated)
  100. {
  101. $this->authenticated = (Boolean) $authenticated;
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function eraseCredentials()
  107. {
  108. if ($this->getUser() instanceof UserInterface) {
  109. $this->getUser()->eraseCredentials();
  110. }
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function serialize()
  116. {
  117. return serialize(array($this->user, $this->authenticated, $this->roles, $this->attributes));
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. public function unserialize($serialized)
  123. {
  124. list($this->user, $this->authenticated, $this->roles, $this->attributes) = unserialize($serialized);
  125. }
  126. /**
  127. * Returns the token attributes.
  128. *
  129. * @return array The token attributes
  130. */
  131. public function getAttributes()
  132. {
  133. return $this->attributes;
  134. }
  135. /**
  136. * Sets the token attributes.
  137. *
  138. * @param array $attributes The token attributes
  139. */
  140. public function setAttributes(array $attributes)
  141. {
  142. $this->attributes = $attributes;
  143. }
  144. /**
  145. * Returns true if the attribute exists.
  146. *
  147. * @param string $name The attribute name
  148. *
  149. * @return Boolean true if the attribute exists, false otherwise
  150. */
  151. public function hasAttribute($name)
  152. {
  153. return array_key_exists($name, $this->attributes);
  154. }
  155. /**
  156. * Returns a attribute value.
  157. *
  158. * @param string $name The attribute name
  159. *
  160. * @return mixed The attribute value
  161. *
  162. * @throws \InvalidArgumentException When attribute doesn't exist for this token
  163. */
  164. public function getAttribute($name)
  165. {
  166. if (!array_key_exists($name, $this->attributes)) {
  167. throw new \InvalidArgumentException(sprintf('This token has no "%s" attribute.', $name));
  168. }
  169. return $this->attributes[$name];
  170. }
  171. /**
  172. * Sets a attribute.
  173. *
  174. * @param string $name The attribute name
  175. * @param mixed $value The attribute value
  176. */
  177. public function setAttribute($name, $value)
  178. {
  179. $this->attributes[$name] = $value;
  180. }
  181. /**
  182. * {@inheritDoc}
  183. */
  184. public function __toString()
  185. {
  186. $class = get_class($this);
  187. $class = substr($class, strrpos($class, '\\')+1);
  188. $roles = array();
  189. foreach ($this->roles as $role) {
  190. $roles[] = $role->getRole();
  191. }
  192. return sprintf('%s(user="%s", authenticated=%s, roles="%s")', $class, $this->getUsername(), json_encode($this->authenticated), implode(', ', $roles));
  193. }
  194. }