Skip to content

Commit

Permalink
Add custom UserChecker
Browse files Browse the repository at this point in the history
  • Loading branch information
0x46616c6b committed Dec 28, 2023
1 parent 6316930 commit 423000b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ security:
security: false
default:
pattern: ^/
user_checker: App\Security\UserChecker
form_login:
enable_csrf: true
require_previous_session: false
Expand Down
33 changes: 33 additions & 0 deletions src/Security/UserChecker.php
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;

Check warning on line 15 in src/Security/UserChecker.php

View check run for this annotation

Codecov / codecov/patch

src/Security/UserChecker.php#L15

Added line #L15 was not covered by tests
}

if ($user->isDeleted()) {
throw new CustomUserMessageAccountStatusException('Your user account is deleted.');
}
}

Check warning on line 21 in src/Security/UserChecker.php

View check run for this annotation

Codecov / codecov/patch

src/Security/UserChecker.php#L21

Added line #L21 was not covered by tests

public function checkPostAuth(UserInterface $user): void
{
if (!$user instanceof User) {
return;

Check warning on line 26 in src/Security/UserChecker.php

View check run for this annotation

Codecov / codecov/patch

src/Security/UserChecker.php#L26

Added line #L26 was not covered by tests
}

if (!$user->isEnabled()) {
throw new CustomUserMessageAccountStatusException('Your user account is disabled.');
}
}

Check warning on line 32 in src/Security/UserChecker.php

View check run for this annotation

Codecov / codecov/patch

src/Security/UserChecker.php#L32

Added line #L32 was not covered by tests
}
33 changes: 33 additions & 0 deletions tests/Security/UserCheckerTest.php
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);
}
}

0 comments on commit 423000b

Please sign in to comment.