src/CoreBundle/Security/SelfStudyVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace CoreBundle\Security;
  3. use CoreBundle\Service\SelfStudyVoterService;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use UserBundle\Entity\User;
  7. class SelfStudyVoter extends Voter
  8. {
  9. const CAN_STUDENT_ACCESS = 'canStudentAccess';
  10. private $service;
  11. public function __construct(SelfStudyVoterService $service)
  12. {
  13. $this->service = $service;
  14. }
  15. /**
  16. * @inheritDoc
  17. */
  18. protected function supports($attribute, $subject): bool
  19. {
  20. return $attribute === self::CAN_STUDENT_ACCESS;
  21. }
  22. /**
  23. * @inheritDoc
  24. */
  25. protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
  26. {
  27. $user = $token->getUser();
  28. if (!$user instanceof User) {
  29. /* The user must be logged in; if not, deny access */
  30. return false;
  31. }
  32. return $this->service->canStudentAccess($user);
  33. }
  34. }