src/Security/Voter/CourseVoter.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Course;
  4. use App\Entity\Job\Lecturer;
  5. use App\Entity\Job\Tutor;
  6. use App\Entity\User;
  7. use Doctrine\ORM\EntityManagerInterface;
  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. class CourseVoter extends Voter
  12. {
  13.     const UPDATE 'COURSE_UPDATE';
  14.     const DELETE 'COURSE_DELETE';
  15.     const READ 'COURSE_READ';
  16.     private $security;
  17.     private $entityManager;
  18.     /**
  19.      * @param Security $security
  20.      * @param EntityManagerInterface $entityManager
  21.      */
  22.     public function __construct(Security $securityEntityManagerInterface $entityManager)
  23.     {
  24.         $this->security $security;
  25.         $this->entityManager $entityManager;
  26.     }
  27.     protected function supports($attribute$subject)
  28.     {
  29.         // if the attribute isn't one we support, return false
  30.         if (!in_array($attribute, [self::UPDATEself::DELETEself::READ])) {
  31.             return false;
  32.         }
  33.         // only vote on Course objects inside this voter
  34.         if (!$subject instanceof Course) {
  35.             return false;
  36.         }
  37.         return true;
  38.     }
  39.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  40.     {
  41.         $user $token->getUser();
  42.         if (!$user instanceof User) {
  43.             // the user must be logged in; if not, deny access
  44.             return false;
  45.         }
  46.         if ($this->security->isGranted(User::ROLE_ADMIN)) {
  47.             return true;
  48.         }
  49.         /** @var Course $course */
  50.         $course $subject;         // you know $subject is a Course object, thanks to supports
  51.         switch ($attribute) {
  52.             case self::UPDATE:
  53.                 return $this->canUpdate($course$user);
  54.             case self::DELETE:
  55.                 return $this->canDelete($course$user);
  56.             case self::READ:
  57.                 return $this->canRead($course$user);
  58.         }
  59.         throw new \LogicException('This code should not be reached!');
  60.     }
  61.     private function canUpdate(Course $courseUser $user)
  62.     {
  63.         if ($this->security->isGranted(User::ROLE_LECTURER)) {
  64.             return $course->getLecturers()->contains($user->getJob(Lecturer::class));
  65.         }
  66.         return false;
  67.     }
  68.     private function canDelete(Course $courseUser $user)
  69.     {
  70.         if ($this->security->isGranted(User::ROLE_LECTURER)) {
  71.             return $course->getLecturers()->contains($user->getJob(Lecturer::class));
  72.         }
  73.         return false;
  74.     }
  75.     private function canRead(Course $courseUser $user)
  76.     {
  77.         if ($this->security->isGranted(User::ROLE_LECTURER)) {
  78.             return $course->getLecturers()->contains($user->getJob(Lecturer::class));
  79.         }
  80.         if ($this->security->isGranted(User::ROLE_TUTOR)) {
  81.             $tutorsInCourse $this->entityManager->getRepository(Tutor::class)->findByCourse($course);
  82.             return in_array($user->getJob(Tutor::class), $tutorsInCourse);
  83.         }
  84.         return false;
  85.     }
  86. }