<?php
namespace Nellapp\Bundle\SDKBundle\Permission\Security\Voter;
use Nellapp\Bundle\SDKBundle\Auth\Entity\UserInterface;
use Nellapp\Bundle\SDKBundle\Channel\Entity\ChannelInterface;
use Nellapp\Bundle\SDKBundle\Menu\Enum\ChannelMenuEnum;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class ChannelUserMenuVoter extends Voter
{
public function supportsType(string $subjectType): bool
{
return is_subclass_of($subjectType, ChannelInterface::class);
}
public function supportsAttribute(string $attribute): bool
{
return in_array($attribute, ChannelMenuEnum::getChoices());
}
protected function supports(string $attribute, $subject): bool
{
return $this->supportsType($subject::class) && $this->supportsAttribute($attribute);
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof UserInterface) {
return false;
}
$channelUserData = $user->getChannelUserDataByChannel($subject);
if (!$channelUserData || !$channelUserData->isActive()) {
return false;
}
return in_array($attribute, $channelUserData->getDisplayedMenu());
}
}