src/Security/Voter/SubstrateVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\CommentTree;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use App\Entity\HomeworkResultComment;
  7. use App\Service\RoleCheckerService;
  8. class SubstrateVoter extends Voter
  9. {
  10.     public const SUBSTRATE_SHOW 'SUBSTRATE_SHOW';
  11.     public const SUBSTRATE_SHOW_TREE 'SUBSTRATE_SHOW_TREE';
  12.     private $security;
  13.     
  14.     public function __construct(RoleCheckerService $security)
  15.     {
  16.         $this->security $security;
  17.     }
  18.     
  19.     protected function supports($attribute$subject)
  20.     {
  21.         return in_array($attribute, [self::SUBSTRATE_SHOWself::SUBSTRATE_SHOW_TREE]);
  22.     }
  23.     /**
  24.      * @var HomeworkResultComment|CommentTree $subject
  25.      */
  26.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  27.     {
  28.         
  29.         switch ($attribute) {
  30.             case 'SUBSTRATE_SHOW':
  31.                 if($this->security->isGranted($subject->getAuthor(), 'ROLE_ADMIN') && $subject->getForEvaluation()){
  32.                     return true;
  33.                 }
  34.                 break;
  35.             case 'SUBSTRATE_SHOW_TREE':
  36.                 if($subject->getLevel() > 0) {
  37.                     return false;
  38.                 }
  39.                 if($this->security->isGranted($subject->getAuthor(), 'ROLE_ADMIN')) {
  40.                     return true;
  41.                 }
  42.                 $curator $subject->getHomeworkResult()->getCurator();
  43.                 if(!$curator) {
  44.                     $curator $subject->getHomeworkResult()->getStudent()->getCurator();
  45.                 }
  46.                 if(!$curator) return false;
  47.                 if($curator->getId() === $subject->getAuthor()->getId()){
  48.                     return true;
  49.                 }
  50.                 break;
  51.         }
  52.         return false;
  53.     }
  54. }