How to Create and Enable Custom User Checkers

How to Create and Enable Custom User Checkers

During the authentication of a user, additional checks might be required to verify if the identified user is allowed to log in. By defining a custom user checker, you can define per firewall which checker should be used.

Creating a Custom User Checker

User checkers are classes that must implement the Symfony\Component\Security\Core\User\UserCheckerInterface. This interface defines two methods called checkPreAuth() and checkPostAuth() to perform checks before and after user authentication. If one or more conditions are not met, an exception should be thrown which extends the Symfony\Component\Security\Core\Exception\AccountStatusException or Symfony\Component\Security\Core\Exception\AuthenticationException:

  1. namespace App\Security;
  2. use App\Entity\User as AppUser;
  3. use App\Exception\AccountDeletedException;
  4. use Symfony\Component\Security\Core\Exception\AccountExpiredException;
  5. use Symfony\Component\Security\Core\User\UserCheckerInterface;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. class UserChecker implements UserCheckerInterface
  8. {
  9. public function checkPreAuth(UserInterface $user): void
  10. {
  11. if (!$user instanceof AppUser) {
  12. return;
  13. }
  14. // user is deleted, show a generic Account Not Found message.
  15. if ($user->isDeleted()) {
  16. throw new AccountDeletedException();
  17. }
  18. }
  19. public function checkPostAuth(UserInterface $user): void
  20. {
  21. if (!$user instanceof AppUser) {
  22. return;
  23. }
  24. // user account is expired, the user may be notified
  25. if ($user->isExpired()) {
  26. throw new AccountExpiredException('...');
  27. }
  28. }
  29. }

Enabling the Custom User Checker

Next, make sure your user checker is registered as a service. If you’re using the default services.yaml configuration, the service is registered automatically.

All that’s left to do is add the checker to the desired firewall where the value is the service id of your user checker:

  • YAML

    1. # config/packages/security.yaml
    2. # ...
    3. security:
    4. firewalls:
    5. main:
    6. pattern: ^/
    7. user_checker: App\Security\UserChecker
    8. # ...
  • XML

    1. <!-- config/packages/security.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <srv:container xmlns="http://symfony.com/schema/dic/security"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:srv="http://symfony.com/schema/dic/services"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd">
    8. <config>
    9. <!-- ... -->
    10. <firewall name="main"
    11. pattern="^/"
    12. user-checker="App\Security\UserChecker">
    13. <!-- ... -->
    14. </firewall>
    15. </config>
    16. </srv:container>
  • PHP

    1. // config/packages/security.php
    2. use App\Security\UserChecker;
    3. $container->loadFromExtension('security', [
    4. // ...
    5. 'firewalls' => [
    6. 'main' => [
    7. 'pattern' => '^/',
    8. 'user_checker' => UserChecker::class,
    9. // ...
    10. ],
    11. ],
    12. ]);

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.