src/Security/Listener/LoginSuccessListener.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Security\Listener;
  3. use App\Entity\Company;
  4. use App\Entity\User;
  5. use App\Security\UserChecker;
  6. use Psr\Log\LoggerInterface;
  7. use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent;
  8. use Symfony\Component\Security\Core\Exception\CustomUserMessageAccountStatusException;
  9. use Symfony\Component\Security\Core\Security;
  10. /**
  11.  * This class is here to check the quotas on a user when the login_success is triggered
  12.  * each request or when the token is created and sent
  13.  * Class LoginSuccessListener
  14.  * @package App\Security\Listener
  15.  */
  16. class LoginSuccessListener
  17. {
  18.     /**
  19.      * @var LoggerInterface
  20.      */
  21.     private $logger;
  22.     /**
  23.      * @var Security
  24.      */
  25.     private $security;
  26.     public function __construct(LoggerInterface $loggerSecurity $security)
  27.     {
  28.         $this->logger $logger;
  29.         $this->security $security;
  30.     }
  31.     public function onSecurityAuthenticationSuccess(AuthenticationSuccessEvent $event)
  32.     {
  33.         $this->logger->info('Check the quotas for the user: ' $event->getAuthenticationToken()->getUsername());
  34.         $user $event->getAuthenticationToken()->getUser();
  35.         if ($user instanceof User && !$user->checkQuotas()){
  36.             $msg UserChecker::QUOTAS_MESSAGE;
  37.             if ($company $user->getCompany()){
  38.                 $msg = ($company->getMessage(Company::MESSAGE_QUOTAS))??$msg;
  39.             }
  40.             throw new CustomUserMessageAccountStatusException($msg);
  41.         }
  42.     }
  43. }