<?php
namespace App\Security;
namespace App\Security;
use App\Entity\Permission;
use App\Entity\Role;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use App\Entity\User;
class RouteVoter extends Voter
{
// private $security;
private mixed $route;
private AuthorizationCheckerInterface $authorizationChecker;
private EntityManagerInterface $em;
public function __construct(RequestStack $requestStack, AuthorizationCheckerInterface $authorizationChecker, EntityManagerInterface $em, ParameterBagInterface $parameterBag) {
$this->route = ($requestStack->getCurrentRequest()->get('_route') !== null) ? $requestStack->getCurrentRequest()->get('_route') : null;
// if ($requestStack->getCurrentRequest()->get('_route') !== null)
// $this->route = $requestStack->getCurrentRequest()->get('_route');
$this->authorizationChecker = $authorizationChecker;
$this->request = $requestStack->getCurrentRequest();
$this->em = $em;
$this->parameterBag = $parameterBag;
}
const ACCESS_ROUTE = 'access_route';
protected function supports($attribute, $subject)
{
return $attribute === self::ACCESS_ROUTE;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
// //isjungiam voteri
// return true;
if(is_null($this->route))
return true;
// dump("voteOnAttribute",$this->route);
//listas routu be permissionu?
// $bypassRoutes = ['security_refresh_api_key', 'translations_for_locale', 'product_list', 'translations_list', 'security_request_reset_password', 'security_password_form', 'security_password_new'];
if(in_array($this->route, $this->parameterBag->get('bypassRoutes')))
return true;
/** @var User $user */
$user = $token->getUser();
// the user must be logged in; if not, deny access
if (!$user instanceof User)
return false;
//super admin can go anywhere
if ($user->superAdmin())
return true;
if (!empty($this->em->getRepository(Permission::class)->findByUserAndRoute($user, $this->route)))
return true;
return false;
}
}