Skip to content

Commit

Permalink
[Security] fixed pre/post authentication checks
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Dec 31, 2013
1 parent 63d226d commit ada82a2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
32 changes: 16 additions & 16 deletions src/Symfony/Component/Security/Core/User/UserChecker.php
Expand Up @@ -32,22 +32,6 @@ public function checkPreAuth(UserInterface $user)
return;
}

if (!$user->isCredentialsNonExpired()) {
$ex = new CredentialsExpiredException('User credentials have expired.');
$ex->setUser($user);
throw $ex;
}
}

/**
* {@inheritdoc}
*/
public function checkPostAuth(UserInterface $user)
{
if (!$user instanceof AdvancedUserInterface) {
return;
}

if (!$user->isAccountNonLocked()) {
$ex = new LockedException('User account is locked.');
$ex->setUser($user);
Expand All @@ -66,4 +50,20 @@ public function checkPostAuth(UserInterface $user)
throw $ex;
}
}

/**
* {@inheritdoc}
*/
public function checkPostAuth(UserInterface $user)
{
if (!$user instanceof AdvancedUserInterface) {
return;
}

if (!$user->isCredentialsNonExpired()) {
$ex = new CredentialsExpiredException('User credentials have expired.');
$ex->setUser($user);
throw $ex;
}
}
}
32 changes: 16 additions & 16 deletions src/Symfony/Component/Security/Tests/Core/User/UserCheckerTest.php
Expand Up @@ -15,44 +15,44 @@

class UserCheckerTest extends \PHPUnit_Framework_TestCase
{
public function testCheckPreAuthNotAdvancedUserInterface()
public function testCheckPostAuthNotAdvancedUserInterface()
{
$checker = new UserChecker();

$this->assertNull($checker->checkPreAuth($this->getMock('Symfony\Component\Security\Core\User\UserInterface')));
$this->assertNull($checker->checkPostAuth($this->getMock('Symfony\Component\Security\Core\User\UserInterface')));
}

public function testCheckPreAuthPass()
public function testCheckPostAuthPass()
{
$checker = new UserChecker();

$account = $this->getMock('Symfony\Component\Security\Core\User\AdvancedUserInterface');
$account->expects($this->once())->method('isCredentialsNonExpired')->will($this->returnValue(true));

$this->assertNull($checker->checkPreAuth($account));
$this->assertNull($checker->checkPostAuth($account));
}

/**
* @expectedException \Symfony\Component\Security\Core\Exception\CredentialsExpiredException
*/
public function testCheckPreAuthCredentialsExpired()
public function testCheckPostAuthCredentialsExpired()
{
$checker = new UserChecker();

$account = $this->getMock('Symfony\Component\Security\Core\User\AdvancedUserInterface');
$account->expects($this->once())->method('isCredentialsNonExpired')->will($this->returnValue(false));

$checker->checkPreAuth($account);
$checker->checkPostAuth($account);
}

public function testCheckPostAuthNotAdvancedUserInterface()
public function testCheckPreAuthNotAdvancedUserInterface()
{
$checker = new UserChecker();

$this->assertNull($checker->checkPostAuth($this->getMock('Symfony\Component\Security\Core\User\UserInterface')));
$this->assertNull($checker->checkPreAuth($this->getMock('Symfony\Component\Security\Core\User\UserInterface')));
}

public function testCheckPostAuthPass()
public function testCheckPreAuthPass()
{
$checker = new UserChecker();

Expand All @@ -61,40 +61,40 @@ public function testCheckPostAuthPass()
$account->expects($this->once())->method('isEnabled')->will($this->returnValue(true));
$account->expects($this->once())->method('isAccountNonExpired')->will($this->returnValue(true));

$this->assertNull($checker->checkPostAuth($account));
$this->assertNull($checker->checkPreAuth($account));
}

/**
* @expectedException \Symfony\Component\Security\Core\Exception\LockedException
*/
public function testCheckPostAuthAccountLocked()
public function testCheckPreAuthAccountLocked()
{
$checker = new UserChecker();

$account = $this->getMock('Symfony\Component\Security\Core\User\AdvancedUserInterface');
$account->expects($this->once())->method('isAccountNonLocked')->will($this->returnValue(false));

$checker->checkPostAuth($account);
$checker->checkPreAuth($account);
}

/**
* @expectedException \Symfony\Component\Security\Core\Exception\DisabledException
*/
public function testCheckPostAuthDisabled()
public function testCheckPreAuthDisabled()
{
$checker = new UserChecker();

$account = $this->getMock('Symfony\Component\Security\Core\User\AdvancedUserInterface');
$account->expects($this->once())->method('isAccountNonLocked')->will($this->returnValue(true));
$account->expects($this->once())->method('isEnabled')->will($this->returnValue(false));

$checker->checkPostAuth($account);
$checker->checkPreAuth($account);
}

/**
* @expectedException \Symfony\Component\Security\Core\Exception\AccountExpiredException
*/
public function testCheckPostAuthAccountExpired()
public function testCheckPreAuthAccountExpired()
{
$checker = new UserChecker();

Expand All @@ -103,6 +103,6 @@ public function testCheckPostAuthAccountExpired()
$account->expects($this->once())->method('isEnabled')->will($this->returnValue(true));
$account->expects($this->once())->method('isAccountNonExpired')->will($this->returnValue(false));

$checker->checkPostAuth($account);
$checker->checkPreAuth($account);
}
}

0 comments on commit ada82a2

Please sign in to comment.