src/EventSubscriber/LoginEventSubscriber.php line 70

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Client;
  4. use App\Entity\Company;
  5. use App\Entity\Log;
  6. use App\Entity\MediaObject;
  7. use App\Entity\Segmentation;
  8. use App\Entity\Specialist;
  9. use App\Entity\User;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent as JWTSuccessAuth;
  14. use Lexik\Bundle\JWTAuthenticationBundle\Events as JWTEvents;
  15. /**
  16.  * Logs actions associated with the users, when the connect and when they disconnect
  17.  * Class LoginEventSubscriber
  18.  * @package App\EventSubscriber
  19.  */
  20. class LoginEventSubscriber implements EventSubscriberInterface
  21. {
  22.     const MESSAGES = [
  23.         'success' => 'User is logged in'
  24.     ];
  25.     /**
  26.      * @var EntityManagerInterface
  27.      */
  28.     private $em;
  29.     /**
  30.      * @var int|string
  31.      */
  32.     private $tokenTtl;
  33.     /**
  34.      * LoginEventSubscriber constructor.
  35.      * @param EntityManagerInterface $em
  36.      */
  37.     public function __construct(EntityManagerInterface $em$tokenTtl)
  38.     {
  39.         $this->em $em;
  40.         $this->tokenTtl $tokenTtl;
  41.     }
  42.     /**
  43.      * {@inheritDoc}
  44.      */
  45.     public static function getSubscribedEvents(): array
  46.     {
  47.         // return the subscribed events, their methods and priorities
  48.         return [
  49.             JWTEvents::AUTHENTICATION_SUCCESS => [
  50.                 ['logUserConnection'0],
  51.             ],
  52.             JWTEvents::JWT_CREATED => [
  53.                 ['onJWTCreatedAddDataToPayload'0],
  54.             ]
  55.         ];
  56.     }
  57.     /**
  58.      * Sets the data's user on the payload
  59.      * @param JWTCreatedEvent $event
  60.      * @return void
  61.      */
  62.     public function onJWTCreatedAddDataToPayload(JWTCreatedEvent $event)
  63.     {
  64.         $user $event->getUser();
  65.         $payload $event->getData();
  66.         if ($user instanceof User) {
  67.             $payload['id'] = $user->getId();
  68.             $payload['email'] = $user->getEmail();
  69.             $payload['segmentation'] = null;
  70.             if($user->getClient() instanceof Client && $user->hasRole(User::ROLE_CLIENT)) {
  71.                 $client $user->getClient();
  72.                 $payload['client'] = [
  73.                     'id' => $client->getId(),
  74.                     'firstname' => $client->getFirstName(),
  75.                     'lastname' => $client->getLastName(),
  76.                 ];
  77.                 if($user->getSegmentation() instanceof Segmentation) {
  78.                     $segmentation $user->getSegmentation();
  79.                     $payload['segmentation'] = [
  80.                         'id' => $segmentation->getId(),
  81.                         'name' => $segmentation->getName()
  82.                     ];
  83.                 }
  84.                 if ($client->getCompany() instanceof Company) {
  85.                     $company $client->getCompany();
  86.                     $payload['company'] = [
  87.                         'id' => $company->getId(),
  88.                         'name' => $company->getName(),
  89.                         'logoTv' => ($company->getLogo() instanceof MediaObject) ? $company->getLogo()->getContentUrl() : null,
  90.                         'logo' => $company->getImageUrl(),
  91.                         'imageUrl' => $company->getImageUrl(),
  92.                         'forceSegmentation' => $company->getForceSegmentation(),
  93.                         'hasSegmentations' => ($company->getSegmentations()->count() > 0) ? true false,
  94.                         // 'cmsTitle' => urlencode($company->getCmsTitle()),
  95.                         // 'cmsText' => urlencode($company->getCmsText()),
  96.                         // 'cmsImg' => ($company->getCmsImg() instanceof MediaObject) ? $company->getCmsImg()->getContentUrl() : null,
  97.                         // 'cmsCtaText' => $company->getCmsCtaText(),
  98.                         // 'cmsCtaUrl' => $company->getCmsCtaUrl(),
  99.                         // 'isAssociative' => $company->isActiveAssociation(),
  100.                     ];
  101.                     // if (!$user->hasRole(User::ROLE_COMPANY)){
  102.                         // $expiration = (!empty($user->getCompany()) ?
  103.                         // $user->getCompany()->getNextRestriction(new \DateTime(), new \DateTime("+ {$this->tokenTtl} seconds")) : null);
  104.                         // if ($expiration instanceof \DateTime) {
  105.                         //     $payload['exp'] = $expiration->getTimestamp();
  106.                         // }
  107.                     // }
  108.                 }
  109.                 // $payload['isAssociation'] = ($client->getAssociation() instanceof Association && $client->getAssociation()->getActive() == 1) ? true : false;
  110.                 $payload['isFirstLogin'] = $user->isFirstLogin();
  111.                 // $payload['isHomeSurvey'] = $user->isHomeSurvey();
  112.                 $payload['rmbKey'] = sha1($user->getUsername().$user->getPassword());
  113.                 // Add displayObjectiveSurvey
  114.                 // $user_interests = $this->em->getRepository(Objective::class)->customFindBy(['userId' => $user->getId(), 'active' => 1]);
  115.                 // $payload['displayObjectiveSurvey'] = (!empty($user->getObjectives()->toArray())) ? false : true;
  116.                 // $payload['isAwardEvent'] = false;
  117.                 // /**
  118.                 //  * @var AwardRepository
  119.                 //  */
  120.                 // $awardRepo = $this->em->getRepository(Award::class);
  121.                 // $award = $awardRepo->getActiveAwardsBy(['user' => $user], ['dateEnd' => "ASC"], 1);
  122.                 // if(!empty($award)) $payload['isAwardEvent'] = true;
  123.                 // $payload['isLive'] = $user->isLive();
  124.                 // $payload['isTv'] = $user->isTv();
  125.             }
  126.             if($user->getCompany() instanceof Company && $user->hasRole(User::ROLE_COMPANY)) {
  127.                 $payload['firstname'] = $user->getFirstName() ?? '';
  128.                 $payload['lastname'] = $user->getLastName() ?? '';
  129.                 $payload['function'] = $user->getFunction() ?? '';
  130.                 $company $user->getCompany();
  131.                 $payload['company']['id'] = $company->getId();
  132.                 $payload['company']['name'] = $company->getName();
  133.                 $payload['company']['isLive'] = $company->isLive();
  134.                 $payload['company']['iTv'] = $company->isTv();
  135.             }
  136.             if($user->getSpecialist() instanceof Specialist && $user->hasRole(User::ROLE_SPECIALIST)) {
  137.                 $payload['firstname'] = $user->getFirstName() ?? '';
  138.                 $payload['lastname'] = $user->getLastName() ?? '';
  139.                 $specialist $user->getSpecialist();
  140.                 $payload['specialist']['id'] = $specialist->getId();
  141.                 $payload['specialist']['firstname'] = $specialist->getFirstName();
  142.                 $payload['specialist']['lastname'] = $specialist->getLastName();
  143.             }
  144.         }
  145.         $event->setData($payload);
  146.     }
  147.     /**
  148.      * Logs the user connection in the db
  149.      * @param JWTSuccessAuth $event
  150.      * @return void
  151.      */
  152.     public function logUserConnection(JWTSuccessAuth $event)
  153.     {
  154.         $user $event->getUser();
  155.         if ($user instanceof User) {
  156.             // $log = new Log();
  157.             // $log
  158.             //     ->setCategory(Log::CATEGORY_LOGIN_EVENT)
  159.             //     ->setSubCategory(Log::SUB_LOGIN_SUCCESS)
  160.             //     ->setMessage(self::MESSAGES['success'])
  161.             //     ->setUser($user);
  162.             $user->setLastLogin(new \DateTime());
  163.             // $this->em->persist($log);
  164.             // $this->em->flush();
  165.             $this->em->persist($user);
  166.             $this->em->flush();
  167.         }
  168.     }
  169. }