summaryrefslogtreecommitdiffstats
path: root/Core/Authentication/Token/AbstractToken.php
diff options
context:
space:
mode:
authorJohannes Schmitt <schmittjoh@gmail.com>2011-03-07 18:17:46 +0100
committerJohannes M. Schmitt <schmittjoh@gmail.com>2011-03-10 10:25:32 +0100
commitf0335ae722034233c2f49179bc6a9bf8ada62633 (patch)
tree677ee84bc31216f3a7998e62fdc7838a2076fe4c /Core/Authentication/Token/AbstractToken.php
parentc224430de65547bc9a25293b6a8caf2b9029f05c (diff)
downloadsymfony-security-f0335ae722034233c2f49179bc6a9bf8ada62633.zip
symfony-security-f0335ae722034233c2f49179bc6a9bf8ada62633.tar.gz
symfony-security-f0335ae722034233c2f49179bc6a9bf8ada62633.tar.bz2
[Security] various changes, see below
- visibility changes from protected to private - AccountInterface -> UserInterface - SecurityContext::vote() -> SecurityContext::isGranted()
Diffstat (limited to 'Core/Authentication/Token/AbstractToken.php')
-rw-r--r--Core/Authentication/Token/AbstractToken.php203
1 files changed, 203 insertions, 0 deletions
diff --git a/Core/Authentication/Token/AbstractToken.php b/Core/Authentication/Token/AbstractToken.php
new file mode 100644
index 0000000..3839154
--- /dev/null
+++ b/Core/Authentication/Token/AbstractToken.php
@@ -0,0 +1,203 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Security\Core\Authentication\Token;
+
+use Symfony\Component\Security\Core\Role\RoleInterface;
+use Symfony\Component\Security\Core\Role\Role;
+use Symfony\Component\Security\Core\User\UserInterface;
+
+/**
+ * Base class for Token instances.
+ *
+ * @author Fabien Potencier <fabien@symfony.com>
+ * @author Johannes M. Schmitt <schmittjoh@gmail.com>
+ */
+abstract class AbstractToken implements TokenInterface
+{
+ private $user;
+ private $roles;
+ private $authenticated;
+ private $attributes;
+
+ /**
+ * Constructor.
+ *
+ * @param Role[] $roles An array of roles
+ */
+ public function __construct(array $roles = array())
+ {
+ $this->authenticated = false;
+ $this->attributes = array();
+
+ $this->roles = array();
+ foreach ($roles as $role) {
+ if (is_string($role)) {
+ $role = new Role($role);
+ } else if (!$role instanceof RoleInterface) {
+ throw new \InvalidArgumentException(sprintf('$roles must be an array of strings, or RoleInterface instances, but got %s.', gettype($role)));
+ }
+
+ $this->roles[] = $role;
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getRoles()
+ {
+ return $this->roles;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getUsername()
+ {
+ if ($this->user instanceof UserInterface) {
+ return $this->user->getUsername();
+ }
+
+ return (string) $this->user;
+ }
+
+ public function getUser()
+ {
+ return $this->user;
+ }
+
+ public function setUser($user)
+ {
+ if (!($user instanceof UserInterface || (is_object($user) && method_exists($user, '__toString')) || is_string($user))) {
+ throw new \InvalidArgumentException('$user must be an instanceof of UserInterface, an object implementing a __toString method, or a primitive string.');
+ }
+
+ if (null === $this->user) {
+ $changed = false;
+ } else if ($this->user instanceof UserInterface) {
+ $changed = $this->user->equals($user);
+ } else if ($user instanceof UserInterface) {
+ $changed = true;
+ } else {
+ $changed = (string) $this->user === (string) $user;
+ }
+
+ if ($changed) {
+ $this->setAuthenticated(false);
+ }
+
+ $this->user = $user;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function isAuthenticated()
+ {
+ return $this->authenticated;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function setAuthenticated($authenticated)
+ {
+ $this->authenticated = (Boolean) $authenticated;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function eraseCredentials()
+ {
+ if ($this->getUser() instanceof UserInterface) {
+ $this->getUser()->eraseCredentials();
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function serialize()
+ {
+ return serialize(array($this->user, $this->authenticated, $this->roles, $this->attributes));
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function unserialize($serialized)
+ {
+ list($this->user, $this->authenticated, $this->roles, $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;
+ }
+}