src/Security/Voters/Olympiad/Profile.php line 15

  1. <?php
  2. namespace App\Security\Voters\Olympiad;
  3. use App\Entity\Olympiad\Olympiad;
  4. use App\Entity\Organisation\Organisation;
  5. use App\Entity\Organisation\User;
  6. use App\Entity\User\UserRights;
  7. use App\Model\Common\GetOwnerInterface;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  11. use Symfony\Component\Security\Core\Security;
  12. class Profile extends Voter
  13. {
  14.     public const PROFILE_LINK 'olymp_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 Olympiad) {
  31.             /* if (in_array($attribute, [self::PROFILE_EDIT, self::PROFILE_PRIVILEGES, self::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.     private function profileLink(): bool
  65.     {
  66.         $user $this->security->getUser();
  67.         $user_id $user->getId();
  68.         if (empty($user_id)) {
  69.             return false;
  70.         }
  71.         $userRight $this->em->getRepository(UserRights::class)->getUserRolesByType($user_id, [GetOwnerInterface::OWNER_OLYMPIADGetOwnerInterface::OWNER_OLYMPIAD_STAGE]);
  72.         if (!$userRight) {
  73.             return false;
  74.         }
  75.         return true;
  76.     }
  77.     /**
  78.      * @param Organisation $subject
  79.      * @return bool
  80.      */
  81.     protected function profileEdit(Organisation $subject): bool
  82.     {
  83.         /** @var \App\Entity\User\User $user */
  84.         $user $this->security->getUser();
  85.         if ($subject->isDeleted() || !$user) {
  86.             return false;
  87.         }
  88.         $orgUser $this->em->getRepository(User::class)->findByUser($user->getId());
  89.         if (!$orgUser) {
  90.             return false;
  91.         }
  92.         return $orgUser->getOrganisation()->getId() == $subject->getId();
  93.     }
  94.     private function havePrivileges(Organisation $subject)
  95.     {
  96.         if (in_array($subject->getTypeString(), [$subject::TYPE_VUZ])) {
  97.             return true;
  98.         }
  99.         return false;
  100.     }
  101.     private function haveSuccessHistory(Organisation $subject)
  102.     {
  103.         if (in_array($subject->getTypeString(), [$subject::TYPE_VUZ])) {
  104.             return true;
  105.         }
  106.         return false;
  107.     }
  108. }