src/Security/RouteVoter.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. namespace App\Security;
  4. use App\Entity\Permission;
  5. use App\Entity\Role;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  11. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  12. use App\Entity\User;
  13. class RouteVoter extends Voter
  14. {
  15.         
  16. //        private $security;
  17.         private mixed $route;
  18.         private AuthorizationCheckerInterface $authorizationChecker;
  19.         private EntityManagerInterface $em;
  20.         
  21.         public function __construct(RequestStack $requestStackAuthorizationCheckerInterface $authorizationCheckerEntityManagerInterface $emParameterBagInterface $parameterBag) {
  22.                 $this->route = ($requestStack->getCurrentRequest()->get('_route') !== null) ? $requestStack->getCurrentRequest()->get('_route') : null;
  23. //                if ($requestStack->getCurrentRequest()->get('_route') !== null)
  24. //                        $this->route = $requestStack->getCurrentRequest()->get('_route');
  25.                 
  26.                 $this->authorizationChecker $authorizationChecker;
  27.                 $this->request $requestStack->getCurrentRequest();
  28.                 $this->em $em;
  29.                 $this->parameterBag $parameterBag;
  30.         }
  31.         
  32.         const ACCESS_ROUTE 'access_route';
  33.         
  34.         protected function supports($attribute$subject)
  35.         {
  36.                 return $attribute === self::ACCESS_ROUTE;
  37.         }
  38.         
  39.         protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  40.         {
  41. //                //isjungiam voteri
  42. //                return true;
  43.                 
  44.                 if(is_null($this->route))
  45.                         return true;
  46.                 
  47. //                dump("voteOnAttribute",$this->route);
  48.                 //listas routu be permissionu?
  49. //                $bypassRoutes = ['security_refresh_api_key', 'translations_for_locale', 'product_list', 'translations_list', 'security_request_reset_password', 'security_password_form', 'security_password_new'];
  50.                 if(in_array($this->route,  $this->parameterBag->get('bypassRoutes')))
  51.                         return true;
  52.                 /** @var User $user */
  53.                 $user $token->getUser();
  54.                 // the user must be logged in; if not, deny access
  55.                 if (!$user instanceof User)
  56.                         return false;
  57.                 //super admin can go anywhere
  58.                 if ($user->superAdmin())
  59.                         return true;
  60.                 
  61.                 if (!empty($this->em->getRepository(Permission::class)->findByUserAndRoute($user$this->route)))
  62.                         return true;
  63.                 
  64.                 return false;
  65.         }
  66. }