api/ApiBundle/EventListener/AuthenticationSuccessListener.php line 43

Open in your IDE?
  1. <?php
  2. namespace Pmag\ApiBundle\EventListener;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
  5. use Pmag\ApiBundle\ApiEvents\ClientEvents;
  6. use Pmag\ApiBundle\Entity\Client;
  7. use Pmag\ApiBundle\Event\ClientEvent;
  8. use Pmag\ApiBundle\Manager\ClientManager;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  11. /**
  12.  * AuthenticationSuccessListener
  13.  * @package Pmag\ApiBundle\EventListener
  14.  * @author Daly Ala <rafin_ala03@hotmail.fr>
  15.  **/
  16. class AuthenticationSuccessListener
  17. {
  18.     /**
  19.      * @var EventDispatcherInterface
  20.      */
  21.     private $dispatcher;
  22.     /**
  23.      * @var ClientManager
  24.      */
  25.     private $cm;
  26.     public function __constructEventDispatcherInterface $dispatcher ClientManager $cm)
  27.     {
  28.         $this->dispatcher $dispatcher;
  29.         $this->cm $cm;
  30.     }
  31.     /**
  32.      * Authentication Success
  33.      *
  34.      * @param AuthenticationSuccessEvent $event
  35.      */
  36.     public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $event)
  37.     {
  38.         $user $event->getUser();
  39.         if (!($user instanceof Client)) {
  40.             return;
  41.         }
  42.         if ($user->isActive() == true) {
  43.              $this->cm->resetNumberOfLoginFailures($user);
  44.             $payload = [];
  45.             $payload $event->getData();
  46.             $payload['role'] = $user->getMobileRoles()->getName();
  47.             $this->dispatcher->dispatch(new ClientEvent($user),ClientEvents::CLIENT_LOGIN);
  48.             $event->setData([
  49.                 'code' => $event->getResponse()->getStatusCode(),
  50.                 'message' => 'Authentication Success',
  51.                 'payload' => $payload,
  52.             ]);
  53.         } else {
  54.             $event->getResponse()->setStatusCode(Response::HTTP_LOCKED);
  55.             $event->setData([
  56.                 'code' => Response::HTTP_LOCKED,
  57.                 'message' => 'Account Locked , Contact The Support ASAP '
  58.             ]);
  59.         }
  60.     }
  61. }