src/Security/Voter/LearningProgramVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\DBAL\Types\RoleEnumType;
  4. use App\Entity\LearningProgramm;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class LearningProgramVoter extends Voter
  8. {
  9.     const VIEW 'view';
  10.     protected function supports($attribute$subject)
  11.     {
  12.         return in_array($attribute, [self::VIEW])
  13.             && $subject instanceof LearningProgramm;
  14.     }
  15.     /**
  16.      * @param string $attribute
  17.      * @param LearningProgramm $subject
  18.      * @param TokenInterface $token
  19.      *
  20.      * @return bool
  21.      */
  22.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  23.     {
  24.         switch ($attribute) {
  25.             case self::VIEW:
  26.                 /** @var \App\Entity\User */
  27.                 $user $token->getUser();
  28.                 return $user->hasRole(RoleEnumType::ROLE_MODERATOR)
  29.                     || $user->hasRole(RoleEnumType::ROLE_SUPER_ADMIN)
  30.                     || (
  31.                         $subject->getStudent() && $subject->getStudent() == $user && !$subject->isHidden()
  32.                     )
  33.                 ;
  34.             default:
  35.                 return false;
  36.         }
  37.     }
  38. }