src/Security/Voter/HeiVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use ApiPlatform\Core\Bridge\Doctrine\Orm\Paginator;
  4. use App\Entity\Hei;
  5. use App\Entity\User;
  6. use App\HeiAdministration\StaffDirectory;
  7. use App\Repository\HeiRepository;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. use Symfony\Component\Security\Core\Security;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. class HeiVoter extends Voter
  13. {
  14.     public const VIEW 'HEI_VIEW';
  15.     public const EDIT 'HEI_EDIT';
  16.     public const CREATE 'HEI_CREATE';
  17.     public const DELETE 'HEI_DELETE';
  18.     public const VIEW_INCOMING_MOBILITIES 'VIEW_INCOMING_MOBILITIES';
  19.     public const VIEW_OUTGOING_MOBILITIES 'VIEW_INCOMING_MOBILITIES';
  20.     private Security $security;
  21.     private StaffDirectory $staffDirectory;
  22.     private HeiRepository $heiRepository;
  23.     public function __construct(
  24.         Security $security,
  25.         StaffDirectory $staffDirectory,
  26.         HeiRepository $heiRepository
  27.     ) {
  28.         $this->security $security;
  29.         $this->staffDirectory $staffDirectory;
  30.         $this->heiRepository $heiRepository;
  31.     }
  32.     protected function supports(string $attributemixed $subject): bool
  33.     {
  34.         return in_array($attribute, [
  35.                 self::VIEW,
  36.                 self::CREATE,
  37.                 self::EDIT,
  38.                 self::DELETE,
  39.                 self::VIEW_INCOMING_MOBILITIES,
  40.                 self::VIEW_OUTGOING_MOBILITIES,
  41.             ])
  42.             && (
  43.                 $subject instanceof Paginator
  44.                 || null === $subject
  45.                 || $subject instanceof Hei
  46.             )
  47.         ;
  48.     }
  49.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  50.     {
  51.         /**
  52.          * @var User $user
  53.          */
  54.         $user $token->getUser();
  55.         // if the user is anonymous, do not grant access
  56.         if (!$user instanceof UserInterface) {
  57.             return false;
  58.         }
  59.         if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
  60.             return true;
  61.         }
  62.         // TODO this should just be temporary until Api Platform allows security check on the owning side
  63.         if ($subject instanceof Paginator) {
  64.             $query $subject
  65.                 ->getQuery()
  66.             ;
  67.             $queryParameters $query->getParameters();
  68.             $id $queryParameters[0]
  69.                 ->getValue()
  70.             ;
  71.             $hei $this->heiRepository->findOneBy(['schacCode' => $id]);
  72.         } else {
  73.             $hei $subject;
  74.         }
  75.         switch ($attribute) {
  76.             case self::VIEW:
  77.                 $student $user->getStudent();
  78.                 return $this->staffDirectory->isStaffAtHei($user$hei)
  79.                     || (null !== $student && $student->getHomeHei()->getId() === $hei->getId())
  80.                     ;
  81.             case self::EDIT:
  82.             case self::VIEW_INCOMING_MOBILITIES:
  83.             case self::VIEW_OUTGOING_MOBILITIES:
  84.                 return $this->staffDirectory->isStaffAtHei($user$hei);
  85.             case self::CREATE:
  86.                 return $this->security->isGranted('ROLE_ADMIN');
  87.             case self::DELETE:
  88.                 // This should never be reached as we already check for SUPER_ADMIN role higher up
  89.                 return false;
  90.         }
  91.         return false;
  92.     }
  93. }