src/Controller/CoreController.php line 127

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\{CategoryDepartment};
  4. use Doctrine\Persistence\ManagerRegistry;
  5. use App\Service\{DepartmentServiceSpecialCharService};
  6. use Symfony\Component\HttpFoundation\{RequestRequestStackResponse};
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. abstract class CoreController extends AbstractController
  9. {
  10.     protected $requestStack;
  11.     protected $specialCharService;
  12.     protected $doctrine;
  13.     public function __construct(RequestStack $requestStackSpecialCharService $specialCharService
  14.     ManagerRegistry $doctrine)
  15.     {
  16.         $this->requestStack $requestStack;
  17.         $this->specialCharService $specialCharService;
  18.         $this->doctrine $doctrine;
  19.         
  20.         $request Request::createFromGlobals();
  21.         $currentPath $request->getPathInfo(); // the URI path being requested
  22.         $requestUri $_SERVER['REQUEST_URI'];
  23.         $acl = [
  24.             '/' => ['unimes.fr'],
  25.             '/direction-des-affaires-financieres-et-comptables' => ['unimes.fr'],
  26.             '/direction-des-affaires-financieres-et-comptables/ticket' => ['unimes.fr'],
  27.             '/direction-du-patrimoine' => ['unimes.fr'],
  28.             '/direction-du-patrimoine/ticket' => ['unimes.fr'],
  29.             '/direction-du-patrimoine/reprographie' => ['unimes.fr'],
  30.             '/direction-du-patrimoine/reprographie/ticket' => ['unimes.fr'],
  31.             '/direction-du-patrimoine/demandes-techniques/ticket' => ['unimes.fr'],
  32.             '/direction-du-patrimoine/reservation-de-vehicule/ticket' => ['unimes.fr'],
  33.             '/snap' => ['unimes.fr'],
  34.             '/snap/ticket' => ['unimes.fr'],
  35.         ];
  36.         
  37.         if(array_key_exists($currentPath$acl)) {
  38.             // Récupérer les roles autorisés pour la route courante et vérifier que l'utilisateur actuel possède les droits pour y accéder.
  39.             $authorizedRoles $acl[$currentPath];
  40.             $this->checkAuthorization($authorizedRoles$requestUri);
  41.         }
  42.     }
  43.     /**
  44.      * Checks if the user is logged in and if they have the right roles to see the homepage
  45.      * Tried to use Symfony firewalls and providers but since we don't connect to the database
  46.      * to retrieve the user's informations, we get stuck in an infinite loop between homepage and cas
  47.      * (User provider won't load a full user with the CAS connection, therefore unable to see if the user's roles fit the ACL)
  48.      *
  49.      * @param array $authorizedRoles
  50.      * @return void 
  51.      */
  52.     protected function checkAuthorization(array $roles$requestUri) {
  53.         // We save the $user in a variable because we need to clear the session
  54.         // So department and categories ids won't be used at the wrong moment
  55.         $session $this->requestStack->getSession();
  56.         $user $session->get('user');
  57.         
  58.         // If $user is set
  59.         if ($user) {
  60.             // We put them back in the session
  61.             $session->set('user'$user);
  62.             // $userRole = $user->getEduPersonPrimaryAffiliation();
  63.             
  64.             $email $user->getEmail();
  65.             $endEmail substr($emailstrpos($email'@') + 1);
  66.             // Roles verifications
  67.             if (!in_array($endEmail$roles) && !empty($roles)) {
  68.                 $session->set('authCode'Response::HTTP_FORBIDDEN);
  69.                 $session->set('error', [
  70.                     "statusText" => str_split("Interdit"),
  71.                     "statusCode" => $session->get('authCode'),
  72.                     "message" => "Vous ne disposez pas des droits pour accéder à cette ressource. Veuillez contacter le service informatique si vous pensez qu'il y a une erreur.",
  73.                     "request" => $requestUri,
  74.                 ]);
  75.             }
  76.         } 
  77.         else {
  78.             $session->set('authCode'Response::HTTP_UNAUTHORIZED);
  79.             $session->set('error', [
  80.                 "statusText" => $this->specialCharService->setTitleSpecialChars("pour accéder à l'application"),
  81.                 "statusCode" => $session->get('authCode'),
  82.                 "message" => "Vous ne disposez pas des droits pour accéder à cette ressource. Veuillez contacter le service informatique si vous pensez qu'il y a une erreur.",
  83.                 "request" => $requestUri,
  84.             ]);
  85.         }
  86.     }
  87.     /**
  88.      * Returns a response if the user should not visit the page
  89.      * or a HTTP_OK code if they're allowed to
  90.      *
  91.      * @param object $session
  92.      * @return mixed
  93.      */
  94.     protected function getAuthorizationResponse($session) {
  95.         if ($session->get('authCode') === Response::HTTP_UNAUTHORIZED) {
  96.             return new Response(nullResponse::HTTP_UNAUTHORIZED);
  97.         }
  98.         else if ($session->get('authCode') === Response::HTTP_FORBIDDEN) {
  99.             return new Response(nullResponse::HTTP_FORBIDDEN);
  100.         }
  101.         return Response::HTTP_OK;
  102.     }
  103.     /**
  104.      * From the parameters transmitted in the url, 
  105.      * we fetch the current service and department
  106.      * then we set them in the session and return them
  107.      *
  108.      * @param object $session
  109.      * @return array [$department, $category, $service, $serviceId, $sector]
  110.      */
  111.     protected function handleUrlParams(object $session) {
  112.     
  113.         $sector $_GET['sector'];
  114.         $service $_GET['service'];
  115.         $serviceId $_GET['service'];
  116.         $session->set('sector'$sector);
  117.         $session->set('service'$service);
  118.         
  119.         $category $this->doctrine->getRepository(Category::class)->findBy(['id' => $service]);
  120.         
  121.         $department DepartmentService::setDepartment($sector);
  122.         
  123.         $service $this->specialCharService->setTitleSpecialChars($category[0]);
  124.         return [
  125.             'department' => $department,
  126.             'category' => $category[0],
  127.             'service' => $service,
  128.             'serviceId' => $serviceId,
  129.             'sector' => $sector
  130.         ];
  131.     }
  132.     /**
  133.      * Creates the object from the wanted Class and the form that matches the object
  134.      *
  135.      * @param string $class
  136.      * @param array $choices
  137.      * @return array
  138.      */
  139.     protected function createFormUtils(string $class$choices ) {
  140.         // Creates the ticket object
  141.         $targetClass '\App\Entity\\' $class;
  142.         $ticket = new $targetClass();
  143.         
  144.         if (str_contains($class'Reprography')) {
  145.             $class 'ReprographyTicket';
  146.         }
  147.         // Creates the form with data and some config
  148.         $form $this->createForm('App\Form\\' $class 'Type'::class, $ticket, [
  149.             'data'=> $choices,
  150.             'attr' => [
  151.                 'class' => 'container__form'
  152.             ],
  153.         ]);
  154.         
  155.         return [
  156.             'form' => $form,
  157.             'ticket' => $ticket
  158.         ];
  159.     }
  160.     /**
  161.      * Identifies if the service or its parent is closed
  162.      *
  163.      * @param object $category
  164.      * @return mixed ( boolean | array )
  165.      */
  166.     protected function isServiceOrDepartmentOpen($category) {
  167.         // We need to retrieve the department for further verification
  168.         $currentDepartment $this->doctrine->getRepository(Department::class)->findOneBy(['id' => $category->getDepaId()]);
  169.         
  170.         // If the category is set to invisible, it means it's closed
  171.         // But we also need to check if the whole department is closed  
  172.         if ($category->getIsInvisible() === || $currentDepartment->getVisi() === 0) {
  173.             // If one of them is closed and the current category has the id 33
  174.             if ($category->getId() === '33') {
  175.                 // Returns the true boolean in order to display the specific error view for the category with the id 33 (BU closed)
  176.                return true
  177.             }
  178.             $code Response::HTTP_NOT_FOUND;
  179.             $message "Le service est fermé et ne peut plus recevoir de tickets pour le moment. Veuillez retenter plus tard.";
  180.             $statusText str_split("Internal Server Error");
  181.             
  182.             // Returns the data that will be displayed in the generic error view
  183.             return [
  184.                 "error-data" => [
  185.                     "statusText" => $statusText,
  186.                     "statusCode" => $code,
  187.                     "message" => $message,
  188.                 ],
  189.                 "error" => new Response(null$code),
  190.             ];
  191.         }
  192.         return false;
  193.     }
  194.     /**
  195.      * Returns the id of the user, depending on if they're logged in or not. 
  196.      * Form forms that handles files.
  197.      *
  198.      * @param object $session
  199.      * @param array/null $form
  200.      * @return string
  201.      */
  202.     protected function getUserId($session$form null) {
  203.         $user $session->get('user');
  204.         if ($user) {
  205.             return 'cas-' $user->getUid();
  206.         }
  207.         else {
  208.             return $form->getData()['ticket']['email'];
  209.         }
  210.     }
  211. }