<?php
namespace Nellapp\Bundle\SDKBundle\Permission\Security\Voter;
use Nellapp\Bundle\SDKBundle\Auth\Entity\UserInterface;
use Nellapp\Bundle\SDKBundle\Permission\UsersResource\UsersResourceInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class UsersResourceVoter extends Voter
{
const USER_IN_RESOURCE = 'USER_IN_RESOURCE';
public function supportsType(string $subjectType): bool
{
return is_subclass_of($subjectType, UsersResourceInterface::class);
}
public function supportsAttribute(string $attribute): bool
{
return $attribute === self::USER_IN_RESOURCE;
}
protected function supports(string $attribute, $subject): bool
{
return $this->supportsAttribute($attribute) && $this->supportsType($subject::class);
}
/**
* @param string $attribute
* @param UsersResourceInterface $subject
* @param TokenInterface $token
* @return bool
*/
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof UserInterface) {
return false;
}
return $subject->hasUser($user);
}
}