src/Security/Voter/LanguageVoter.php line 10

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\Security;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. class LanguageVoter extends Voter
  8. {
  9.     public const EDIT 'POST_EDIT';
  10.     public const VIEW 'POST_VIEW';
  11.     public const ADD 'POST_ADD';
  12.     public const DELETE 'POST_DELETE';
  13.     public function __construct( private readonly Security $security ) {}
  14.     protected function supports(string $attribute$subject): bool
  15.     {
  16.         // replace with your own logic
  17.         // https://symfony.com/doc/current/security/voters.html
  18.         return in_array($attribute, [self::EDITself::VIEWself::ADDself::DELETE])
  19.             && $subject instanceof \App\Entity\Language;
  20.     }
  21.     /* attribute = accion, y subject es el objeto */
  22.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  23.     {
  24.         if ($this->security->isGranted('ROLE_ADMIN'))
  25.             return true;
  26.         if ($this->security->isGranted('ROLE_MANAGER')) {
  27.             return true;
  28.         }
  29.         /* respuesta segura al ANON */
  30.         return false;
  31.     }
  32. }