src/Security/Voters/Organisation/Profile.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 Profile extends Voter
  10. {
  11.     public const PROFILE_EDIT 'org_profile_edit';
  12.     public const PROFILE_PRIVILEGES 'org_privileges';
  13.     public const PROFILE_SUCCESS_HISTORY 'org_success_history';
  14.     public const PROFILE_LINK 'org_profile_link';
  15.     /**
  16.      * @var Security
  17.      */
  18.     private $security;
  19.     /**
  20.      * @var EntityManagerInterface
  21.      */
  22.     private $em;
  23.     public function __construct(Security $securityEntityManagerInterface $em)
  24.     {
  25.         $this->security $security;
  26.         $this->em $em;
  27.     }
  28.     protected function supports(string $attribute$subject): bool
  29.     {
  30.         if ($subject instanceof Organisation) {
  31.             if (in_array($attribute, [self::PROFILE_EDITself::PROFILE_PRIVILEGESself::PROFILE_SUCCESS_HISTORY,])) {
  32.                 return true;
  33.             }
  34.         }
  35. //        if ($subject instanceof \App\Entity\User\User) {
  36.         if ($this->security->getUser()) {
  37.             if (in_array($attribute, [self::PROFILE_LINK])) {
  38.                 return true;
  39.             }
  40.         }
  41. //        }
  42.         return false;
  43.     }
  44.     /**
  45.      * @param string $attribute
  46.      * @param Organisation $subject
  47.      * @param TokenInterface $token
  48.      * @return bool
  49.      */
  50.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  51.     {
  52.         switch ($attribute) {
  53.             case self::PROFILE_EDIT:
  54.                 return $this->profileEdit($subject);
  55.             case self::PROFILE_LINK:
  56.                 return $this->profileLink();
  57.             case self::PROFILE_PRIVILEGES:
  58.                 return $this->havePrivileges($subject);
  59.             case self::PROFILE_SUCCESS_HISTORY:
  60.                 return $this->haveSuccessHistory($subject);
  61.         }
  62.         throw new \LogicException('This code should not be reached!');
  63.     }
  64.     /**
  65.      * @param Organisation $subject
  66.      * @return bool
  67.      */
  68.     protected function profileEdit(Organisation $subject): bool
  69.     {
  70.         /** @var \App\Entity\User\User $user */
  71.         $user $this->security->getUser();
  72.         if ($subject->isDeleted() || !$user) {
  73.             return false;
  74.         }
  75.         $orgUser $this->em->getRepository(User::class)->findByUser($user->getId());
  76.         if (!$orgUser) {
  77.             return false;
  78.         }
  79.         return $orgUser->getOrganisation()->getId() == $subject->getId();
  80.     }
  81.     private function profileLink()
  82.     {
  83.         $subject $this->security->getUser();
  84.         $orgUser $this->em->getRepository(User::class)->findByUser($subject->getId());
  85.         if (!$orgUser) {
  86.             return false;
  87.         }
  88.         if ($orgUser->getOrganisation()->isDeleted()) {
  89.             return false;
  90.         }
  91.         return true;
  92.     }
  93.     private function havePrivileges(Organisation $subject)
  94.     {
  95.         if (in_array($subject->getTypeString(), [$subject::TYPE_VUZ])) {
  96.             return true;
  97.         }
  98.         return false;
  99.     }
  100.     private function haveSuccessHistory(Organisation $subject)
  101.     {
  102.         if (in_array($subject->getTypeString(), [$subject::TYPE_VUZ])) {
  103.             return true;
  104.         }
  105.         return false;
  106.     }
  107. }