src/Security/Voter/Mentor/UserDetailsVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\Mentor;
  3. use App\Entity\Course\Course;
  4. use App\Entity\User\User;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. class UserDetailsVoter extends Voter
  9. {
  10.     public const SHOW 'MENTOR_SHOW_STUDENT_DETAILS';
  11.     protected function supports(string $attribute$subject): bool
  12.     {
  13.         return in_array($attribute, [self::SHOW])
  14.             && $subject instanceof User
  15.         ;
  16.     }
  17.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  18.     {
  19.         /** @var User $mentor */
  20.         $mentor $token->getUser();
  21.         // if the mentor is anonymous, do not grant access
  22.         if (!$mentor instanceof UserInterface) {
  23.             return false;
  24.         }
  25.         /** @var User $student */
  26.         $student $subject;
  27.         switch ($attribute) {
  28.             case self::SHOW:
  29.                 /* @var Course $course */
  30.                 foreach ($mentor->getLmsCourses() as $course) {
  31.                     if ($course->getCourse()->getUsers()->contains($student)) {
  32.                         return true;
  33.                     }
  34.                 }
  35.         }
  36.         return false;
  37.     }
  38. }