src/Security/Voter/AccessVoter.php line 12
<?php
namespace App\Security\Voter;
use App\Entity\Account;
use App\Entity\AccountInterface;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Uid\Uuid;
class AccessVoter extends Voter
{
public function __construct(private readonly Security $security)
{
}
protected function supports(string $attribute, mixed $subject): bool
{
return $attribute === 'CAN_ACCESS' && ($subject instanceof AccountInterface or $subject instanceof Account);
}
/**
* @param string $attribute
* @param AccountInterface $subject
* @param TokenInterface $token
* @return bool
*/
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
if($this->security->isGranted('ROLE_ADMIN')){
return true;
}
if ($subject instanceof Account) {
return $subject->getId()->equals(Uuid::fromString($token->getUser()->account));
}
return $subject->getAccount()->getId()->equals(Uuid::fromString($token->getUser()->account));
}
}