src/Security/Voter/AccesoAlmacenVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  4. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  5. use Symfony\Component\Security\Core\User\UserInterface;
  6. use Symfony\Component\Security\Core\Security;
  7. use App\Entity\User;
  8. class AccesoAlmacenVoter extends Voter
  9. {
  10.     private $security;
  11.     public function __construct(Security $security)
  12.     {
  13.         $this->security $security;
  14.     }
  15.     protected function supports($attribute$subject)
  16.     {
  17.         // replace with your own logic
  18.         // https://symfony.com/doc/current/security/voters.html
  19.         return in_array($attribute, ['VER'])
  20.             && ($subject !== NULL);
  21.     }
  22.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  23.     {
  24.         //** $user User */
  25.         $user $token->getUser();
  26.         // if the user is anonymous, do not grant access
  27.         if (!$user instanceof UserInterface) {
  28.             return false;
  29.         }
  30.         switch ($attribute) {
  31.             case 'VER':
  32.                 if ($this->security->isGranted('ROLE_ADMIN')){return TRUE;};
  33.                 if ($user->getAlmacen() == $subject){return TRUE;};
  34.         }
  35.         return false;
  36.     }
  37. }