-
Notifications
You must be signed in to change notification settings - Fork 0
feat(security): reject pending and blocked users at login (#63) #88
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
Open
martinydeAI
wants to merge
3
commits into
feature/issue-45-user-entity-extension
Choose a base branch
from
feature/issue-63-account-status-checker
base: feature/issue-45-user-entity-extension
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,65 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Security; | ||
|
|
||
| use App\Entity\User; | ||
| use App\Enum\UserStatus; | ||
| use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
| use Symfony\Component\Security\Core\Exception\CustomUserMessageAccountStatusException; | ||
| use Symfony\Component\Security\Core\User\UserCheckerInterface; | ||
| use Symfony\Component\Security\Core\User\UserInterface; | ||
|
|
||
| /** | ||
| * Reject login attempts for any {@see User} whose {@see UserStatus} is | ||
| * not {@see UserStatus::Approved}. | ||
| * | ||
| * Wired on the `main` firewall via `security.yaml`'s `user_checker:` | ||
| * key. Symfony Security calls {@see self::checkPreAuth()} before the | ||
| * password is verified; throwing a | ||
| * {@see CustomUserMessageAccountStatusException} halts the flow and | ||
| * surfaces the (localised) translation key on the login form. | ||
| * | ||
| * A `Blocked` user retains the roles | ||
| * they had before being blocked — they just can't sign in to exercise | ||
| * them. | ||
| */ | ||
| final class AccountStatusChecker implements UserCheckerInterface | ||
| { | ||
| /** | ||
| * Refuse pending and blocked users before the password is checked. | ||
| * | ||
| * Non-`User` implementations fall through (the password checker | ||
| * will reject them on its own terms). | ||
| * | ||
| * @param UserInterface $user the user attempting to authenticate | ||
| * @param TokenInterface|null $token unused; Symfony 8 added the slot for hooks that need it | ||
| * | ||
| * @throws CustomUserMessageAccountStatusException when status is Pending or Blocked | ||
| */ | ||
| public function checkPreAuth(UserInterface $user, ?TokenInterface $token = null): void | ||
| { | ||
| if (!$user instanceof User) { | ||
| return; | ||
| } | ||
|
|
||
| // Keys live in the `security` translation domain — see | ||
| // translations/security.da.yaml. | ||
| match ($user->getStatus()) { | ||
| UserStatus::Pending => throw new CustomUserMessageAccountStatusException('account.pending'), | ||
| UserStatus::Blocked => throw new CustomUserMessageAccountStatusException('account.blocked'), | ||
| UserStatus::Approved => null, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Post-auth hook required by the interface; no checks needed here. | ||
| * | ||
| * @param UserInterface $user the user that just authenticated successfully | ||
| * @param TokenInterface|null $token unused; Symfony 8 added the slot for hooks that need it | ||
| */ | ||
| public function checkPostAuth(UserInterface $user, ?TokenInterface $token = null): void | ||
| { | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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,74 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Tests\Unit\Security; | ||
|
|
||
| use App\Entity\User; | ||
| use App\Enum\UserStatus; | ||
| use App\Security\AccountStatusChecker; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Symfony\Component\Security\Core\Exception\CustomUserMessageAccountStatusException; | ||
| use Symfony\Component\Security\Core\User\UserInterface; | ||
|
|
||
| final class AccountStatusCheckerTest extends TestCase | ||
|
martinyde marked this conversation as resolved.
|
||
| { | ||
| // Tests that an Approved user passes the pre-auth hook without raising. | ||
| public function testApprovedUserPassesPreAuth(): void | ||
| { | ||
| $user = (new User()) | ||
| ->setName('Alice') | ||
| ->setStatus(UserStatus::Approved); | ||
|
|
||
| (new AccountStatusChecker())->checkPreAuth($user); | ||
|
|
||
| // No exception thrown is the assertion; explicit to keep PHPUnit happy. | ||
| self::assertTrue(true); | ||
| } | ||
|
|
||
| // Ensures a Pending user is rejected with the 'account.pending' message key. | ||
| public function testPendingUserIsRejectedWithLocalisedMessage(): void | ||
| { | ||
| $user = (new User()) | ||
| ->setName('Pending') | ||
| ->setStatus(UserStatus::Pending); | ||
|
|
||
| $this->expectException(CustomUserMessageAccountStatusException::class); | ||
| $this->expectExceptionMessage('account.pending'); | ||
|
|
||
| (new AccountStatusChecker())->checkPreAuth($user); | ||
| } | ||
|
|
||
| // Ensures a Blocked user is rejected with the 'account.blocked' message key. | ||
| public function testBlockedUserIsRejectedWithLocalisedMessage(): void | ||
| { | ||
| $user = (new User()) | ||
| ->setName('Blocked') | ||
| ->setStatus(UserStatus::Blocked); | ||
|
|
||
| $this->expectException(CustomUserMessageAccountStatusException::class); | ||
| $this->expectExceptionMessage('account.blocked'); | ||
|
|
||
| (new AccountStatusChecker())->checkPreAuth($user); | ||
| } | ||
|
|
||
| // Verifies non-App User implementations fall through to the password checker. | ||
| public function testForeignUserImplementationsAreIgnored(): void | ||
| { | ||
| $foreignUser = $this->createMock(UserInterface::class); | ||
|
|
||
| (new AccountStatusChecker())->checkPreAuth($foreignUser); | ||
|
|
||
| self::assertTrue(true); | ||
| } | ||
|
|
||
| // Tests that checkPostAuth does nothing (required by the interface). | ||
| public function testCheckPostAuthIsANoOp(): void | ||
| { | ||
| $user = (new User())->setStatus(UserStatus::Approved); | ||
|
|
||
| (new AccountStatusChecker())->checkPostAuth($user); | ||
|
|
||
| self::assertTrue(true); | ||
| } | ||
| } | ||
This file contains hidden or 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,10 @@ | ||
| # Danish strings rendered in the `security` translation domain. | ||
| # | ||
| # Symfony Security's CustomUserMessageAccountStatusException carries a | ||
| # `messageKey` that the login template renders as | ||
| # `error.messageKey|trans(error.messageData, 'security')`. The keys | ||
| # below match the messageKeys thrown by App\Security\AccountStatusChecker. | ||
|
|
||
| account: | ||
| pending: "Din konto venter på godkendelse fra en administrator." | ||
| blocked: "Din konto er spærret. Kontakt en administrator for at få den åbnet igen." |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we strip roles from blocked users for good measure, or is that bad practice?