src/Security/Voter/MandateVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\AbstractCollaborator;
  4. use App\Entity\Mandate;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\Security;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. class MandateVoter extends Voter
  10. {
  11.     public const POST 'POST';
  12.     public const PUT 'PUT';
  13.     /**
  14.      * @var Security
  15.      */
  16.     private Security $security;
  17.     /**
  18.      * @param Security $security
  19.      */
  20.     public function __construct(Security $security)
  21.     {
  22.         $this->security $security;
  23.     }
  24.     protected function supports(string $attribute$subject): bool
  25.     {
  26.         return in_array($attribute, [self::POST,self::PUT], true) && $subject instanceof Mandate;
  27.     }
  28.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  29.     {
  30.         $user $this->security->getUser();
  31.         // Deny anonymous users
  32.         if (!$user instanceof UserInterface) {
  33.             return false;
  34.         }
  35.         /** @var Mandate $subject */
  36.         switch ($attribute) {
  37.             case self::PUT:
  38.                 return $subject->isInProgress()
  39.                         && !$subject->isBusinessIndicationMandate()
  40.                         && ($subject->getProperty()->hasSameCollaborator($user)
  41.                     || $this->security->isGranted(AbstractCollaborator::ROLE_MANAGER));
  42.             case self::POST:
  43.                 return $subject->getProperty()->hasSameCollaborator($user)
  44.                     || $this->security->isGranted(AbstractCollaborator::ROLE_MANAGER);
  45.         }
  46.         throw new \RuntimeException(sprintf('Unhandled attribute "%s"'$attribute));
  47.     }
  48. }