src/Security/Placement/PaymentPlan/PaymentPlanVoter.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Security\Placement\PaymentPlan;
  3. use ApiPlatform\Core\Api\IriConverterInterface;
  4. use App\Entity\Formula;
  5. use App\Entity\Placement\Pdf\PaymentPlan\Config\Config;
  6. use App\Entity\Placement\Pdf\PaymentPlan\Config\Group\Group;
  7. use App\Entity\Placement\Pdf\PaymentPlan\Config\Group\Payment\AdvancedSumPayment;
  8. use App\Entity\Placement\Pdf\PaymentPlan\Config\Group\Payment\Formula\AdvancedFormula;
  9. use App\Entity\Placement\Pdf\PaymentPlan\Config\Group\Payment\OneLinePayment;
  10. use App\Entity\Placement\Pdf\PaymentPlan\PaymentPlan;
  11. use App\Entity\Placement\Pdf\PaymentPlan\UnitInfo\SystemValueInfo;
  12. use App\Entity\Placement\Pdf\PaymentPlan\UnitInfo\UnitInfo;
  13. use App\Entity\PlacementProperty;
  14. use App\Security\EntityVoters\EntityVoter;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Symfony\Component\Routing\RouterInterface;
  17. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  18. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  19. use Doctrine\Common\Collections\Collection;
  20. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  21. use Symfony\Component\Security\Core\User\UserInterface;
  22. class PaymentPlanVoter extends EntityVoter
  23. {
  24.     private IriConverterInterface $iriConverter;
  25.     private UserInterface $user;
  26.     public function __construct(IriConverterInterface $iriConverterRouterInterface $routerRequestStack $requestStack)
  27.     {
  28.         $this->iriConverter $iriConverter;
  29.         parent::__construct($router,$requestStack);
  30.     }
  31.     /**
  32.      * @param string $attribute
  33.      * @param $subject PaymentPlan
  34.      * @param TokenInterface $token
  35.      * @return bool
  36.      */
  37.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  38.     {
  39.         $this->user $token->getUser();
  40.         if (!$this->user)
  41.             return false;
  42.         if ($this->user && $this->user->getRole()->getName() === 'ROLE_SUPER_ADMIN')
  43.             return true;
  44.         switch ($attribute)
  45.         {
  46.             case 'GET_COLLECTION':
  47.                 print_r("GET_ALL\n");
  48.                 return true;
  49.                 break;
  50.             case 'GET' :
  51.                 print_r("GET_ONE DELETE\n");
  52.                 if (!$subject->getComplex()->isUserHasAccess($this->user))
  53.                     return false;
  54.                 break;
  55.             case 'PATCH':
  56.                 print_r("PATCH PUT\n");
  57.                 if (!$subject->getComplex()->isUserHasAccess($this->user))
  58.                     return false;
  59.                 break;
  60.             case 'PUT':
  61.                 break;
  62.             case 'DELETE':
  63.                 break;
  64.             case 'POST':
  65.                 print_r("POST\n");
  66.                 break;
  67.             case 'POST_DENORMALIZE':
  68.                 return $this->checkPost($subject);
  69.                 break;
  70.         }
  71.         return false;
  72.     }
  73.     private function checkPost(PaymentPlan $paymentPlan): bool {
  74.         if ($paymentPlan->getComplex() && !$paymentPlan->getComplex()->isUserHasAccess($this->user))
  75.             throw new AccessDeniedException("U have no access to set given complex with id: {$paymentPlan->getComplex()->getId()}"null);
  76.         $this->checkLinkedPlacements($paymentPlan);
  77.         $this->checkLinkedUnitInfos($paymentPlan->getUnitInfos());
  78.         $this->checkLinkedConfig($paymentPlan->getConfig());
  79.         return true;
  80.     }
  81.     /**
  82.      * @param PaymentPlan $paymentPlan
  83.      * @return void
  84.      */
  85.     private function checkLinkedPlacements(PaymentPlan $paymentPlan): void {
  86.         if (($placements $paymentPlan->getPlacements()) && $placements->count() > 0) {
  87.             foreach ($placements as $placement) {
  88.                 if (!$placement->getComplex()->isUserHasAccess($this->user)) {
  89.                     throw new AccessDeniedException("U have no access to add given placement with id: {$placement->getId()}");
  90.                 } else if ($paymentPlan->getComplex()->getId() !== $placement->getComplex()->getId())
  91.                     throw new AccessDeniedException("U have no access to add given placement with id: {$placement->getId()} because it belongs to another complex.");
  92.             }
  93.         }
  94.     }
  95.     /**
  96.      * @param Collection<int, UnitInfo>|null $unitInfos
  97.      * @return void
  98.      */
  99.     private function checkLinkedUnitInfos(?Collection $unitInfos): void {
  100.         if ($unitInfos && $unitInfos->count() > 0) {
  101.             foreach ($unitInfos as $unitInfo) {
  102.                 if ($unitInfo instanceof SystemValueInfo && $unitInfo->getPlacementProperty()) {
  103.                     if (!$unitInfo->getPlacementProperty()->getComplex()->isUserHasAccess($this->user))
  104.                         throw new AccessDeniedException("U have no access to add to unitInfos given placement property with id: {$unitInfo->getPlacementProperty()->getId()} ");
  105.                 }
  106.             }
  107.         }
  108.     }
  109.     /**
  110.      * @param Config|null $config
  111.      * @return void
  112.      */
  113.     private function checkLinkedConfig(?Config $config): void {
  114.         if ($config && ($groups $config->getGroups()) && $groups->count() > 0) {
  115.             $this->checkLinkedGroups($config->getGroups());
  116.         }
  117.     }
  118.     /**
  119.      * @param Collection<int, Group>|null $groups
  120.      * @return void
  121.      */
  122.     private function checkLinkedGroups(?Collection $groups) {
  123.         if ($groups && $groups->count() > 0)
  124.         foreach ($groups as $group) {
  125.             $this->checkLinkedPayments($group->getPayments());
  126.         }
  127.     }
  128.     private function checkLinkedPayments(?Collection $payments) {
  129.         if ($payments && $payments->count() > 0) {
  130.             foreach ($payments as $payment) {
  131.                 if ($payment instanceof OneLinePayment) {
  132.                     $this->checkLinkedFormula($payment->getFormula());
  133.                 } else if ($payment instanceof AdvancedSumPayment) {
  134.                     $this->checkAdvancedFormulas($payment->getAdvancedFormulas());
  135.                 }
  136.             }
  137.         }
  138.     }
  139.     private function checkLinkedFormula(?Formula $formula) {
  140.         if ($formula && ($formulaMembers $formula->getFormulaMembers())) {
  141.             foreach ($formulaMembers as $formulaMember) {
  142.                 if (strpos($formulaMember'/api/') !== false) {
  143.                     $resource $this->iriConverter->getItemFromIri($formulaMember);
  144.                     if ($resource instanceof PlacementProperty) {
  145.                         if (!$resource->getComplex()->isUserHasAccess($this->user)) {
  146.                             throw new AccessDeniedException("U have no access to add to oneLinePayment given placement property with id: {$resource->getId()} ");
  147.                         }
  148.                     }
  149.                 }
  150.             }
  151.         }
  152.     }
  153.     /**
  154.      * @param Collection<int, AdvancedFormula>|null $advancedFormulas
  155.      * @return void
  156.      */
  157.     private function checkAdvancedFormulas(?Collection $advancedFormulas) {
  158.         if ($advancedFormulas && $advancedFormulas->count() > 0) {
  159.             foreach ($advancedFormulas as $advancedFormula) {
  160.                 $this->checkLinkedFormula($advancedFormula->getFormula());
  161.             }
  162.         }
  163.     }
  164.     private function isAllowedPost($userPaymentPlan $subject): bool {
  165.         if (!$user)
  166.             return false;
  167.         if ($subject->getComplex()->isUserHasAccess($user))
  168.             return true;
  169.         return false;
  170.     }
  171.     protected function getVoteEntityClass(): string
  172.     {
  173.         return PaymentPlan::class;
  174.     }
  175. }