src/Security/Voter/FlatPollVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Resident;
  4. use App\Security\Voter\DTO\FlatPoll;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class FlatPollVoter extends Voter
  8. {
  9.     /** @var string */
  10.     public const FLAT_AND_POLL_BELONG_TO_SAME_BUILDING 'FLAT_AND_POLL_BELONG_TO_SAME_BUILDING';
  11.     /** @var array<string> */
  12.     private array $accessTypes = [self::FLAT_AND_POLL_BELONG_TO_SAME_BUILDING];
  13.     /**
  14.      * @param string $attribute
  15.      * @param mixed $subject
  16.      * @return bool
  17.      */
  18.     protected function supports(string $attributemixed $subject): bool
  19.     {
  20.         return in_array($attribute$this->accessTypes)
  21.             && $subject instanceof FlatPoll;
  22.     }
  23.     /**
  24.      * @param string $attribute
  25.      * @param mixed $subject
  26.      * @param TokenInterface $token
  27.      * @return bool
  28.      */
  29.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  30.     {
  31.         $user $token->getUser();
  32.         // if the user is anonymous, do not grant access
  33.         if (!$user instanceof Resident) {
  34.             return false;
  35.         }
  36.         return $attribute === self::FLAT_AND_POLL_BELONG_TO_SAME_BUILDING
  37.             && $subject->getFlat()->getBuilding() === $subject->getPoll()->getBuilding();
  38.     }
  39. }