User - Security

There may be times when you want to decide if a user is able to login or not based on customer rules. To enable this we use the Symfony User Checker functionality and extended it allowing multiple rules to be applied separate of each other.

Add to Symfony Security

To add this to your symfony application you need to modify your security.yaml

security:
  firewalls:
      // ...
      main:
        // ...
        user_checker: Parthenon\User\Security\UserChecker\UserCheckerObserver

Add a rule

With our extension to the user checker functionality you just create another implementation of a user checker and then add tag parthenon.user.security.user_checker.

An example is our if user has confirmed their email checker.

<?php

declare(strict_types=1);

/*
 * Copyright Humbly Arrogant Ltd 2020-2021, all rights reserved.
 */

namespace Parthenon\User\Security\UserChecker;

use Symfony\Component\Security\Core\Exception\CustomUserMessageAccountStatusException;
use Symfony\Component\Security\Core\User\UserCheckerInterface;
use Symfony\Component\Security\Core\User\UserInterface;

class ConfirmedUserChecker implements UserCheckerInterface
{
    public function checkPreAuth(UserInterface $user)
    {
    }

    public function checkPostAuth(UserInterface $user)
    {
        if (!$user instanceof \Parthenon\User\Entity\UserInterface) {
            return;
        }

        if (!$user->isConfirmed()) {
            throw new CustomUserMessageAccountStatusException('You need to confirm your account');
        }
    }
}