src/Security/Voter/CanWriteMessageToCoordinatorVoter.php line 13

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter;
  4. use App\DBAL\Types\RoleEnumType;
  5. use App\Service\DutyService;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\Security;
  9. class CanWriteMessageToCoordinatorVoter extends Voter
  10. {
  11.     public const CAN_WRITE_MESSAGE_TO_COORDINATOR 'CAN_WRITE_MESSAGE_TO_COORDINATOR';
  12.     private Security $security;
  13.     private DutyService $dutyService;
  14.     public function __construct(
  15.         Security $security,
  16.         DutyService $dutyService
  17.     ) {
  18.         $this->security $security;
  19.         $this->dutyService $dutyService;
  20.     }
  21.     protected function supports($attribute$subject): bool
  22.     {
  23.         return self::CAN_WRITE_MESSAGE_TO_COORDINATOR === $attribute;
  24.     }
  25.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  26.     {
  27.         if (
  28.             $this->security->isGranted(RoleEnumType::ROLE_LIMITED_ACCESS)
  29.             || $this->security->isGranted(RoleEnumType::ROLE_DEMO)
  30.             || $this->security->isGranted(RoleEnumType::ROLE_INTRO_STUDENT)
  31.             || $this->security->isGranted(RoleEnumType::ROLE_DEMO_INTENSIVE)
  32.         ) {
  33.             return false;
  34.         }
  35.         if (!$this->dutyService->issetDutyCoordinators()) {
  36.             return false;
  37.         }
  38.         return true;
  39.     }
  40. }