diff options
author | Jeremy Mikola <jmikola@gmail.com> | 2011-02-14 15:32:23 -0500 |
---|---|---|
committer | Fabien Potencier <fabien.potencier@gmail.com> | 2011-02-15 21:50:02 +0100 |
commit | 2be9415df2f7cb7d6cc142068473a0ace178b1f6 (patch) | |
tree | 12185c5757e853d10368a6e6c4aac8cb2c615f04 /Core/Authentication/Token | |
parent | ea1f579363177d2145b828592044a1464eeee549 (diff) | |
download | symfony-security-2be9415df2f7cb7d6cc142068473a0ace178b1f6.zip symfony-security-2be9415df2f7cb7d6cc142068473a0ace178b1f6.tar.gz symfony-security-2be9415df2f7cb7d6cc142068473a0ace178b1f6.tar.bz2 |
[Security] Allow authentication tokens to hold attributes
Diffstat (limited to 'Core/Authentication/Token')
-rw-r--r-- | Core/Authentication/Token/Token.php | 67 | ||||
-rw-r--r-- | Core/Authentication/Token/TokenInterface.php | 42 |
2 files changed, 107 insertions, 2 deletions
diff --git a/Core/Authentication/Token/Token.php b/Core/Authentication/Token/Token.php index 41b9f67..3db6472 100644 --- a/Core/Authentication/Token/Token.php +++ b/Core/Authentication/Token/Token.php @@ -29,6 +29,7 @@ abstract class Token implements TokenInterface protected $credentials; protected $immutable; protected $providerKey; + protected $attributes; /** * Constructor. @@ -40,6 +41,7 @@ abstract class Token implements TokenInterface $this->setRoles($roles); $this->authenticated = false; $this->immutable = false; + $this->attributes = array(); } /** @@ -193,7 +195,7 @@ abstract class Token implements TokenInterface */ public function serialize() { - return serialize(array($this->user, $this->credentials, $this->authenticated, $this->roles, $this->immutable, $this->providerKey)); + return serialize(array($this->user, $this->credentials, $this->authenticated, $this->roles, $this->immutable, $this->providerKey, $this->attributes)); } /** @@ -201,6 +203,67 @@ abstract class Token implements TokenInterface */ public function unserialize($serialized) { - list($this->user, $this->credentials, $this->authenticated, $this->roles, $this->immutable, $this->providerKey) = unserialize($serialized); + list($this->user, $this->credentials, $this->authenticated, $this->roles, $this->immutable, $this->providerKey, $this->attributes) = unserialize($serialized); + } + + /** + * Returns the token attributes. + * + * @return array The token attributes + */ + public function getAttributes() + { + return $this->attributes; + } + + /** + * Sets the token attributes. + * + * @param array $attributes The token attributes + */ + public function setAttributes(array $attributes) + { + $this->attributes = $attributes; + } + + /** + * Returns true if the attribute exists. + * + * @param string $name The attribute name + * + * @return Boolean true if the attribute exists, false otherwise + */ + public function hasAttribute($name) + { + return array_key_exists($name, $this->attributes); + } + + /** + * Returns a attribute value. + * + * @param string $name The attribute name + * + * @return mixed The attribute value + * + * @throws \InvalidArgumentException When attribute doesn't exist for this token + */ + public function getAttribute($name) + { + if (!array_key_exists($name, $this->attributes)) { + throw new \InvalidArgumentException(sprintf('This token has no "%s" attribute.', $name)); + } + + return $this->attributes[$name]; + } + + /** + * Sets a attribute. + * + * @param string $name The attribute name + * @param mixed $value The attribute value + */ + public function setAttribute($name, $value) + { + $this->attributes[$name] = $value; } } diff --git a/Core/Authentication/Token/TokenInterface.php b/Core/Authentication/Token/TokenInterface.php index b6ac31c..cce35cc 100644 --- a/Core/Authentication/Token/TokenInterface.php +++ b/Core/Authentication/Token/TokenInterface.php @@ -99,4 +99,46 @@ interface TokenInterface extends \Serializable * Removes sensitive information from the token. */ function eraseCredentials(); + + /** + * Returns the token attributes. + * + * @return array The token attributes + */ + function getAttributes(); + + /** + * Sets the token attributes. + * + * @param array $attributes The token attributes + */ + function setAttributes(array $attributes); + + /** + * Returns true if the attribute exists. + * + * @param string $name The attribute name + * + * @return Boolean true if the attribute exists, false otherwise + */ + function hasAttribute($name); + + /** + * Returns a attribute value. + * + * @param string $name The attribute name + * + * @return mixed The attribute value + * + * @throws \InvalidArgumentException When attribute doesn't exist for this token + */ + function getAttribute($name); + + /** + * Sets a attribute. + * + * @param string $name The attribute name + * @param mixed $value The attribute value + */ + function setAttribute($name, $value); } |