src/Security/Voter/PropertyDetailsVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Agent;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. use App\Entity\AbstractCollaborator;
  8. use App\Entity\Property;
  9. class PropertyDetailsVoter extends Voter
  10. {
  11.     public const PROPERTY_DETAILS 'PROPERTY_DETAILS';
  12.     protected function supports(string $attribute$subject): bool
  13.     {
  14.         return $subject instanceof Property && $attribute === self::PROPERTY_DETAILS;
  15.     }
  16.     /**
  17.      * @param string         $attribute
  18.      * @param Property       $subject
  19.      * @param TokenInterface $token
  20.      *
  21.      * @return bool
  22.      */
  23.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  24.     {
  25.         /** @var UserInterface $user */
  26.         $user $token->getUser();
  27.         if (!$user instanceof AbstractCollaborator) {
  28.             return false;
  29.         }
  30.         if (!$subject->getIsShared()) {
  31.             return true;
  32.         }
  33.         if ($subject->getCollaborator()->getId() === $user->getId()) {
  34.             return true;
  35.         }
  36.         $agentAllowed array_filter($subject->getAgentsSharedWith()->toArray(), function(Agent $agent) use ($user) {
  37.             return $user->getId() === $agent->getId();
  38.         });
  39.         
  40.         return count($agentAllowed) > 0;
  41.     }
  42. }