<?php
namespace Nellapp\Bundle\SDKBundle\Permission\Security\Voter;
use Nellapp\Bundle\SDKBundle\Permission\UserOwner\UserOwnerGetter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class UserOwnerVoter extends Voter
{
const USER_OWNER = 'USER_OWNER';
public function __construct(
private UserOwnerGetter $userOwnerGetter,
) {}
public function supportsType(string $subjectType): bool
{
return $this->userOwnerGetter->supportsType($subjectType);
}
public function supportsAttribute(string $attribute): bool
{
return $attribute === self::USER_OWNER;
}
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 === null) {
return false;
}
return $this->userOwnerGetter->isOwnerOf($user, $subject);
}
}