src/Security/Voter/Ajustes/UsuarioVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\Ajustes;
  3. use App\Entity\Ajustes\Usuario;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\Security;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. class UsuarioVoter extends Voter
  9. {
  10.     const VIEW      'USER_VIEW';
  11.     const EDIT      'USER_EDIT';
  12.     const DELETE    'USER_DELETE';
  13.     const DUPLICATE 'USER_DUPLICATE';
  14.     private $security;
  15.     public function __construct(Security $security)
  16.     {
  17.         $this->security $security;
  18.     }
  19.     protected function supports(string $attribute$subject): bool
  20.     {
  21.         // replace with your own logic
  22.         // https://symfony.com/doc/current/security/voters.html
  23.         return in_array($attribute, [self::EDITself::VIEWself::DELETEself::DUPLICATE])
  24.             && $subject instanceof Usuario;
  25.     }
  26.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  27.     {
  28.         $user $token->getUser();
  29.         // if the user is anonymous, do not grant access
  30.         if (!$user instanceof UserInterface) {
  31.             return false;
  32.         }
  33.         // ... (check conditions and return true to grant permission) ...
  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(Usuario $subject,Usuario $user){
  55.         if ($this->security->isGranted('ROLE_USR_VIEW'))
  56.             return true;
  57.         if($subject == $user)
  58.             return true;
  59.         
  60.         return false;
  61.     }
  62.     private function canEdit(Usuario $subject,Usuario $user){
  63.         if ($this->security->isGranted('ROLE_USR_EDIT'))
  64.             return true;
  65.         if($subject == $user)
  66.             return true;
  67.         return false;
  68.     }
  69.     private function canDelete(Usuario $subject,Usuario $user){
  70.         if ($this->security->isGranted('ROLE_USR_DELETE'))
  71.             return true;
  72.         return false;
  73.     }
  74.     private function canDuplicate(Usuario $subject,Usuario $user){
  75.         if ($this->security->isGranted('ROLE_USR_DUPLICATE'))
  76.             return true;
  77.         return false;
  78.     }
  79. }