src/Security/Voter/TaskRemoveVoter.php line 13

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter;
  4. use App\Entity\TimeControl\Task;
  5. use App\Repository\TimeEntryRepository;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. class TaskRemoveVoter extends Voter
  10. {
  11.     public function __construct(private TimeEntryRepository $timeEntryRepository)
  12.     {
  13.     }
  14.     /**
  15.      * @return bool
  16.      */
  17.     protected function supports(string $attribute$subject)
  18.     {
  19.         return $attribute === 'TASK_REMOVE' && $subject instanceof Task;
  20.     }
  21.     /**
  22.      * @param mixed|Task $subject
  23.      *
  24.      * @return bool
  25.      */
  26.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token)
  27.     {
  28.         $user $token->getUser();
  29.         if (!$user instanceof UserInterface) {
  30.             return false;
  31.         }
  32.         if ($this->timeEntryRepository->getFixedTimeEntriesByTask($subject)) {
  33.             return false;
  34.         }
  35.         return true;
  36.     }
  37. }