<?php
namespace App\Tasks\Security\Voter;
use App\Core\Entity\User;
use App\Tasks\Entity\Issue;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
final class IssueVoter extends Voter
{
const ISSUE_ACCESS = 'issue_access';
/**
* {@inheritdoc}
*/
public function supports(string $attribute, $subject): bool
{
return $subject instanceof Issue && in_array($attribute, [
self::ISSUE_ACCESS,
]);
}
/**
* {@inheritdoc}
*
* @param Issue $subject
*/
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
if ($attribute === self::ISSUE_ACCESS) {
return $subject->getPerson()->getId() === $user->getId();
}
return false;
}
}