src/Security/Voter/ColumnVoter.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 ColumnVoter extends Voter
  8. {
  9.     public const EDIT 'POST_EDIT';
  10.     public const VIEW 'POST_VIEW';
  11.     private Security $security;
  12.     public function __construct(Security $security)
  13.     {
  14.         $this->security $security;
  15.     }
  16.     protected function supports(string $attribute$subject): bool
  17.     {
  18.         // replace with your own logic
  19.         // https://symfony.com/doc/current/security/voters.html
  20.         return in_array($attribute, ['ADMIN_COLUMN_EDIT'])
  21.             && $subject instanceof \App\Entity\Column;
  22.     }
  23.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  24.     {
  25.         $user $token->getUser();
  26.         // if the user is anonymous, do not grant access
  27.         if (!$user instanceof UserInterface) {
  28.             return false;
  29.         }
  30.         if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
  31.             return true;
  32.         }
  33.         // ... (check conditions and return true to grant permission) ...
  34.         switch ($attribute) {
  35.             case 'ADMIN_COLUMN_EDIT':
  36.                 return $user === $subject->getWork()->getUser() || $this->security->isGranted('ROLE_SUPER_ADMIN');;
  37.                 // return true or false
  38.         }
  39.         return false;
  40.     }
  41. }