<?php
/**
* Created by PhpStorm.
* User: Pablo
* Date: 1/31/2019
* Time: 3:05 PM
*/
namespace App\Security;
use App\Entity\Pedido1;
use App\Entity\Rol;
use App\Entity\RolPermiso;
use App\Entity\Usuario;
use App\Entity\Permiso;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Security;
class PermisoVoter extends Voter
{
private $security;
private $em;
public function __construct(Security $security, EntityManagerInterface $em)
{
$this->security = $security;
$this->em = $em;
}
protected function supports($attribute, $subject)
{
$permiso_entity = $this->em->getRepository(Permiso::class)->findOneBy(['permiso'=>$attribute]);
if (!$permiso_entity){
return false;
}
return true;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
$roles = $user->getRoles();
foreach ($roles as $rol){
$rol_entity = $this->em->getRepository(Rol::class)->findOneBy(['rol'=>$rol]);
$permisos = $this->em->getRepository(RolPermiso::class)->findBy(['idRol'=>$rol_entity]);
foreach ($permisos as $permiso){
if ($attribute === $permiso->getIdPermiso()->getPermiso()){
return true;
}
}
}
return false;
}
}