src/Security/Voter/Administracion/AccionVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\Administracion;
  3. use App\Entity\Administracion\Accion;
  4. use App\Entity\Ajustes\Usuario;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\Security;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. class AccionVoter extends Voter
  10. {
  11.     const VIEW      'ACCION_VIEW';
  12.     const EDIT      'ACCION_EDIT';
  13.     const DELETE    'ACCION_DELETE';
  14.     const DUPLICATE 'ACCION_DUPLICATE';
  15.     private $security;
  16.     public function __construct(Security $security)
  17.     {
  18.         $this->security $security;
  19.     }
  20.     protected function supports(string $attribute$subject): bool
  21.     {
  22.         // replace with your own logic
  23.         // https://symfony.com/doc/current/security/voters.html
  24.         return in_array($attribute, [self::EDITself::VIEWself::DELETEself::DUPLICATE])
  25.             && $subject instanceof Accion;
  26.     }
  27.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  28.     {
  29.         $user $token->getUser();
  30.         // if the user is anonymous, do not grant access
  31.         if (!$user instanceof UserInterface) {
  32.             return false;
  33.         }
  34.         switch ($attribute) {
  35.             case self::EDIT:
  36.                 // logic to determine if the user can EDIT
  37.                 // return true or false
  38.                 return $this->canEdit($subject$user);
  39.             case self::VIEW:
  40.                 // logic to determine if the user can VIEW
  41.                 // return true or false
  42.                 return $this->canView($subject$user);
  43.             case self::DELETE:
  44.                 // logic to determine if the user can DELETE
  45.                 // return true or false
  46.                 return $this->canDelete($subject$user);
  47.             case self::DUPLICATE:
  48.                 // logic to determine if the user can DUPLICATE
  49.                 // return true or false
  50.                 return $this->canDuplicate($subject$user);
  51.         }
  52.         return false;
  53.     }
  54.     private function canView(Accion $subjectUsuario $user){
  55.         if ($this->security->isGranted('ROLE_ACT_VIEW'))
  56.             return true;
  57.         
  58.         
  59.         return false;
  60.     }
  61.     private function canEdit(Accion $subjectUsuario $user){
  62.         if ($this->security->isGranted('ROLE_ACT_EDIT'))
  63.             return true;
  64.         
  65.         return false;
  66.     }
  67.     private function canDelete(Accion $subjectUsuario $user){
  68.         if ($this->security->isGranted('ROLE_ACT_DELETE'))
  69.             return true;
  70.         return false;
  71.     }
  72.     private function canDuplicate(Accion $subjectUsuario $user){
  73.         if ($this->security->isGranted('ROLE_ACT_DUPLICATE'))
  74.             return true;
  75.         return false;
  76.     }
  77. }