A
A
Alexander Evgenievich2015-02-03 15:21:18
symfony
Alexander Evgenievich, 2015-02-03 15:21:18

How to check additional parameters during authentication in Symfony2?

In my case, I need to know the status (value in the database) of the user during authentication, and if the status is equal to a certain value, then do not authenticate and return an authorization error.
---
Found this option. So will it be right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Александр Евгеньевич, 2015-02-03
@banderos120

Если кому интересно сделал следующим образом:
Создал свой UserChecker и переопределил checker компонента security

class SecurityUserChecker extends UserChecker
{

  /**
   * Checks the user account before authentication.
   *
   * @param UserInterface $user a UserInterface instance
   */
  public function checkPreAuth(UserInterface $user) {
    parent::checkPreAuth($user);

    if(!$user instanceof CustomUserInterface){
      return;
    }

    if($user->isDeleted()){
      $ex = new DeletedException('User account is deleted.');
      $ex->setUser($user);
      throw $ex;
    }
  }
}

security.user_checker:
        class: UserBundle\Security\SecurityUserChecker

Добавил интерфейс с одним методом проверки, удален пользователь, али нет, и унаследовал его от стандартного Security интерфейса:
interface CustomUserInterface extends AdvancedUserInterface
{
  /**
   * @return boolean
   */
  public function isDeleted();
}

Well, I added the implementation of this interface to Entity User . In the isDeleted() method, I checked for deletion and that's all in principle. Well, I added my own DeletedException.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question