summaryrefslogtreecommitdiffstats
path: root/Core
diff options
context:
space:
mode:
authorJohannes Schmitt <schmittjoh@gmail.com>2011-02-11 01:07:59 +0100
committerFabien Potencier <fabien.potencier@gmail.com>2011-02-12 21:53:04 +0100
commitd2c92956f4fcc7c980363f369b5f8bd545539eb5 (patch)
tree46012ea8f72f7583d656c5cbccff487e327f7508 /Core
parent0c9e3edd50a02b8ac80f5ba9165df792b28575c7 (diff)
downloadsymfony-security-d2c92956f4fcc7c980363f369b5f8bd545539eb5.zip
symfony-security-d2c92956f4fcc7c980363f369b5f8bd545539eb5.tar.gz
symfony-security-d2c92956f4fcc7c980363f369b5f8bd545539eb5.tar.bz2
[Security] Refactored security context, moved getUser() implementation to templating
Diffstat (limited to 'Core')
-rw-r--r--Core/Authentication/Token/Token.php4
-rw-r--r--Core/SecurityContext.php30
-rw-r--r--Core/SecurityContextInterface.php21
3 files changed, 30 insertions, 25 deletions
diff --git a/Core/Authentication/Token/Token.php b/Core/Authentication/Token/Token.php
index 1efa5d6..41b9f67 100644
--- a/Core/Authentication/Token/Token.php
+++ b/Core/Authentication/Token/Token.php
@@ -85,9 +85,7 @@ abstract class Token implements TokenInterface
*/
public function __toString()
{
- if (is_string($this->user)) {
- return $this->user;
- } else if ($this->user instanceof AccountInterface) {
+ if ($this->user instanceof AccountInterface) {
return $this->user->getUsername();
}
diff --git a/Core/SecurityContext.php b/Core/SecurityContext.php
index 405ace9..079636a 100644
--- a/Core/SecurityContext.php
+++ b/Core/SecurityContext.php
@@ -11,6 +11,8 @@
namespace Symfony\Component\Security\Core;
+use Symfony\Component\Security\Core\User\AccountInterface;
+use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
@@ -22,13 +24,10 @@ use Symfony\Component\Security\Acl\Voter\FieldVote;
* It gives access to the token representing the current user authentication.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ * @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
-class SecurityContext
+class SecurityContext implements SecurityContextInterface
{
- const ACCESS_DENIED_ERROR = '_security.403_error';
- const AUTHENTICATION_ERROR = '_security.last_error';
- const LAST_USERNAME = '_security.last_username';
-
protected $token;
protected $accessDecisionManager;
protected $authenticationManager;
@@ -39,30 +38,17 @@ class SecurityContext
*
* @param AccessDecisionManagerInterface|null $accessDecisionManager An AccessDecisionManager instance
*/
- public function __construct(AuthenticationManagerInterface $authenticationManager, AccessDecisionManagerInterface $accessDecisionManager = null, $alwaysAuthenticate = false)
+ public function __construct(AuthenticationManagerInterface $authenticationManager, AccessDecisionManagerInterface $accessDecisionManager, $alwaysAuthenticate = false)
{
$this->authenticationManager = $authenticationManager;
$this->accessDecisionManager = $accessDecisionManager;
$this->alwaysAuthenticate = $alwaysAuthenticate;
}
- public function getUser()
- {
- return null === $this->token ? null : $this->token->getUser();
- }
-
- public function vote($attributes, $object = null, $field = null)
+ public final function vote($attributes, $object = null)
{
- if (null === $this->token || null === $this->accessDecisionManager) {
- return false;
- }
-
- if ($field !== null) {
- if (null === $object) {
- throw new \InvalidArgumentException('$object cannot be null when field is not null.');
- }
-
- $object = new FieldVote($object, $field);
+ if (null === $this->token) {
+ throw new AuthenticationCredentialsNotFoundException('The security context contains no authentication token.');
}
if ($this->alwaysAuthenticate || !$this->token->isAuthenticated()) {
diff --git a/Core/SecurityContextInterface.php b/Core/SecurityContextInterface.php
new file mode 100644
index 0000000..fd205d6
--- /dev/null
+++ b/Core/SecurityContextInterface.php
@@ -0,0 +1,21 @@
+<?php
+
+namespace Symfony\Component\Security\Core;
+
+use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
+
+/**
+ * The SecurityContextInterface.
+ *
+ * @author Johannes M. Schmitt <schmittjoh@gmail.com>
+ */
+interface SecurityContextInterface
+{
+ const ACCESS_DENIED_ERROR = '_security.403_error';
+ const AUTHENTICATION_ERROR = '_security.last_error';
+ const LAST_USERNAME = '_security.last_username';
+
+ function getToken();
+ function setToken(TokenInterface $account);
+ function vote($attributes, $object = null);
+} \ No newline at end of file