src/Security/Voter/Ajustes/ConfigGlobalVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\Ajustes;
  3. use App\Entity\Ajustes\ConfigGlobal;
  4. use App\Entity\Ajustes\Usuario;
  5. use Symfony\Component\Security\Core\Security;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. class ConfigGlobalVoter extends Voter
  10. {
  11.     const VIEW      'CONFIG_VIEW';
  12.     const EDIT      'CONFIG_EDIT';
  13.     const DELETE    'CONFIG_DELETE';
  14.     const DUPLICATE 'CONFIG_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::DELETE])
  25.             && $subject instanceof ConfigGlobal;
  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.         }
  48.         return false;
  49.     }
  50.     private function canView(ConfigGlobal $subject,Usuario $user){
  51.         if ($this->security->isGranted('ROLE_CONFIG_VIEW'))
  52.             return true;
  53.         
  54.         
  55.         return false;
  56.     }
  57.     private function canEdit(ConfigGlobal $subject,Usuario $user){
  58.         if ($this->security->isGranted('ROLE_CONFIG_EDIT'))
  59.             return true;
  60.         return false;
  61.     }
  62.     private function canDelete(ConfigGlobal $subject,Usuario $user){
  63.         if ($this->security->isGranted('ROLE_CONFIG_DELETE'))
  64.             return true;
  65.         return false;
  66.     }
  67. }