<?php
namespace Nellapp\Bundle\SDKBundle\Developers\Security\Voter;
use Nellapp\Bundle\SDKBundle\Channel\Entity\ChannelInterface;
use Nellapp\Bundle\SDKBundle\Developers\ChannelScopeAuthorisationService;
use Nellapp\Bundle\SDKBundle\GlobalGetters\Service\ConfigurationService;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
class ChannelScopeVoter extends Voter
{
public function __construct(
private ChannelScopeAuthorisationService $channelScopeAuthorisationService,
private ConfigurationService $configurationService,
) {}
protected function supports($attribute, $subject): bool
{
return is_string($attribute)
&& str_starts_with($attribute, 'channel:')
&& $subject instanceof ChannelInterface;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
if (!$this->configurationService->getConfiguration($subject)?->isEnableDevelopersApi()) {
throw new NotFoundHttpException();
}
return $this->channelScopeAuthorisationService->canAccess($attribute, $subject);
}
}