-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6316930
commit 423000b
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace App\Security; | ||
|
||
use App\Entity\User; | ||
use Symfony\Component\Security\Core\Exception\CustomUserMessageAccountStatusException; | ||
use Symfony\Component\Security\Core\User\UserCheckerInterface; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
class UserChecker implements UserCheckerInterface | ||
{ | ||
public function checkPreAuth(UserInterface $user): void | ||
{ | ||
if (!$user instanceof User) { | ||
return; | ||
} | ||
|
||
if ($user->isDeleted()) { | ||
throw new CustomUserMessageAccountStatusException('Your user account is deleted.'); | ||
} | ||
} | ||
|
||
public function checkPostAuth(UserInterface $user): void | ||
{ | ||
if (!$user instanceof User) { | ||
return; | ||
} | ||
|
||
if (!$user->isEnabled()) { | ||
throw new CustomUserMessageAccountStatusException('Your user account is disabled.'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace App\Tests\Security; | ||
|
||
use App\Entity\User; | ||
use App\Security\UserChecker; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Security\Core\Exception\CustomUserMessageAccountStatusException; | ||
|
||
class UserCheckerTest extends TestCase | ||
{ | ||
public function testCheckPreAuth(): void | ||
{ | ||
$user = new User(); | ||
$user->setDeleted(true); | ||
|
||
$checker = new UserChecker(); | ||
$this->expectException(CustomUserMessageAccountStatusException::class); | ||
$this->expectExceptionMessage('Your user account is deleted.'); | ||
$checker->checkPreAuth($user); | ||
} | ||
|
||
public function testCheckPostAuth(): void | ||
{ | ||
$user = new User(); | ||
$user->setEnabled(false); | ||
|
||
$checker = new UserChecker(); | ||
$this->expectException(CustomUserMessageAccountStatusException::class); | ||
$this->expectExceptionMessage('Your user account is disabled.'); | ||
$checker->checkPostAuth($user); | ||
} | ||
} |