src/Security/Voter/ElearningPlatform/MemberAccessVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\ElearningPlatform;
  3. use App\Entity\User;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. /**
  7.  * @extends Voter<string, mixed>
  8.  */
  9. class MemberAccessVoter extends Voter
  10. {
  11.     public const MEMBER_ACCESS 'member_access';
  12.     public const ANONYMOUS_USER 'anon.';
  13.     protected function supports(string $attribute$subject): bool
  14.     {
  15.         return self::MEMBER_ACCESS === $attribute;
  16.     }
  17.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  18.     {
  19.         /** @var User|null $user */
  20.         $user $token->getUser();
  21.         if (!$user instanceof User) {
  22.             return false;
  23.         }
  24.         $userRoles $user->getRoles();
  25.         if (empty($userRoles)) {
  26.             return false;
  27.         }
  28.         $member $user->getMember();
  29.         if (null === $member) {
  30.             return false;
  31.         }
  32.         if (true === $member->isDeleted()) {
  33.             return false;
  34.         }
  35.         return true;
  36.     }
  37. }