Skip to content

How to Create and Enable Custom User Checkers

Warning: You are browsing the documentation for Symfony 2.x, which is no longer maintained.

Read the updated version of this page for Symfony 7.1 (the current stable version).

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.

2.8

The ability to configure a custom user checker per firewall was introduced in Symfony 2.8.

Creating a Custom User Checker

User checkers are classes that must implement the 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 AccountStatusException.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
namespace AppBundle\Security;

use AppBundle\Exception\AccountDeletedException;
use AppBundle\Security\User as AppUser;
use Symfony\Component\Security\Core\Exception\AccountExpiredException;
use Symfony\Component\Security\Core\User\UserCheckerInterface;
use Symfony\Component\Security\Core\User\UserInterface;

class UserChecker implements UserCheckerInterface
{
    public function checkPreAuth(UserInterface $user)
    {
        if (!$user instanceof AppUser) {
            return;
        }

        // user is deleted, show a generic Account Not Found message.
        if ($user->isDeleted()) {
            throw new AccountDeletedException('...');
        }
    }

    public function checkPostAuth(UserInterface $user)
    {
        if (!$user instanceof AppUser) {
            return;
        }

        // user account is expired, the user may be notified
        if ($user->isExpired()) {
            throw new AccountExpiredException('...');
        }
    }
}

Enabling the Custom User Checker

All that's left to be done is creating a service definition and configuring this in the firewall configuration. Configuring the service is done like any other service:

1
2
3
4
# app/config/services.yml
services:
    app.user_checker:
        class: AppBundle\Security\UserChecker

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:

1
2
3
4
5
6
7
8
9
# app/config/security.yml

# ...
security:
    firewalls:
        secured_area:
            pattern: ^/
            user_checker: app.user_checker
            # ...

Additional Configurations

It's possible to have a different user checker per firewall.

1
2
3
4
5
6
7
8
9
10
11
12
# app/config/security.yml

# ...
security:
    firewalls:
        admin:
            pattern: ^/admin
            user_checker: app.admin_user_checker
            # ...
        secured_area:
            pattern: ^/
            user_checker: app.user_checker
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version