src/Security/Voter/PropertyVisitVoucherVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\AbstractCollaborator;
  4. use App\Entity\Mandate;
  5. use App\Entity\PropertyVisitVoucher;
  6. use App\Entity\Reference\ReferenceMandateStatus;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Component\Security\Core\Security;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. use Exception;
  12. class PropertyVisitVoucherVoter extends Voter
  13. {
  14.     public const POST 'POST';
  15.     public const PUT 'PUT';
  16.     public const DELETE 'DELETE';
  17.     public const GET 'GET';
  18.     /**
  19.      * @var Security
  20.      */
  21.     private Security $security;
  22.     /**
  23.      * @param Security $security
  24.      */
  25.     public function __construct(Security $security)
  26.     {
  27.         $this->security $security;
  28.     }
  29.     protected function supports(string $attribute$subject)
  30.     {
  31.         return in_array(
  32.             $attribute,
  33.             [self::POSTself::PUTself::DELETEself::GET],
  34.             true
  35.         ) && $subject instanceof PropertyVisitVoucher;
  36.     }
  37.     /**
  38.      * @throws Exception
  39.      */
  40.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token)
  41.     {
  42.         /** @var UserInterface $user */
  43.         $user $this->security->getUser();
  44.         // Deny anonymous users
  45.         if (!$user instanceof UserInterface) {
  46.             return false;
  47.         }
  48.         $mandates $subject->getMandates();
  49.         /** @var PropertyVisitVoucher $subject */
  50.         switch ($attribute) {
  51.             case self::DELETE:
  52.                 if (
  53.                     $subject->getCreatedBy() === $user->getId()
  54.                     && $this->security->isGranted(AbstractCollaborator::ROLE_AGENT)
  55.                 ) {
  56.                     return true;
  57.                 }
  58.                 break;
  59.             case self::PUT:
  60.                 foreach ($mandates as $mandate) {
  61.                     if (
  62.                         $this->isHaveValidMandate($mandate)
  63.                         && $this->security->isGranted(AbstractCollaborator::ROLE_AGENT)
  64.                         && $subject->getCreatedBy() === $user->getId()
  65.                     ) {
  66.                         return true;
  67.                     }
  68.                 }
  69.                 break;
  70.             case self::GET:
  71.                 if (
  72.                     $subject->getCreatedBy() === $user->getId()
  73.                     && $this->security->isGranted(AbstractCollaborator::ROLE_AGENT)
  74.                 ) {
  75.                     return true;
  76.                 }
  77.                 break;
  78.             case self::POST:
  79.                 foreach ($mandates as $mandate) {
  80.                     if ($this->isHaveValidMandate($mandate)) {
  81.                         return true;
  82.                     }
  83.                 }
  84.                 return false;
  85.         }
  86.         throw new Exception(sprintf('Unhandled attribute "%s"'$attribute));
  87.     }
  88.     public function isHaveValidMandate(Mandate $mandate): bool
  89.     {
  90.         if (
  91.             !($mandate->getReferenceMandateStatus()->getCode(
  92.             ) === ReferenceMandateStatus::REFERENCE_CODE_MANDATE_STATUS_VALID ||
  93.             $mandate->getReferenceMandateStatus()->getCode(
  94.             ) === ReferenceMandateStatus::REFERENCE_CODE_MANDATE_STATUS_RESERVED)
  95.         ) {
  96.             return false;
  97.         }
  98.         return true;
  99.     }
  100. }