<?php
namespace Nellapp\Bundle\SDKBundle\Permission\Security\Voter;
use Nellapp\Bundle\SDKBundle\Auth\Entity\UserInterface;
use Nellapp\Bundle\SDKBundle\Channel\Entity\ChannelInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class ChannelUserDataVoter extends Voter
{
const IS_CHANNEL_USER_DATA = 'IS_CHANNEL_USER_DATA'; // ChannelUserData that are not learner
const IS_LEARNER = 'IS_LEARNER';
public function supportsType(string $subjectType): bool
{
return is_subclass_of($subjectType, ChannelInterface::class);
}
public function supportsAttribute(string $attribute): bool
{
return $attribute === self::IS_CHANNEL_USER_DATA || $attribute === self::IS_LEARNER;
}
protected function supports(string $attribute, $subject): bool
{
return $this->supportsAttribute($attribute) && $this->supportsType($subject::class);
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof UserInterface) {
return false;
}
$channelUserData = $user->getChannelUserDataByChannel($subject);
if ($channelUserData === null) {
return false;
}
if ($channelUserData->isActive() !== true) {
return false;
}
if ($attribute === self::IS_CHANNEL_USER_DATA) {
return $channelUserData->isLearner() === false;
}
return $channelUserData->isLearner();
}
}