src/Security/Voter/CompanyVoter.php line 22

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter;
  4. use App\Entity\Admin;
  5. use App\Entity\Area;
  6. use App\Entity\Company;
  7. use App\Entity\Order;
  8. use App\Entity\Page;
  9. use App\Entity\Page\StaticPage;
  10. use App\Entity\PromotionalCode;
  11. use App\Entity\Quotation;
  12. use App\Entity\Reservation\Row;
  13. use App\Entity\Show;
  14. use App\Entity\Sibil\Declaration;
  15. use App\Repository\AreaRepository;
  16. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  17. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  18. class CompanyVoter extends Voter
  19. {
  20.     public function __construct(
  21.         private readonly AreaRepository $areaRepository,
  22.     ) {
  23.     }
  24.     protected function supports(string $attribute$subject): bool
  25.     {
  26.         return ($attribute === 'COMPANY_ACCESS'
  27.             && (
  28.                 $subject instanceof Company
  29.                 || $subject instanceof Show\Theater
  30.                 || $subject instanceof Show\Session
  31.                 || $subject instanceof PromotionalCode
  32.                 || $subject instanceof Show\Session\SittingPlacePrice
  33.                 || $subject instanceof Row
  34.                 || $subject instanceof Page
  35.                 || $subject instanceof Page\Section
  36.                 || $subject instanceof StaticPage
  37.                 || $subject instanceof Order
  38.                 || $subject instanceof Quotation
  39.                 || $subject instanceof Declaration
  40.             )
  41.         );
  42.     }
  43.     /**
  44.      * @param string $attribute
  45.      * @param Show $subject
  46.      * @param TokenInterface $token
  47.      * @return bool
  48.      */
  49.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  50.     {
  51.         $user $token->getUser();
  52.         if (!$user instanceof Admin || !$user->getCompany()) {
  53.             return true;
  54.         }
  55.         /** @var Company $company */
  56.         $company $user->getCompany();
  57.         try {
  58.             if ($subject instanceof Company) {
  59.                 return $company->getId() === $subject->getId();
  60.             }
  61.             if ($subject instanceof Show\Theater || $subject instanceof PromotionalCode || $subject instanceof Order || $subject instanceof Quotation) {
  62.                 return $company->getId() === $subject->getCompany()->getId();
  63.             }
  64.             if ($subject instanceof Show\Session) {
  65.                 return $company->getId() === $subject->getTheater()->getCompany()->getId();
  66.             }
  67.             if ($subject instanceof Show\Session\SittingPlacePrice || $subject instanceof Declaration || $subject instanceof Row) {
  68.                 return $company->getId() === $subject->getSession()->getTheater()->getCompany()->getId();
  69.             }
  70.             if ($subject instanceof Page || $subject instanceof Page\Section || $subject instanceof StaticPage) {
  71.                 $areas $this->areaRepository->getAreasByCompany((string)$company->getId());
  72.                 if (empty($areas)) {
  73.                     return false;
  74.                 }
  75.                 if ($subject instanceof Page || $subject instanceof StaticPage) {
  76.                     $areaId $subject->getArea()->getId();
  77.                 } elseif ($subject instanceof Page\Section) {
  78.                     $areaId $subject->getPage()->getArea()->getId();
  79.                 }
  80.                 $found array_filter($areas, static function (Area $area) use ($areaId) {
  81.                     return $areaId === $area->getId();
  82.                 });
  83.                 return count($found) > 0;
  84.             }
  85.         } catch (\Exception $e) {
  86.         }
  87.         return false;
  88.     }
  89. }