src/Tasks/Security/Voter/IssueVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Tasks\Security\Voter;
  3. use App\Core\Entity\User;
  4. use App\Tasks\Entity\Issue;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. final class IssueVoter extends Voter
  8. {
  9.     const ISSUE_ACCESS 'issue_access';
  10.     /**
  11.      * {@inheritdoc}
  12.      */
  13.     public function supports(string $attribute$subject): bool
  14.     {
  15.         return $subject instanceof Issue && in_array($attribute, [
  16.             self::ISSUE_ACCESS,
  17.         ]);
  18.     }
  19.     /**
  20.      * {@inheritdoc}
  21.      *
  22.      * @param Issue $subject
  23.      */
  24.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  25.     {
  26.         $user $token->getUser();
  27.         if (!$user instanceof User) {
  28.             return false;
  29.         }
  30.         if ($attribute === self::ISSUE_ACCESS) {
  31.             return $subject->getPerson()->getId() === $user->getId();
  32.         }
  33.         return false;
  34.     }
  35. }