src/Security/Voter/HomeworkResultVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\DBAL\Types\HomeworkResultStatusEnum;
  4. use App\Entity\HomeworkResult;
  5. use App\Entity\User;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. class HomeworkResultVoter extends Voter
  10. {
  11.     const CHECK 'homework_check';
  12.     const CAN_HANDOVER 'can_handover';
  13.     protected function supports($attribute$subject)
  14.     {
  15.         return in_array($attribute, [self::CHECKself::CAN_HANDOVER])
  16.             && $subject instanceof HomeworkResult;
  17.     }
  18.     /**
  19.      * @param string $attribute
  20.      * @param HomeworkResult $subject
  21.      * @param TokenInterface $token
  22.      *
  23.      * @return bool
  24.      */
  25.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  26.     {
  27.         $user $token->getUser();
  28.         // if the user is anonymous, do not grant access
  29.         if (!$user instanceof UserInterface) {
  30.             return false;
  31.         }
  32.         // ... (check conditions and return true to grant permission) ...
  33.         switch ($attribute) {
  34.             case self::CHECK:
  35.                 /** @var User $student */
  36.                 $student $subject->getStudent();
  37.                 if($student->getCurator()->getId() == $user->getId()) {
  38.                     return true;
  39.                 }
  40.                 return false;
  41.                 break;
  42.             case self::CAN_HANDOVER:
  43.                 if (
  44.                     !$subject->getId()
  45.                     || (
  46.                         $user == $subject->getStudent()
  47.                         && (
  48.                             $subject->getHomework()->getAutocheck()
  49.                             && $subject->hasAttempts()
  50.                             || (
  51.                                 !$subject->hasAttempts()
  52.                                 && HomeworkResultStatusEnum::STATUS_ON_REVIEW == $subject->getStatus()
  53.                             )
  54.                         )
  55.                         || (
  56.                             !$subject->getHomework()->getAutocheck()
  57.                             && HomeworkResultStatusEnum::STATUS_DONE != $subject->getStatus()
  58.                         )
  59.                     )
  60.                 ) {
  61.                     return true;
  62.                 }
  63.                 break;
  64.         }
  65.         return false;
  66.     }
  67. }