src/Security/Athena/GlobalVoter.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Security\Athena;
  3. use App\Entity\Apollon\BTag;
  4. use App\Entity\Apollon\EditorialContent;
  5. use App\Entity\Apollon\NewsBlog;
  6. use App\Entity\Apollon\NewsBlogCategory;
  7. use App\Entity\Apollon\TipSheet;
  8. use App\Entity\Apollon\Website;
  9. use App\Entity\Apollon\WebsiteInstance;
  10. use App\Entity\Apollon\WebsitePage;
  11. use App\Entity\Apollon\WebsiteTemplate;
  12. use App\Entity\Asclepios\Audit;
  13. use App\Entity\Asclepios\Clinic;
  14. use App\Entity\Asclepios\ClinicEmployee;
  15. use App\Entity\Asclepios\ClinicEmployeeClinic;
  16. use App\Entity\Asclepios\Customer;
  17. use App\Entity\Asclepios\Holiday;
  18. use App\Entity\Asclepios\Order;
  19. use App\Entity\Asclepios\Price;
  20. use App\Entity\Asclepios\PriceCategory;
  21. use App\Entity\Asclepios\PricePlan;
  22. use App\Entity\Asclepios\Service;
  23. use App\Entity\Asclepios\Subscription;
  24. use App\Entity\Asclepios\VetNews;
  25. use App\Entity\Athena\User;
  26. use App\Service\Asclepios\ClinicService;
  27. use Doctrine\ORM\EntityManagerInterface;
  28. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  29. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  30. use Symfony\Component\Security\Core\Security;
  31. class GlobalVoter extends Voter
  32. {
  33.     private ClinicService $clinicService;
  34.     private EntityManagerInterface $entityManager;
  35.     private Security $security;
  36.     public function __construct(EntityManagerInterface $entityManagerSecurity $securityClinicService $clinicService)
  37.     {
  38.         $this->clinicService $clinicService;
  39.         $this->entityManager $entityManager;
  40.         $this->security $security;
  41.     }
  42.     protected function supports(string $attribute$subject): bool
  43.     {
  44.         if ($attribute !== 'ACCESS') {
  45.             return false;
  46.         }
  47.         if (!in_array(get_class($subject), [
  48.             Audit::class,
  49.             BTag::class,
  50.             Clinic::class,
  51.             ClinicEmployee::class,
  52.             Customer::class,
  53.             EditorialContent::class,
  54.             Holiday::class,
  55.             NewsBlog::class,
  56.             Order::class,
  57.             Price::class,
  58.             PricePlan::class,
  59.             Service::class,
  60.             Subscription::class,
  61.             Website::class,
  62.             WebsitePage::class,
  63.         ])) {
  64.             return false;
  65.         }
  66.         return true;
  67.     }
  68.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  69.     {
  70.         $user $token->getUser();
  71.         if (!$user instanceof User) {
  72.             return false;
  73.         }
  74.         if ($this->security->isGranted('ROLE_SALESMAN')) {
  75.             return true;
  76.         }
  77.         if (
  78.             $this->security->isGranted('ROLE_LABO') &&
  79.             get_class($subject) === BTag::class
  80.         ) {
  81.             while ($subject->getParent()) {
  82.                 $subject $subject->getParent();
  83.             }
  84.             if (($vetNews $this->entityManager->getRepository(VetNews::class)->findOneByContent($subject)) &&
  85.                 $vetNews->getAuthor() === $this->security->getUser()
  86.             ) {
  87.                 return true;
  88.             }
  89.         }
  90.         if (
  91.             $this->security->isGranted('ROLE_WRITER') &&
  92.             get_class($subject) === NewsBlog::class &&
  93.             $subject->getUser() === $user
  94.         ) {
  95.             return true;
  96.         }
  97.         if (
  98.             $this->security->isGranted('ROLE_WRITER') &&
  99.             get_class($subject) === BTag::class
  100.         ) {
  101.             while ($subject->getParent()) {
  102.                 $subject $subject->getParent();
  103.             }
  104.             if (($newsBlog $this->entityManager->getRepository(NewsBlog::class)->findOneByContent($subject)) &&
  105.                 $newsBlog->getUser() === $user
  106.             ) {
  107.                 return true;
  108.             }
  109.         }
  110.         if ($clinics $this->getClinicBySubject($subject)) {
  111.             foreach ($clinics as $clinic) {
  112.                 foreach ($this->entityManager->getRepository(ClinicEmployeeClinic::class)->findByClinic($clinic) as $employee) {
  113.                     if ($employee->getClinicEmployee()?->getUser()?->getId() == $user->getId()) {
  114.                         return true;
  115.                     }
  116.                 }
  117.                 foreach ($user->getFranchise()?->getClinics() ?? [] as $franchisedClinic) {
  118.                     if ($clinic->getId() === $franchisedClinic->getId()) {
  119.                         return true;
  120.                     }
  121.                 }
  122.             }
  123.         }
  124.         return false;
  125.     }
  126.     private function getClinicBySubject($subject): array
  127.     {
  128.         $result = [];
  129.         switch (get_class($subject)) {
  130.             case BTag::class:
  131.                 while ($subject->getParent()) {
  132.                     $subject $subject->getParent();
  133.                 }
  134.                 if (
  135.                     (
  136.                         ($template $this->entityManager->getRepository(WebsiteTemplate::class)->findOneByHeader($subject)) ||
  137.                         ($template $this->entityManager->getRepository(WebsiteTemplate::class)->findOneByMain($subject)) ||
  138.                         ($template $this->entityManager->getRepository(WebsiteTemplate::class)->findOneByAside($subject)) ||
  139.                         ($template $this->entityManager->getRepository(WebsiteTemplate::class)->findOneByFooter($subject))
  140.                     ) &&
  141.                     ($website $this->entityManager->getRepository(Website::class)->findOneByTemplate($template)) &&
  142.                     $website->getClinic()
  143.                 ) {
  144.                     $result[] = $website->getClinic();
  145.                 }
  146.                 if (
  147.                     ($newsBlog $this->entityManager->getRepository(NewsBlog::class)->findOneByContent($subject)) &&
  148.                     $newsBlog->getClinic()
  149.                 ) {
  150.                     $result[] = $newsBlog->getClinic();
  151.                 }
  152.                 if (
  153.                     ($tipSheet $this->entityManager->getRepository(TipSheet::class)->findOneByContent($subject)) &&
  154.                     $tipSheet->getClinic()
  155.                 ) {
  156.                     $result[] = $tipSheet->getClinic();
  157.                 }
  158.                 if (
  159.                     ($editorialContent $this->entityManager->getRepository(EditorialContent::class)->findOneByContent($subject)) &&
  160.                     $editorialContent->getClinic()
  161.                 ) {
  162.                     $result[] = $editorialContent->getClinic();
  163.                 }
  164.                 break;
  165.             case Clinic::class:
  166.                 $result[] = $subject;
  167.                 break;
  168.             case ClinicEmployee::class:
  169.                 foreach ($subject->getClinicEmployeeClinics() as $clinicEmployeeClinic) {
  170.                     if ($clinicEmployeeClinic->getClinic()) {
  171.                         $result[] = $clinicEmployeeClinic->getClinic();
  172.                     }
  173.                 }
  174.                 break;
  175.             case Audit::class:
  176.             case Customer::class:
  177.             case EditorialContent::class:
  178.             case Holiday::class:
  179.             case NewsBlog::class:
  180.             case Order::class:
  181.             case Price::class:
  182.             case PriceCategory::class:
  183.             case Service::class:
  184.             case Website::class:
  185.                 if ($subject->getClinic()) {
  186.                     $result[] = $subject->getClinic();
  187.                 }
  188.                 break;
  189.             case PricePlan::class:
  190.                 if ($clinic $this->entityManager->getRepository(Clinic::class)->findOneByPricePlan($subject)) {
  191.                     $result[] = $clinic;
  192.                 }
  193.                 break;
  194.             case Subscription::class:
  195.                 if ($subject->getCustomer()?->getClinic()) {
  196.                     $result[] = $subject->getCustomer()?->getClinic();
  197.                 }
  198.                 break;
  199.             case WebsitePage::class:
  200.                 if (
  201.                     ($instance $this->entityManager->getRepository(WebsiteInstance::class)->findOneByTemplate($subject->getWebsiteTemplate())) &&
  202.                     $instance->getShowcase()?->getClinic()
  203.                 ) {
  204.                     $result[] = $instance->getShowcase()?->getClinic();
  205.                 }
  206.                 break;
  207.         }
  208.         return $result;
  209.     }
  210. }