src/Security/Voter/BlockDatesDeleteVoter.php line 14

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter;
  4. use App\Entity\TimeControl\BlockDates;
  5. use App\Entity\User;
  6. use Symfony\Component\Security\Core\{Authentication\Token\TokenInterfaceAuthorization\Voter\VoterSecurity};
  7. /**
  8.  * Does user can delete the BlockDates period?
  9.  */
  10. class BlockDatesDeleteVoter extends Voter
  11. {
  12.     private Security $security;
  13.     public function __construct(Security $security)
  14.     {
  15.         $this->security $security;
  16.     }
  17.     protected function supports(string $attribute$subject): bool
  18.     {
  19.         return $attribute === 'BLOCKDATES_DELETE' && $subject instanceof BlockDates;
  20.     }
  21.     /**
  22.      * @param BlockDates|mixed $subject
  23.      */
  24.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  25.     {
  26.         $user $token->getUser();
  27.         if (!$user instanceof User) {
  28.             return false;
  29.         }
  30.         $manager $subject->getBlockedBy();
  31.         if ($manager) {
  32.             $blockedBy $manager->getUser();
  33.         } else {
  34.             $blockedBy null;
  35.         }
  36.         if ($user === $blockedBy) {
  37.             return $this->security->isGranted('ROLE_MANAGER');
  38.         } else {
  39.             return $this->security->isGranted('ROLE_ADMIN');
  40.         }
  41.     }
  42. }