src/EventSubscriber/PasswordChangeSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\User;
  4. use App\Entity\Enterprise;
  5. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Symfony\Component\HttpKernel\Event\RequestEvent;
  9. use Symfony\Component\HttpFoundation\RedirectResponse;
  10. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  11. use Symfony\Component\Security\Core\Security;
  12. class PasswordChangeSubscriber implements EventSubscriberInterface
  13. {
  14.     private $security;
  15.     private $urlGenerator;
  16.     public function __construct(Security $securityUrlGeneratorInterface $urlGenerator)
  17.     {
  18.         $this->security $security;
  19.         $this->urlGenerator $urlGenerator;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             KernelEvents::REQUEST => [
  25.                 ['forcePasswordChange'0]
  26.             ],
  27.         ];
  28.     }
  29.     public function forcePasswordChange(RequestEvent $event): void
  30.     {
  31.         // only deal with the main request, disregard subrequests
  32.         if (!$event->isMasterRequest()) {
  33.             return;
  34.         }
  35.         $user $this->security->getUser();
  36.         // if you do not have a valid user, it means it's not an authenticated request, so it's not our concern
  37.         if (!$user instanceof User) {
  38.             return;            
  39.         }
  40.         //TODO: mettre la désactivation au service du monde en traduisant le tout.
  41.         if (($user->getRoles()[0]!=="ROLE_REGIONMANAGER" && $user->getRoles()[0]!=="ROLE_GESTIONNAIRE" && $user->getRoles()[0]!=="ROLE_ADMIN" && $user->getRoles()[0]!=="ROLE_BUSINESSLINEMANAGER"&& $user->getRoles()[0]!=="ROLE_GROUPMANAGER") ) {
  42.             $isDeactivate true;
  43.             foreach($user->getContact()->getEnterpriseContactPositions() as $ecp){
  44.                 if($ecp->getEnterprise()->getStatus() == true)
  45.                 {
  46.                     $isDeactivate false;
  47.                 }                
  48.             }
  49.         }
  50.         if (!$user->getIsActive()) {
  51.             throw new AccessDeniedException('votre profil est désactivé');
  52.         }
  53.         // if it's not their first login, and they do not need to change their password, move on
  54.         if (!$user->getForcepasswordchange()) {
  55.             return;
  56.         }
  57.         
  58.         
  59.         // if we get here, it means we need to redirect them to the password change view.
  60.         $redirectTo $this->urlGenerator->generate('changepassword');
  61.         if ($event->getRequest()->getRequestUri() !== $redirectTo) {
  62.             $event->setResponse(new RedirectResponse($redirectTo));
  63.         }
  64.         return;
  65.     }
  66. }