src/Security/Voter/Ajustes/AlertasVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\Ajustes;
  3. use App\Entity\Ajustes\Alertas;
  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 AlertasVoter extends Voter
  10. {
  11.     const VIEW      'ALERTAS_VIEW';
  12.     const EDIT      'ALERTAS_EDIT';
  13.     const DELETE    'ALERTAS_DELETE';
  14.     const DUPLICATE 'ALERTAS_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 Alertas;
  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.         // ... (check conditions and return true to grant permission) ...
  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.         }
  53.         return false;
  54.     }
  55.     private function canView(Alertas $subject,Usuario $user){
  56.         if ($this->security->isGranted('ROLE_ALERTAS_VIEW'))
  57.             return true;
  58.         if($subject == $user)
  59.             return true;
  60.         
  61.         return false;
  62.     }
  63.     private function canEdit(Alertas $subjectUsuario $user){
  64.         if ($this->security->isGranted('ROLE_ALERTAS_EDIT'))
  65.             return true;
  66.         if($subject == $user)
  67.             return true;
  68.         return false;
  69.     }
  70.     private function canDelete(Alertas $subjectUsuario $user){
  71.         if ($this->security->isGranted('ROLE_ALERTAS_DELETE'))
  72.             return true;
  73.         return false;
  74.     }
  75.     private function canDuplicate(Usuario $subject,Usuario $user){
  76.         if ($this->security->isGranted('ROLE_ALERTAS_DUPLICATE'))
  77.             return true;
  78.         return false;
  79.     }
  80. }