vendor/nellapp/sdk-bundle/src/Permission/Security/Voter/ChannelUserDataVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace Nellapp\Bundle\SDKBundle\Permission\Security\Voter;
  3. use Nellapp\Bundle\SDKBundle\Auth\Entity\UserInterface;
  4. use Nellapp\Bundle\SDKBundle\Channel\Entity\ChannelInterface;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class ChannelUserDataVoter extends Voter
  8. {
  9.     const IS_CHANNEL_USER_DATA 'IS_CHANNEL_USER_DATA'// ChannelUserData that are not learner
  10.     const IS_LEARNER 'IS_LEARNER';
  11.     public function supportsType(string $subjectType): bool
  12.     {
  13.         return is_subclass_of($subjectTypeChannelInterface::class);
  14.     }
  15.     public function supportsAttribute(string $attribute): bool
  16.     {
  17.         return $attribute === self::IS_CHANNEL_USER_DATA || $attribute === self::IS_LEARNER;
  18.     }
  19.     protected function supports(string $attribute$subject): bool
  20.     {
  21.         return $this->supportsAttribute($attribute) && $this->supportsType($subject::class);
  22.     }
  23.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  24.     {
  25.         $user $token->getUser();
  26.         if (!$user instanceof UserInterface) {
  27.             return false;
  28.         }
  29.         $channelUserData $user->getChannelUserDataByChannel($subject);
  30.         if ($channelUserData === null) {
  31.             return false;
  32.         }
  33.         if ($channelUserData->isActive() !== true) {
  34.             return false;
  35.         }
  36.         if ($attribute === self::IS_CHANNEL_USER_DATA) {
  37.             return $channelUserData->isLearner() === false;
  38.         }
  39.         return $channelUserData->isLearner();
  40.     }
  41. }