src/Listener/JWTListener.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\Listener;
  3. use App\Constants\AppConstants;
  4. use App\Exception\Heimdall\ContractNotFoundException;
  5. use App\Exception\UserNotFoundException;
  6. use App\Helper\Heimdall\HeimdallHelper;
  7. use App\Manager\OrganizationManagerInterface;
  8. use App\Manager\ServiceManagerInterface;
  9. use App\Manager\UserManagerInterface;
  10. use App\Manager\UserProfileManagerInterface;
  11. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
  12. use Symfony\Component\Security\Core\User\UserInterface;
  13. /**
  14.  * Class JWTListener.
  15.  */
  16. class JWTListener
  17. {
  18.     protected OrganizationManagerInterface $organizationManager;
  19.     private ServiceManagerInterface $serviceManager;
  20.     protected UserManagerInterface $userManager;
  21.     protected UserProfileManagerInterface $userProfileManager;
  22.     public function __construct(
  23.         OrganizationManagerInterface $organizationManager,
  24.         ServiceManagerInterface $serviceManager,
  25.         UserManagerInterface $userManager,
  26.         UserProfileManagerInterface $userProfileManager
  27.     ) {
  28.         $this->organizationManager $organizationManager;
  29.         $this->serviceManager $serviceManager;
  30.         $this->userManager $userManager;
  31.         $this->userProfileManager $userProfileManager;
  32.     }
  33.     /**
  34.      * Hook for when JWT is created: let's customize the payload.
  35.      *
  36.      * @see https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Resources/doc/2-data-customization.md
  37.      *
  38.      * @return void
  39.      */
  40.     public function onJWTCreated(JWTCreatedEvent $event): void
  41.     {
  42.         $eventUser $event->getUser();
  43.         $payload $event->getData();
  44.         $payload['id'] = $eventUser->getId();
  45.         $payload['heimdallId'] = $this->getUserHeimdallId($eventUser);
  46.         $payload['email'] = $eventUser->getEmail();
  47.         $payload['firstName'] = $eventUser->getFirstName();
  48.         $payload['lastName'] = $eventUser->getLastName();
  49.         $payload['ldapUuid'] = $eventUser->getLdapUuid();
  50.         $payload['currentContract'] = $this->getCurrentContract($eventUser);
  51.         $payload['roles'] = $this->getUserRoles($eventUser);
  52.         $event->setData($payload);
  53.     }
  54.     /**
  55.      * Computes user roles based on which services are activate foreach contract.
  56.      *
  57.      * @todo: Documentation on dynamic user ROLE_ voters
  58.      */
  59.     protected function getUserRoles(UserInterface $user): array
  60.     {
  61.         $heimdallUser $this->userManager->findUser($user->getEmail());
  62.         $userProfiles $this->userProfileManager->getUserProfiles($heimdallUser);
  63.         $rolesUserProfiles HeimdallHelper::getRolesFromUserProfiles($userProfiles);
  64.         $user->setHeimdallId($heimdallUser->getId());
  65.         $organizationContractServices $this->organizationManager->getUserOrganizationContractServices($userfalse);
  66.         $services $this->serviceManager->getServicesByOrganizationContractServices(
  67.             $organizationContractServices
  68.         );
  69.         $rolesOrganizationContractService HeimdallHelper::getRolesFromServices($services);
  70.         return array_merge(
  71.             [AppConstants::USER_PORTAL_ZEUS],
  72.             $rolesUserProfiles,
  73.             $rolesOrganizationContractService
  74.         );
  75.     }
  76.     /**
  77.      * Computes user roles based on which services are activate foreach contract.
  78.      */
  79.     protected function getUserHeimdallId(UserInterface $user): ?string
  80.     {
  81.         try {
  82.             $userHeimdall $this->userManager->findUser($user->getEmail());
  83.         } catch (UserNotFoundException $e) {
  84.             return null;
  85.         }
  86.         return $userHeimdall->getId();
  87.     }
  88.     protected function getCurrentContract(UserInterface $user): array
  89.     {
  90.         $contract $user->getCurrentContract();
  91.         return [
  92.             'id' => $contract->getId(),
  93.             'label' => $contract->getLabel(),
  94.             'status' => $contract->getStatus(),
  95.             'contractMetadatas' => $contract->getContractMetadatas()
  96.         ];
  97.     }
  98. }