<?php
namespace App\Service;
use App\Entity\Modules\Lnk;
use App\Entity\Modules\Modules;
use App\Entity\User;
use App\Service\Abstract\MainService;
class ModulesManager extends MainService
{
/**
* @var string
*/
private string $rejectedRouteReason;
/**
* @param string $currentRoute
*
* @return bool
*/
public function checkRoute(string $currentRoute): bool
{
$modulesRep = $this->em->getRepository(Modules::CLASS);
$module = $modulesRep->findOneBy(['route' => $currentRoute]);
if (!$module) {
return true;
}
$this->rejectedRouteReason = 'no_access';
if (!$this->isLogged()) {
$this->rejectedRouteReason = 'not_logged';
return false;
} elseif ($this->getUserStatus() !== User::STATUS_ACTIVE) {
$this->rejectedRouteReason = 'status_' . $this->getUserStatus();
return false;
}
$linksRep = $this->em->getRepository(Lnk::CLASS);
$link = $linksRep->findOneBy(
[
'roleId' => $this->getUserRole(),
'moduleId' => $module->getId()
]
);
if ($link) {
return true;
}
return false;
}
/**
* @param bool $menuOnly
*
* @return array
*/
public function getAllModules(
bool $menuOnly = false
): array
{
try {
$connection = $this->em->getConnection();
return $connection->executeQuery("
SELECT
m.id,
m.name,
m.route,
m.icon,
m.parent,
m.menu_position,
m.popup_open,
l.menu
FROM modules_lnk l
INNER JOIN modules m
ON m.id = l.module_id
WHERE
l.role_id = :PERM" . ($menuOnly ? ' AND l.menu = 1' : ''),
[
'PERM' => $this->getUserRole()
]
)
->fetchAllAssociative();
} catch (\Doctrine\DBAL\Exception $e) {
}
return [];
}
/**
* @return string
*/
public function getRejectReason(): string
{
return $this->rejectedRouteReason;
}
}