<?php
namespace Nellapp\Bundle\SDKBundle\Permission\Security\Voter;
use Nellapp\Bundle\SDKBundle\Auth\Entity\UserInterface;
use Nellapp\Bundle\SDKBundle\Permission\Entity\ChannelResourceInterface;
use Nellapp\Bundle\SDKBundle\Permission\SharablePermission\SharableChannelResourcePermissionService;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class SharableChannelResourcePermissionVoter extends Voter
{
public function __construct(
private SharableChannelResourcePermissionService $sharablePermissionService,
)
{
}
public function supportsAttribute(string $attribute): bool
{
return str_starts_with($attribute, 'CHANNEL_USER_PERM');
}
public function supportsType(string $subjectType): bool
{
return is_subclass_of($subjectType, ChannelResourceInterface::class) && $this->sharablePermissionService->hasSharablePermissionFor($subjectType);
}
protected function supports(string $attribute, $subject): bool
{
return $this->supportsAttribute($attribute) && $this->supportsType($subject::class);
}
/**
* @param string $attribute
* @param ChannelResourceInterface $subject
* @param TokenInterface $token
* @return bool
*/
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof UserInterface) {
return false;
}
$channelUserData = $user->getChannelUserDataByChannel($subject->getOwnerChannel());
if ($channelUserData === null) {
return false;
}
return $this->sharablePermissionService->hasAccess($channelUserData, $subject, $attribute);
}
}