src/Security/Voters/Organisation/View.php line 12

  1. <?php
  2. namespace App\Security\Voters\Organisation;
  3. use App\Entity\Organisation\Organisation;
  4. use App\Entity\Organisation\User;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\Security;
  9. class View extends Voter
  10. {
  11.     public const UNIVERSITY_VIEW 'university_view';
  12.     public const OLYMP_CENTER_VIEW 'olymp_center_view';
  13.     /**
  14.      * @var Security
  15.      */
  16.     private $security;
  17.     /**
  18.      * @var EntityManagerInterface
  19.      */
  20.     private $em;
  21.     public function __construct(Security $securityEntityManagerInterface $em)
  22.     {
  23.         $this->security $security;
  24.         $this->em $em;
  25.     }
  26.     protected function supports(string $attribute$subject): bool
  27.     {
  28.         if ($subject instanceof Organisation) {
  29.             if (in_array($attribute, [self::UNIVERSITY_VIEWself::OLYMP_CENTER_VIEW])) {
  30.                 return true;
  31.             }
  32.         }
  33.         return false;
  34.     }
  35.     /**
  36.      * @param string $attribute
  37.      *
  38.      * @param TokenInterface $token
  39.      * @return bool
  40.      */
  41.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  42.     {
  43.         switch ($attribute) {
  44.             case self::UNIVERSITY_VIEW:
  45.                 return $this->universityView($subject);
  46.             case self::OLYMP_CENTER_VIEW:
  47.                 return $this->ocView($subject);
  48.         }
  49.         throw new \LogicException('This code should not be reached!');
  50.     }
  51.     /**
  52.      *
  53.      * @return bool
  54.      */
  55.     protected function universityView(Organisation $subject): bool
  56.     {
  57. //        dump($subject);
  58.         if (false == $subject->isTypeVuz()) {
  59.             return false;
  60.         }
  61.         return $this->haveAccess($subject);
  62.     }
  63.     private function haveAccess(Organisation $subject): bool
  64.     {
  65.         if ($subject->isDeleted()) {
  66.             return false;
  67.         }
  68.         if ($subject->isDeleted() == false && $subject->isActive() == true) {
  69.             return true;
  70.         }
  71.         /** @var \App\Entity\User\User $user */
  72.         $user $this->security->getUser();
  73.         if (!$user && !$subject->isActive()) {
  74.             return false;
  75.         }
  76.         if ($subject->isActive() == false) {
  77.             if ($this->security->isGranted('ROLE_ADMIN')) {
  78.                 return true;
  79.             }
  80.             $orgUser $this->em->getRepository(User::class)->findByUser($user->getId());
  81.             if (!$orgUser) {
  82.                 return false;
  83.             }
  84.             return $orgUser->getOrganisation()->getId() == $subject->getId();
  85.         }
  86.         return false;
  87.     }
  88.     /**
  89.      *
  90.      * @return bool
  91.      */
  92.     protected function ocView(Organisation $subject): bool
  93.     {
  94. //        dump($subject);
  95.         if (false == $subject->isTypeOc()) {
  96.             return false;
  97.         }
  98.         return $this->haveAccess($subject);
  99.     }
  100. }