src/Subscriber/WizardSubscriber.php line 79

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Subscriber;
  4. use App\Logger\Log;
  5. use App\Entity\Site;
  6. use App\Entity\User;
  7. use App\Entity\Year;
  8. use App\Service\AuthService;
  9. use App\Service\IAuthService;
  10. use App\Service\SchoolService;
  11. use App\Service\IOptionService;
  12. use App\Controller\AuthController;
  13. use App\Controller\UserController;
  14. use App\Controller\StaffController;
  15. use App\Controller\SuperController;
  16. use App\Controller\WizardController;
  17. use App\Entity\BaseAuth;
  18. use Symfony\Component\Security\Core\Security;
  19. use Symfony\Component\HttpKernel\KernelEvents;
  20. use Doctrine\Common\Collections\ArrayCollection;
  21. use Symfony\Component\HttpFoundation\JsonResponse;
  22. use Symfony\Component\HttpFoundation\RedirectResponse;
  23. use Symfony\Contracts\Translation\TranslatorInterface;
  24. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  25. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  26. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  27. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  28. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  29. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  30. use Symfony\Component\HttpFoundation\RequestStack;
  31. class WizardSubscriber implements EventSubscriberInterface
  32. {
  33.     private Security $security;
  34.     private ParameterBagInterface $parameterBag;
  35.     protected Log $log;
  36.     protected TranslatorInterface $translator;
  37.     protected SessionInterface $session;
  38.     protected UrlGeneratorInterface $urlGenerator;
  39.     protected AuthService $authService;
  40.     protected SchoolService $schoolService;
  41.     protected RequestStack $request;
  42.     public function __construct(
  43.         SessionInterface $session,
  44.         Security $security,
  45.         Log $log,
  46.         AuthService $authService,
  47.         ParameterBagInterface $parameterBag,
  48.         SchoolService $schoolService,
  49.         UrlGeneratorInterface $urlGenerator,
  50.         TranslatorInterface $translator,
  51.         RequestStack $request
  52.     ) {
  53.         $this->session $session;
  54.         $this->security $security;
  55.         $this->log $log;
  56.         $this->authService $authService;
  57.         $this->schoolService $schoolService;
  58.         $this->parameterBag $parameterBag;
  59.         $this->urlGenerator $urlGenerator;
  60.         $this->translator $translator;
  61.         $this->request $request;
  62.     }
  63.     public function onInteractiveLogin(InteractiveLoginEvent $event)
  64.     {}
  65.     public function onKernelController(ControllerEvent $event)
  66.     {
  67.         $controller $event->getController();
  68.         if (!is_array($controller)) {
  69.             return;
  70.         }
  71.       
  72.         $user $this->security->getUser();
  73.         if (!$controller[0] instanceof WizardController && ($controller[0] instanceof UserController &&  $user instanceof BaseAuth &&
  74.      !in_array(IAuthService::ROLE_STAFF$user->getRoles2()) 
  75.      && !in_array(IAuthService::ROLE_SUPER_ADMINISTRATOR$user->getRoles2())) && !$controller[0] instanceof AuthController
  76.         && !$controller[0] instanceof StaffController && !$controller[0] instanceof SuperController && !empty($user)
  77.         && $user instanceof User && $event->getRequest()->get('_route') != 'user_home' /* ERROR ETAIT LA $event->getRequest()->attributes->get('_action') */
  78.         && $event->getRequest()->get('_route') != 'user_sites') {       
  79.            
  80.             $school $user->getSchool();
  81.             $site null;
  82.             if(!in_array(IAuthService::ROLE_DIRECTOR$user->getRoles2()) && !empty($user->getSite())){
  83.                 $site $user->getSite();
  84.             }else if (!empty($this->session->get(IOptionService::CURRENT_SITE))){
  85.                 $site $this->session->get(IOptionService::CURRENT_SITE);
  86.                 // Rafraîchissement de l'entité site
  87.                 $site $this->schoolService->findSite(['guid' => $site->getGuid()]);
  88.             }elseif (\count($sites $this->schoolService->findAllSite(['school' => $school])) > 0) {
  89.                 $site $sites[0];
  90.               
  91.             }
  92.             if(!empty($site) && !empty($school)){
  93.                 $this->session->set(IOptionService::CURRENT_SITE$site);
  94.                 $years $this->schoolService->getAllYear(['school' => $school,'site' => $site]);
  95.                 $redirectToWizard false;
  96.                 $redirectToWizardAction null;
  97.                 // S'il n'existe aucune année scolaire pour l'établissement, alors c'est une première
  98.                 if(count($years) <= ) {
  99.                     // Création d'une nouvelle année scolaire 
  100.                     $newYear = new Year();
  101.                     $currentCalendarYear = (new \DateTime('now'))->format('Y');
  102.                     $newYear->setLabel(sprintf('%s - %s',$currentCalendarYear,$currentCalendarYear 1));
  103.                     $newYear->setStartDate(new \DateTime('now'));
  104.                     $newYear->setEndDate(new \DateTime('now'));
  105.                     $newYear->setActive(true);
  106.                     $newYear->setCurrent(true);
  107.                     $newYear->setSite($site);
  108.                     $newYear->setSchool($school);
  109.                     $newYear->setWizardActive(true);
  110.                     $newYear->setWizardStatus(IOptionService::WIZARD_WELCOME);
  111.                     $this->schoolService->createOrUpdateYear($newYear,true);
  112.                     $redirectToWizard true;
  113.                     $redirectToWizardAction IOptionService::WIZARD_WELCOME;
  114.                 }else{
  115.                     $wizardActiveYears = (new ArrayCollection($years))->filter(function($entry) {
  116.                         return ($entry instanceof Year && $entry->getWizardActive());
  117.                     });   
  118.                     if(count($wizardActiveYears) > 0){
  119.                         $redirectToWizard true;
  120.                         $redirectToWizardAction $years[0]->getWizardStatus();
  121.                     }
  122.                 }
  123.                 if($redirectToWizard){
  124.                     if(in_array(IAuthService::ROLE_DIRECTOR$user->getRoles2())){
  125.                         $event->setController(function () use ($redirectToWizardAction){
  126.                             return new RedirectResponse($this->urlGenerator->generate($redirectToWizardAction));
  127.                         });
  128.                     }else{return $this->redirectLogout($event);} 
  129.                 }
  130.             }else{
  131.                 return $this->redirectLogout($event);
  132.             }
  133.         }
  134.     }
  135.     public static function getSubscribedEvents()
  136.     {
  137.         return [
  138.             KernelEvents::CONTROLLER => 'onKernelController',
  139.         ];
  140.     }
  141.     public function redirectLogout(ControllerEvent $event)
  142.     {
  143.         if ($event->getRequest()->isMethod('POST') && $event->getRequest()->isXmlHttpRequest()) {
  144.             if (!$event->getController() instanceof AuthController) {
  145.                 $event->setController(function () {
  146.                     return new JsonResponse([
  147.                         'redirect' => $this->parameterBag->get('app.url').'signout',
  148.                     ], 400);
  149.                 });
  150.             } else {
  151.                 $event->setController(function () {
  152.                     return new JsonResponse('signout'200);
  153.                 });
  154.             }
  155.         } else {
  156.             $event->setController(function () {
  157.                 return new RedirectResponse($this->parameterBag->get('app.url').'signout');
  158.             });
  159.         }
  160.     }
  161. }