<?php
// src/EventListener/ExceptionListener.php
namespace App\EventListener;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class GeneralListener {
/* public function onKernelException(ExceptionEvent $event)
{
// You get the exception object from the received event
$exception = $event->getThrowable();
$message = sprintf(
'My Error says: %s with code: %s',
$exception->getMessage(),
$exception->getCode()
);
// Customize your response object to display the exception details
$response = new Response();
$response->setContent($message);
// HttpExceptionInterface is a special type of exception that
// holds status code and header details
if ($exception instanceof HttpExceptionInterface) {
$response->setStatusCode($exception->getStatusCode());
$response->headers->replace($exception->getHeaders());
} else {
$response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
}
// sends the modified response object to the event
$event->setResponse($response);
}
*/
public function onKernelController(ControllerEvent $event) {
$routeName = $event->getRequest()->get('_route');
$checkWs = explode("_", $routeName);
if ($checkWs[0] == 'ws' || $routeName == 'app_login' || $checkWs == 'homepage') {
} else {
if ($routeName) {
$lastUsername = $event->getRequest()->getSession()->get(Security::LAST_USERNAME);
$perms = $event->getRequest()->getSession()->get($lastUsername . "_perms");
$routes = [];
if ($perms) {
$listArray = ["_index", "_new", "_edit", "_delete", "_show"];
$cleanRoute = $routeName;
foreach ($listArray as $list) {
$cleanRoute = str_replace($list, "", $cleanRoute);
}
$hasAccess = 0;
foreach ($perms as $perm) {
$cleanCurrentRoute = $perm['url_access'];
foreach ($listArray as $list) {
$cleanCurrentRoute = str_replace($list, "", $cleanCurrentRoute);
}
if ($cleanCurrentRoute == $cleanRoute) {
$routes[] = $cleanRoute . "_index";
$routes[] = $cleanRoute[0] . "_selector_1";
$routes[] = $cleanRoute[0] . "_selector_2";
$routes[] = $cleanRoute[0] . "_selector_3";
$routes[] = $cleanRoute[0] . "_selector_4";
$routes[] = $cleanRoute[0] . "_selector_5";
if ($perm['write_permission'] == 1) {
$routes[] = $cleanRoute . "_new";
};
if ($perm['edit_permission'] == 1) {
$routes[] = $cleanRoute . "_edit";
};
if ($perm['delete_permission'] == 1) {
$routes[] = $cleanRoute . "_delete";
};
if ($perm['read_permission'] == 1) {
$routes[] = $cleanRoute . "_show";
};
}
}
if (in_array($routeName, $routes)) {
$hasAccess++;
}
// if ($hasAccess == 0) {
// throw new AccessDeniedHttpException('Se requiere autorización para ingresar a esta sección');
// }
}
}
}
}
}