src/EventSubscriber/LoginEventSubscriber.php line 139

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Association;
  4. use App\Entity\Award;
  5. use App\Entity\Objective;
  6. use App\Entity\Company;
  7. use App\Entity\Department;
  8. use App\Entity\Log;
  9. use App\Entity\MediaObject;
  10. use App\Entity\User;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent as JWTSuccessAuth;
  15. use Lexik\Bundle\JWTAuthenticationBundle\Events as JWTEvents;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. /**
  18.  * Logs actions associated with the users, when the connect and when they disconnect
  19.  * Class LoginEventSubscriber
  20.  * @package App\EventSubscriber
  21.  */
  22. class LoginEventSubscriber implements EventSubscriberInterface
  23. {
  24.     const MESSAGES = [
  25.         'success' => 'User is logged in'
  26.     ];
  27.     /**
  28.      * @var EntityManagerInterface
  29.      */
  30.     private $manager;
  31.     /**
  32.      * @var int|string
  33.      */
  34.     private $tokenTtl;
  35.     /**
  36.      * LoginEventSubscriber constructor.
  37.      * @param EntityManagerInterface $manager
  38.      */
  39.     public function __construct(EntityManagerInterface $manager$tokenTtl)
  40.     {
  41.         $this->manager $manager;
  42.         $this->tokenTtl $tokenTtl;
  43.     }
  44.     /**
  45.      * {@inheritDoc}
  46.      */
  47.     public static function getSubscribedEvents(): array
  48.     {
  49.         // return the subscribed events, their methods and priorities
  50.         return [
  51.             JWTEvents::AUTHENTICATION_SUCCESS => [
  52.                 ['logUserConnection'0],
  53.             ],
  54.             JWTEvents::JWT_CREATED => [
  55.                 ['onJWTCreatedAddDataToPayload'0],
  56.             ],
  57.         ];
  58.     }
  59.     /**
  60.      * Sets the company user on the payload
  61.      * @param JWTCreatedEvent $event
  62.      */
  63.     public function onJWTCreatedAddDataToPayload(JWTCreatedEvent $event)
  64.     {
  65.         $user $event->getUser();
  66.         $payload $event->getData();
  67.         if ($user instanceof User) {
  68.             $payload['email'] = $user->getEmail();
  69.             $payload['id'] = $user->getId();
  70.             $payload['firstLogin'] = $user->isFirstLogin();
  71.             $payload['homeSurvey'] = $user->isHomeSurvey();
  72.             if($user->getDepartment() instanceof Department) {
  73.                 $department $user->getDepartment();
  74.                 $payload['department'] = [
  75.                     'id' => $department->getId(),
  76.                     'name' => $department->getName()
  77.                 ];
  78.             }
  79.             $payload['rmbKey'] = sha1($user->getUsername().$user->getPassword());
  80.             // Add Company info into JWT
  81.             if ($user->getCompany() instanceof Company) {
  82.                 $company $user->getCompany();
  83.                 // VĂ©rifie si les associations sont actives
  84.                 $isAssociative false;
  85.                 if(!empty($company->getAssociations()->toArray())) {
  86.                     foreach ($company->getAssociations()->toArray() as $association) {
  87.                         if($association->getActive() == 1) {
  88.                             $isAssociative true;
  89.                             break;
  90.                         }
  91.                     }
  92.                 }
  93.                 $company_arr = [
  94.                     'id' => $company->getId(),
  95.                     'name' => $company->getName(),
  96.                     'logo' => ($company->getLogo() instanceof MediaObject) ? $company->getLogo()->getContentUrl() : null,
  97.                     'cmsTitle' => urlencode($company->getCmsTitle()),
  98.                     'cmsText' => urlencode($company->getCmsText()),
  99.                     'cmsImg' => ($company->getCmsImg() instanceof MediaObject) ? $company->getCmsImg()->getContentUrl() : null,
  100.                     'cmsCtaText' => $company->getCmsCtaText(),
  101.                     'cmsCtaUrl' => $company->getCmsCtaUrl(),
  102.                     'isAssociative' => $isAssociative,
  103.                     'isResa' => $company->getResaLinkActive(),
  104.                 ];
  105.                 $payload['company'] = $company_arr;
  106.                 if (!$user->hasRole('ROLE_MANAGER')){
  107.                     // $expiration = (!empty($user->getCompany()) ?
  108.                     // $user->getCompany()->getNextRestriction(new \DateTime(), new \DateTime("+ {$this->tokenTtl} seconds")) : null);
  109.                     // if ($expiration instanceof \DateTime) {
  110.                     //     $payload['exp'] = $expiration->getTimestamp();
  111.                     // }
  112.                 }
  113.             }
  114.             // Add displayObjectiveSurvey
  115.             // $user_interests = $this->manager->getRepository(Objective::class)->customFindBy(['userId' => $user->getId(), 'active' => 1]);
  116.             $payload['displayObjectiveSurvey'] = (!empty($user->getObjectives()->toArray())) ? false true;
  117.             $payload['isAwardEvent'] = false;
  118.             $award $this->manager->getRepository(Award::class)->getActiveAwardsBy(['user' => $user], ['dateEnd' => "ASC"], 1);
  119.             if(!empty($award)) $payload['isAwardEvent'] = true;
  120.             $payload['isAssociation'] = ($user->getAssociation() instanceof Association && $user->getAssociation()->getActive() == 1) ? true false;
  121.         }
  122.         $event->setData($payload);
  123.     }
  124.     /**
  125.      * Logs the user connection in the db
  126.      * @param JWTSuccessAuth $event
  127.      */
  128.     public function logUserConnection(JWTSuccessAuth $event)
  129.     {
  130.         $user $event->getUser();
  131.         if ($user instanceof User) {
  132.             $log = new Log();
  133.             $log
  134.                 ->setCategory(Log::CATEGORY_LOGIN_EVENT)
  135.                 ->setSubCategory(Log::SUB_LOGIN_SUCCESS)
  136.                 ->setMessage(self::MESSAGES['success'])
  137.                 ->setUser($user);
  138.             $user->setLastLogin(new \DateTime());
  139.             $this->manager->persist($log);
  140.             $this->manager->persist($user);
  141.             $this->manager->flush();
  142.         }
  143.     }
  144. }