<?php declare(strict_types=1);
namespace App\EventListener;
use App\Entity\Advisor;
use App\Entity\SalesAgreement;
use App\Repository\SalesAgreementRepository;
use App\Utils\CommonConstants;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\Security;
/**
* Class WeActListener
* @package App\EventListener
*/
class WeActListener implements EventSubscriberInterface
{
/** @var SalesAgreementRepository */
private $saleAgreementRepository;
/** @var Security */
private $security;
/**
* WeActListener constructor.
*
* @param SalesAgreementRepository $saleAgreementRepository
* @param Security $security
*/
public function __construct(
SalesAgreementRepository $saleAgreementRepository,
Security $security
) {
$this->saleAgreementRepository = $saleAgreementRepository;
$this->security = $security;
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * ['eventName' => 'methodName']
* * ['eventName' => ['methodName', $priority]]
* * ['eventName' => [['methodName1', $priority], ['methodName2']]]
*
* The code must not depend on runtime state as it will only be called at compile time.
* All logic depending on runtime state must be put into the individual methods handling the events.
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => [['onKernelRequest']],
];
}
/**
* @param RequestEvent $events
*
*/
public function onKernelRequest(RequestEvent $events)
{
$request = $events->getRequest();
$user = $this->security->getUser();
$routeParams = $request->get('_route_params');
if (null != $routeParams && null != $user
&& in_array($user->getRoles()[0], CommonConstants::ARRAY_AGENCY_ROLES)) {
if (key_exists('salesAgreement', $routeParams)) {
$salesAgreement = $this->saleAgreementRepository->find($routeParams['salesAgreement']);
if (null == $salesAgreement || !$this->checkUserVisibility($user, $salesAgreement)
) {
throw new AccessDeniedException("Vous n'avez pas accès a ce contenu");
}
}
}
}
/**
* @param $user
* @param SalesAgreement $salesAgreement
*
* @return bool
*/
private function checkUserVisibility($user, SalesAgreement $salesAgreement): bool
{
$agencyUser = $salesAgreement->getAgencyUser();
if ($agencyUser instanceof Advisor) {
return $user == $agencyUser || $user == $agencyUser->getDirector();
}
return $user === $agencyUser;
}
}