Skip to content

[Security] fixed pre/post authentication checks #9902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 31, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions src/Symfony/Component/Security/Core/User/UserChecker.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
}
}