src/Service/ModulesManager.php line 83

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\Modules\Lnk;
  4. use App\Entity\Modules\Modules;
  5. use App\Entity\User;
  6. use App\Service\Abstract\MainService;
  7. class ModulesManager extends MainService
  8. {
  9.     /**
  10.      * @var string
  11.      */
  12.     private string $rejectedRouteReason;
  13.     /**
  14.      * @param string $currentRoute
  15.      *
  16.      * @return bool
  17.      */
  18.     public function checkRoute(string $currentRoute): bool
  19.     {
  20.         $modulesRep $this->em->getRepository(Modules::CLASS);
  21.         $module $modulesRep->findOneBy(['route' => $currentRoute]);
  22.         if (!$module) {
  23.             return true;
  24.         }
  25.         $this->rejectedRouteReason 'no_access';
  26.         if (!$this->isLogged()) {
  27.             $this->rejectedRouteReason 'not_logged';
  28.             return false;
  29.         } elseif ($this->getUserStatus() !== User::STATUS_ACTIVE) {
  30.             $this->rejectedRouteReason 'status_' $this->getUserStatus();
  31.             return false;
  32.         }
  33.         $linksRep $this->em->getRepository(Lnk::CLASS);
  34.         $link $linksRep->findOneBy(
  35.             [
  36.                 'roleId'   => $this->getUserRole(),
  37.                 'moduleId' => $module->getId()
  38.             ]
  39.         );
  40.         if ($link) {
  41.             return true;
  42.         }
  43.         return false;
  44.     }
  45.     /**
  46.      * @param bool $menuOnly
  47.      *
  48.      * @return array
  49.      */
  50.     public function getAllModules(
  51.         bool $menuOnly false
  52.     ): array
  53.     {
  54.         try {
  55.             $connection $this->em->getConnection();
  56.             return $connection->executeQuery("
  57.                 SELECT
  58.                     m.id,
  59.                     m.name,
  60.                     m.route,
  61.                     m.icon,
  62.                     m.parent,
  63.                     m.menu_position,
  64.                     m.popup_open,
  65.                     l.menu
  66.                 FROM modules_lnk l
  67.                 INNER JOIN modules m
  68.                     ON m.id = l.module_id
  69.                 WHERE
  70.                     l.role_id = :PERM" . ($menuOnly ' AND l.menu = 1' ''),
  71.                 [
  72.                     'PERM' => $this->getUserRole()
  73.                 ]
  74.             )
  75.             ->fetchAllAssociative();
  76.         } catch (\Doctrine\DBAL\Exception $e) {
  77.         }
  78.         return [];
  79.     }
  80.     /**
  81.      * @return string
  82.      */
  83.     public function getRejectReason(): string
  84.     {
  85.         return $this->rejectedRouteReason;
  86.     }
  87. }