src/Security/ExamVoter.php line 12

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security;
  4. use App\Entity\Exam;
  5. use App\Entity\User;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. class ExamVoter extends Voter
  9. {
  10.     const VIEW 'view';
  11.     protected function supports(string $attribute$subject)
  12.     {
  13.         if (!in_array($attribute, [self::VIEW])) {
  14.             return false;
  15.         }
  16.         if (!$subject instanceof Exam) {
  17.             return false;
  18.         }
  19.         return true;
  20.     }
  21.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token)
  22.     {
  23.         $user $token->getUser();
  24.         if (!$user instanceof User) {
  25.             // the user must be logged in; if not, deny access
  26.             return false;
  27.         }
  28.         /** @var Exam $survey */
  29.         $exam $subject;
  30.         switch ($attribute) {
  31.             case self::VIEW:
  32.                 return $this->canView($exam$user);
  33.         }
  34.     }
  35.     private function canView(Exam $examUser $user)
  36.     {
  37.         return $exam->canUserView($user);
  38.     }
  39. }