src/Security/Voter/AccountVoter.php line 11
<?php
namespace App\Security\Voter;
use App\Entity\Account;
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 AccountVoter extends Voter
{
public function __construct(
private readonly Security $security
) {
}
protected function supports(string $attribute, mixed $subject): bool
{
return in_array($attribute, ['ACCESS_ACCOUNT_EDIT', 'ACCESS_ACCOUNT_READ']) && $subject instanceof Account;
}
/**
* @param string $attribute
* @param Account $subject
* @param TokenInterface $token
* @return bool
*/
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
if ($this->security->isGranted('ROLE_ADMIN')) {
return true;
}
return $this->security->isGranted(str_replace('ACCESS', 'ROLE', $attribute)) &&
$subject->getId()->equals(Uuid::fromString($token->getUser()->account));
}
}