src/Security/Voter/StudyGroupVoter.php line 15

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