Skip to content

[Security] Check account isn't locked before user authentication #9622

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

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function testCheckPreAuthPass()

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

$this->assertNull($checker->checkPreAuth($account));
}
Expand All @@ -45,6 +46,20 @@ public function testCheckPreAuthCredentialsExpired()
$checker->checkPreAuth($account);
}

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

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

$checker->checkPreAuth($account);
}

public function testCheckPostAuthNotAdvancedUserInterface()
{
$checker = new UserChecker();
Expand All @@ -57,26 +72,12 @@ public function testCheckPostAuthPass()
$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(true));
$account->expects($this->once())->method('isAccountNonExpired')->will($this->returnValue(true));

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

/**
* @expectedException \Symfony\Component\Security\Core\Exception\LockedException
*/
public function testCheckPostAuthAccountLocked()
{
$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);
}

/**
* @expectedException \Symfony\Component\Security\Core\Exception\DisabledException
*/
Expand All @@ -85,7 +86,6 @@ public function testCheckPostAuthDisabled()
$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);
Expand All @@ -99,7 +99,6 @@ public function testCheckPostAuthAccountExpired()
$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(true));
$account->expects($this->once())->method('isAccountNonExpired')->will($this->returnValue(false));

Expand Down
10 changes: 4 additions & 6 deletions src/Symfony/Component/Security/Core/User/UserChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function checkPreAuth(UserInterface $user)
$ex = new CredentialsExpiredException('User credentials have expired.');
$ex->setUser($user);
throw $ex;
} elseif (!$user->isAccountNonLocked()) {
$ex = new LockedException('User account is locked.');
$ex->setUser($user);
throw $ex;
}
}

Expand All @@ -48,12 +52,6 @@ public function checkPostAuth(UserInterface $user)
return;
}

if (!$user->isAccountNonLocked()) {
$ex = new LockedException('User account is locked.');
$ex->setUser($user);
throw $ex;
}

if (!$user->isEnabled()) {
$ex = new DisabledException('User account is disabled.');
$ex->setUser($user);
Expand Down