<?php
declare(strict_types=1);
namespace App\Security\Voter;
use App\Entity\TimeControl\Task;
use App\Repository\TimeEntryRepository;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class TaskRemoveVoter extends Voter
{
public function __construct(private TimeEntryRepository $timeEntryRepository)
{
}
/**
* @return bool
*/
protected function supports(string $attribute, $subject)
{
return $attribute === 'TASK_REMOVE' && $subject instanceof Task;
}
/**
* @param mixed|Task $subject
*
* @return bool
*/
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
if (!$user instanceof UserInterface) {
return false;
}
if ($this->timeEntryRepository->getFixedTimeEntriesByTask($subject)) {
return false;
}
return true;
}
}