src/Security/Voter/WorkSpaceVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\WorkSpace;
  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 WorkSpaceVoter extends Voter
  9. {
  10.     public const EDIT 'POST_EDIT';
  11.     public const VIEW 'POST_VIEW';
  12.     private Security $security;
  13.     public function __construct(Security $security)
  14.     {
  15.         $this->security $security;
  16.     }
  17.     protected function supports(string $attribute$subject): bool
  18.     {
  19.         // replace with your own logic
  20.         // https://symfony.com/doc/current/security/voters.html
  21.         return in_array($attribute, [ 'ADMIN_WORK_EDIT'])
  22.             && $subject instanceof WorkSpace;
  23.     }
  24.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  25.     {
  26.         $user $token->getUser();
  27.         // if the user is anonymous, do not grant access
  28.         if (!$user instanceof UserInterface) {
  29.             return false;
  30.         }
  31.         if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
  32.             return true;
  33.         }
  34.         switch ($attribute) {
  35.             case 'ADMIN_WORK_EDIT':
  36. //                dd($subject->getUser());
  37.                 /** @var WorkSpace $subject */
  38.                 return $user === $subject->getUser() || $this->security->isGranted('ROLE_SUPER_ADMIN');;
  39.                 break;
  40.         }
  41.         return false;
  42.     }
  43. }