src/Security/Voter/PropertyVoter.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\AbstractCollaborator;
  4. use App\Entity\Property;
  5. use App\Entity\Reference\ReferencePropertyStatus;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\Security;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. use Exception;
  11. class PropertyVoter extends Voter
  12. {
  13.     public const GET 'GET';
  14.     public const POST 'POST';
  15.     public const PROPERTY_EDIT 'PROPERTY_EDIT';
  16.     /**
  17.      * @var Security
  18.      */
  19.     private Security $security;
  20.     /**
  21.      * @param Security $security
  22.      */
  23.     public function __construct(Security $security)
  24.     {
  25.         $this->security $security;
  26.     }
  27.     protected function supports(string $attribute$subject)
  28.     {
  29.         return in_array(
  30.             $attribute,
  31.             [self::POSTself::GETself::PROPERTY_EDIT],
  32.             true
  33.         ) && $subject instanceof Property;
  34.     }
  35.     /**
  36.      * @throws Exception
  37.      */
  38.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token)
  39.     {
  40.         /** @var UserInterface $user */
  41.         $user $token->getUser();
  42.         // Deny anonymous users
  43.         if (!$user instanceof UserInterface) {
  44.             return false;
  45.         }
  46.         /** @var Property $subject */
  47.         switch ($attribute) {
  48.             case self::PROPERTY_EDIT:
  49.                 if (
  50.                     ($this->security->isGranted(AbstractCollaborator::ROLE_AGENT) &&
  51.                         $subject->getCollaborator()->getId() === $user->getId() &&
  52.                         $subject->getReferencePropertyStatus()->getName(
  53.                         ) == ReferencePropertyStatus::REFERENCE_PROPERTY_STATUS_INVALIDE)
  54.                     ||
  55.                     (
  56.                         $this->security->isGranted(
  57.                             AbstractCollaborator::ROLE_MANAGER
  58.                         ) && $subject->getReferencePropertyStatus()->getName(
  59.                         ) != ReferencePropertyStatus::REFERENCE_PROPERTY_STATUS_RESERVED
  60.                     )
  61.                 ) {
  62.                     return true;
  63.                 }
  64.                 return false;
  65.             case self::POST:
  66.                 if ($this->security->isGranted(AbstractCollaborator::ROLE_AGENT) || $this->security->isGranted(
  67.                     AbstractCollaborator::ROLE_MANAGER
  68.                 )) {
  69.                     return true;
  70.                 }
  71.                 return false;
  72.         }
  73.         throw new Exception(sprintf('Unhandled attribute "%s"'$attribute));
  74.     }
  75. }