src/Security/Voter/PersonalLearningProgrammVoter.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 PersonalLearningProgrammVoter extends Voter
  8. {
  9.     const CAN_EDIT_DATES_AFTER_START 'can_edit_dates_after_start';
  10.     protected function supports($attribute$subject)
  11.     {
  12.         return
  13.             in_array($attribute, [
  14.                 self::CAN_EDIT_DATES_AFTER_START,
  15.             ])
  16.             && $subject instanceof LearningProgramm
  17.             && $subject->getStudent()
  18.         ;
  19.     }
  20.     /**
  21.      * @param string $attribute
  22.      * @param LearningProgramm $subject
  23.      * @param TokenInterface $token
  24.      *
  25.      * @return bool
  26.      */
  27.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  28.     {
  29.         /** @var \App\Entity\User */
  30.         $user $token->getUser();
  31.         switch ($attribute) {
  32.             case self::CAN_EDIT_DATES_AFTER_START:
  33.                 if (!$subject->isStarted() || $user->hasRole(RoleEnumType::ROLE_SUPER_ADMIN)) {
  34.                     return true;
  35.                 }
  36.                 return false;
  37.             default:
  38.                 return false;
  39.         }
  40.     }
  41. }