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

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\Administracion;
  3. use App\Entity\Administracion\Propuesta;
  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 PropuestaVoter extends Voter
  10. {
  11.     const VIEW      'PRO_VIEW';
  12.     const EDIT      'PRO_EDIT';
  13.     const DELETE    'PRO_DELETE';
  14.     const DUPLICATE 'PRO_DUPLICATE';
  15.     const CONFIG    'PRO_CONFIG';
  16.     private $security;
  17.     public function __construct(Security $security)
  18.     {
  19.         $this->security $security;
  20.     }
  21.     protected function supports(string $attribute$subject): bool
  22.     {
  23.         // replace with your own logic
  24.         // https://symfony.com/doc/current/security/voters.html
  25.         return in_array($attribute, [self::EDITself::VIEWself::DELETEself::DUPLICATEself::CONFIG])
  26.             && $subject instanceof Propuesta;
  27.     }
  28.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  29.     {
  30.         $user $token->getUser();
  31.         // if the user is anonymous, do not grant access
  32.         if (!$user instanceof UserInterface) {
  33.             return false;
  34.         }
  35.         switch ($attribute) {
  36.             case self::EDIT:
  37.                 // logic to determine if the user can EDIT
  38.                 // return true or false
  39.                 return $this->canEdit($subject$user);
  40.             case self::VIEW:
  41.                 // logic to determine if the user can VIEW
  42.                 // return true or false
  43.                 return $this->canView($subject$user);
  44.             case self::DELETE:
  45.                 // logic to determine if the user can DELETE
  46.                 // return true or false
  47.                 return $this->canDelete($subject$user);
  48.             case self::DUPLICATE:
  49.                 // logic to determine if the user can DUPLICATE
  50.                 // return true or false
  51.                 return $this->canDuplicate($subject$user);
  52.             case self::CONFIG:
  53.                 // logic to determine if the user can DUPLICATE
  54.                 // return true or false
  55.                 return $this->canConfig($subject$user);
  56.         }
  57.         return false;
  58.     }
  59.     private function canView(Propuesta $subjectUsuario $user){
  60.         if ($this->security->isGranted('ROLE_PRO_VIEW'))
  61.             return true;
  62.         
  63.         
  64.         return false;
  65.     }
  66.     private function canEdit(Propuesta $subjectUsuario $user){
  67.         if ($this->security->isGranted('ROLE_PRO_EDIT'))
  68.             return true;
  69.         
  70.         return false;
  71.     }
  72.     private function canDelete(Propuesta $subjectUsuario $user){
  73.         if ($this->security->isGranted('ROLE_PRO_DELETE'))
  74.             return true;
  75.         return false;
  76.     }
  77.     private function canDuplicate(Propuesta $subjectUsuario $user){
  78.         if ($this->security->isGranted('ROLE_PRO_DUPLICATE'))
  79.             return true;
  80.         return false;
  81.     }
  82.     private function canConfig(Propuesta $subjectUsuario $user){
  83.         if ($this->security->isGranted('ROLE_PRO_CONFIG'))
  84.             return true;
  85.         return false;
  86.     }
  87. }