src/Security/Voter/Scholar/Front/LessonVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\Scholar\Front;
  3. use App\Entity\Scholar\Lesson\Lesson;
  4. use App\Entity\Scholar\Module\Module;
  5. use App\Entity\Scholar\Training\Training;
  6. use App\Service\Scholar\ScholarNavigationSecurityService;
  7. use Nellapp\Bundle\SDKBundle\Auth\Entity\UserInterface;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. class LessonVoter extends Voter
  11. {
  12.     public function __construct(
  13.         private ScholarNavigationSecurityService $scholarNavigationSecurityService,
  14.     )
  15.     {
  16.     }
  17.     private function isSubjectValid($subject): bool
  18.     {
  19.         if (!is_array($subject)) {
  20.             return false;
  21.         }
  22.         if (
  23.             !array_key_exists('training'$subject)
  24.             || !array_key_exists('module'$subject)
  25.             || !array_key_exists('lesson'$subject)
  26.         ) {
  27.             return false;
  28.         }
  29.         $training $subject['training'];
  30.         $module $subject['module'];
  31.         $lesson $subject['lesson'];
  32.         return $training instanceof Training && $module instanceof Module && $lesson instanceof Lesson;
  33.     }
  34.     public function supportsAttribute(string $attribute): bool
  35.     {
  36.         return str_starts_with($attribute'FRONT_ACCESS');
  37.     }
  38.     public function supportsType(string $subjectType): bool
  39.     {
  40.         return $subjectType === 'array';
  41.     }
  42.     protected function supports(string $attribute$subject): bool
  43.     {
  44.         return $this->isSubjectValid($subject) && $this->supportsAttribute($attribute);
  45.     }
  46.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  47.     {
  48.         $user $token->getUser();
  49.         if (!$user instanceof UserInterface) {
  50.             return false;
  51.         }
  52.         if ($attribute === 'FRONT_ACCESS') {
  53.             return $this->scholarNavigationSecurityService->canAccessLesson(
  54.                 $subject['training'],
  55.                 $subject['module'],
  56.                 $subject['lesson'],
  57.             );
  58.         }
  59.         if ($attribute === 'FRONT_ACCESS_REVIEW') {
  60.             return $this->scholarNavigationSecurityService->canAccessLessonReview(
  61.                 $subject['training'],
  62.                 $subject['module'],
  63.                 $subject['lesson'],
  64.             );
  65.         }
  66.         return false;
  67.     }
  68. }