summaryrefslogtreecommitdiffstats
path: root/Core
diff options
context:
space:
mode:
Diffstat (limited to 'Core')
-rw-r--r--Core/Authorization/Voter/AbstractVoter.php26
-rw-r--r--Core/Tests/Authorization/Voter/AbstractVoterTest.php5
-rw-r--r--Core/Tests/User/InMemoryUserProviderTest.php33
-rw-r--r--Core/User/InMemoryUserProvider.php37
4 files changed, 59 insertions, 42 deletions
diff --git a/Core/Authorization/Voter/AbstractVoter.php b/Core/Authorization/Voter/AbstractVoter.php
index 71f570f..665d5f1 100644
--- a/Core/Authorization/Voter/AbstractVoter.php
+++ b/Core/Authorization/Voter/AbstractVoter.php
@@ -41,10 +41,9 @@ abstract class AbstractVoter implements VoterInterface
// abstain vote by default in case none of the attributes are supported
$vote = self::ACCESS_ABSTAIN;
- $class = get_class($object);
foreach ($attributes as $attribute) {
- if (!$this->supports($attribute, $class)) {
+ if (!$this->supports($attribute, $object)) {
continue;
}
@@ -61,33 +60,16 @@ abstract class AbstractVoter implements VoterInterface
}
/**
- * Determines if the attribute and class are supported by this voter.
- *
- * To determine if the passed class is instance of the supported class, the
- * isClassInstanceOf() method can be used.
+ * Determines if the attribute and object are supported by this voter.
*
* @param string $attribute An attribute
- * @param string $class The fully qualified class name of the passed object
+ * @param string $object The object to secure
*
- * @return bool True if the attribute and class is supported, false otherwise
+ * @return bool True if the attribute and object is supported, false otherwise
*/
abstract protected function supports($attribute, $class);
/**
- * A helper method to test if the actual class is instanceof or equal
- * to the expected class.
- *
- * @param string $actualClass The actual class name
- * @param string $expectedClass The expected class name
- *
- * @return bool
- */
- protected function isClassInstanceOf($actualClass, $expectedClass)
- {
- return $expectedClass === $actualClass || is_subclass_of($actualClass, $expectedClass);
- }
-
- /**
* Perform a single access check operation on a given attribute, object and token.
* It is safe to assume that $attribute and $object's class pass supports method call.
*
diff --git a/Core/Tests/Authorization/Voter/AbstractVoterTest.php b/Core/Tests/Authorization/Voter/AbstractVoterTest.php
index aafef5a..537dc4c 100644
--- a/Core/Tests/Authorization/Voter/AbstractVoterTest.php
+++ b/Core/Tests/Authorization/Voter/AbstractVoterTest.php
@@ -63,9 +63,8 @@ class AbstractVoterTest_Voter extends AbstractVoter
return 'EDIT' === $attribute;
}
- protected function supports($attribute, $class)
+ protected function supports($attribute, $object)
{
- return $this->isClassInstanceOf($class, 'stdClass')
- && in_array($attribute, array('EDIT', 'CREATE'));
+ return $object instanceof \stdClass && in_array($attribute, array('EDIT', 'CREATE'));
}
}
diff --git a/Core/Tests/User/InMemoryUserProviderTest.php b/Core/Tests/User/InMemoryUserProviderTest.php
index dfc4237..0a1815f 100644
--- a/Core/Tests/User/InMemoryUserProviderTest.php
+++ b/Core/Tests/User/InMemoryUserProviderTest.php
@@ -18,18 +18,39 @@ class InMemoryUserProviderTest extends \PHPUnit_Framework_TestCase
{
public function testConstructor()
{
- $provider = new InMemoryUserProvider(array(
+ $provider = $this->createProvider();
+
+ $user = $provider->loadUserByUsername('fabien');
+ $this->assertEquals('foo', $user->getPassword());
+ $this->assertEquals(array('ROLE_USER'), $user->getRoles());
+ $this->assertFalse($user->isEnabled());
+ }
+
+ public function testRefresh()
+ {
+ $user = new User('fabien', 'bar');
+
+ $provider = $this->createProvider();
+
+ $refreshedUser = $provider->refreshUser($user);
+ $this->assertEquals('foo', $refreshedUser->getPassword());
+ $this->assertEquals(array('ROLE_USER'), $refreshedUser->getRoles());
+ $this->assertFalse($refreshedUser->isEnabled());
+ $this->assertFalse($refreshedUser->isCredentialsNonExpired());
+ }
+
+ /**
+ * @return InMemoryUserProvider
+ */
+ protected function createProvider()
+ {
+ return new InMemoryUserProvider(array(
'fabien' => array(
'password' => 'foo',
'enabled' => false,
'roles' => array('ROLE_USER'),
),
));
-
- $user = $provider->loadUserByUsername('fabien');
- $this->assertEquals('foo', $user->getPassword());
- $this->assertEquals(array('ROLE_USER'), $user->getRoles());
- $this->assertFalse($user->isEnabled());
}
public function testCreateUser()
diff --git a/Core/User/InMemoryUserProvider.php b/Core/User/InMemoryUserProvider.php
index 624eb3d..9aa39ca 100644
--- a/Core/User/InMemoryUserProvider.php
+++ b/Core/User/InMemoryUserProvider.php
@@ -67,17 +67,9 @@ class InMemoryUserProvider implements UserProviderInterface
*/
public function loadUserByUsername($username)
{
- if (!isset($this->users[strtolower($username)])) {
- $ex = new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
- $ex->setUsername($username);
-
- throw $ex;
- }
+ $user = $this->getUser($username);
- $user = $this->users[strtolower($username)];
-
- return new User($user->getUsername(), $user->getPassword(), $user->getRoles(), $user->isEnabled(), $user->isAccountNonExpired(),
- $user->isCredentialsNonExpired(), $user->isAccountNonLocked());
+ return new User($user->getUsername(), $user->getPassword(), $user->getRoles(), $user->isEnabled(), $user->isAccountNonExpired(), $user->isCredentialsNonExpired(), $user->isAccountNonLocked());
}
/**
@@ -89,7 +81,9 @@ class InMemoryUserProvider implements UserProviderInterface
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
}
- return $this->loadUserByUsername($user->getUsername());
+ $storedUser = $this->getUser($user->getUsername());
+
+ return new User($storedUser->getUsername(), $storedUser->getPassword(), $storedUser->getRoles(), $storedUser->isEnabled(), $storedUser->isAccountNonExpired(), $storedUser->isCredentialsNonExpired() && $storedUser->getPassword() === $user->getPassword(), $storedUser->isAccountNonLocked());
}
/**
@@ -99,4 +93,25 @@ class InMemoryUserProvider implements UserProviderInterface
{
return $class === 'Symfony\Component\Security\Core\User\User';
}
+
+ /**
+ * Returns the user by given username.
+ *
+ * @param string $username The username.
+ *
+ * @return User
+ *
+ * @throws UsernameNotFoundException If user whose given username does not exist.
+ */
+ private function getUser($username)
+ {
+ if (!isset($this->users[strtolower($username)])) {
+ $ex = new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
+ $ex->setUsername($username);
+
+ throw $ex;
+ }
+
+ return $this->users[strtolower($username)];
+ }
}