diff --git a/config/packages/security.yaml b/config/packages/security.yaml index 561f076b..0ac3c96a 100755 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -105,6 +105,7 @@ security: security: false default: pattern: ^/ + user_checker: App\Security\UserChecker form_login: enable_csrf: true require_previous_session: false diff --git a/src/Security/UserChecker.php b/src/Security/UserChecker.php new file mode 100644 index 00000000..36ab3cb7 --- /dev/null +++ b/src/Security/UserChecker.php @@ -0,0 +1,33 @@ +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.'); + } + } +} diff --git a/tests/Security/UserCheckerTest.php b/tests/Security/UserCheckerTest.php new file mode 100644 index 00000000..cde26211 --- /dev/null +++ b/tests/Security/UserCheckerTest.php @@ -0,0 +1,33 @@ +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); + } +}