<?php
namespace App\EventSubscriber\Location;
use App\Services\UtilsService;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\KernelEvents;
use Doctrine\ORM\EntityManagerInterface;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Services\VendorService;
use App\Exception\AccessDeniedException;
use App\Exception\NotFoundException;
use App\Entity\App\Location;
use App\Entity\App\User;
use App\Entity\App\Role;
use Symfony\Contracts\Translation\TranslatorInterface;
class LocationPreWriteSubscriber implements EventSubscriberInterface
{
private $tokenStorage;
private $authorizationChecker;
private $vendorService;
private $entityManager;
private $utilsService;
private $translator;
public function __construct(
TokenStorageInterface $tokenStorage,
AuthorizationCheckerInterface $checker,
VendorService $vendorService,
EntityManagerInterface $entityManager,
UtilsService $utilsService,
TranslatorInterface $translator
) {
$this->tokenStorage = $tokenStorage;
$this->authorizationChecker = $checker;
$this->vendorService = $vendorService;
$this->entityManager = $entityManager;
$this->utilsService = $utilsService;
$this->translator = $translator;
}
public function onKernelView(ViewEvent $event)
{
if ($this->utilsService->isAPublicRequest($event)) {
return;
}
$location = $event->getControllerResult();
$request = $event->getRequest();
$method = $request->getMethod();
$userCurrent = $this->tokenStorage->getToken()->getUser();
if (!($location instanceof Location) ||
(Request::METHOD_POST !== $method && Request::METHOD_PUT && Request::METHOD_DELETE !== $method)
)
return;
if (Request::METHOD_DELETE === $method) {
$entityExist = [];
if (count($location->getTickets()->toArray()) > 0) {
$entityExist[] = $this->translator->trans('tickets');
}
if (count($location->getVendorStaff()->toArray()) > 0) {
$entityExist[] = $this->translator->trans('users');
}
if (count($location->getMaintenanceElements()->toArray()) > 0) {
$entityExist[] = $this->translator->trans('maintenanceElement.plural');
}
if (count($location->getResources()->toArray()) > 0) {
$entityExist[] = $this->translator->trans('resource.plural');
}
if (count($location->getTasks()->toArray()) > 0) {
$entityExist[] = $this->translator->trans('task.plural');
}
if (count($entityExist) > 0) {
throw new AccessDeniedException(
sprintf('%s: %s', $this->translator->trans('Action not allowed. The location is linked to'), implode(', ', $entityExist))
,417);
}
}
if ($this->authorizationChecker->isGranted('ROLE_SUPERADMIN'))
return;
if(!$userCurrent instanceof User)
throw new NotFoundException($this->translator->trans('User current not found'));
$controlAccess = [Role::ROLE_ADMIN];
if (!$this->vendorService->isUserRoleInToVendor($location->getVendor(), $userCurrent, $controlAccess)) {
$controlAccessTranslator = [];
foreach ($controlAccess as $roleName) {
$controlAccessTranslator[] = $this->translator->trans($roleName);
}
throw new AccessDeniedException(
$this->translator->trans('access_allowed_only_for') . (implode(', ', $controlAccessTranslator))
);
}
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['onKernelView', EventPriorities::PRE_WRITE]
];
}
}