<?php
declare(strict_types=1);
namespace App\Security\Voter;
use App\DBAL\Types\RoleEnumType;
use App\Service\DutyService;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
class CanWriteMessageToCoordinatorVoter extends Voter
{
public const CAN_WRITE_MESSAGE_TO_COORDINATOR = 'CAN_WRITE_MESSAGE_TO_COORDINATOR';
private Security $security;
private DutyService $dutyService;
public function __construct(
Security $security,
DutyService $dutyService
) {
$this->security = $security;
$this->dutyService = $dutyService;
}
protected function supports($attribute, $subject): bool
{
return self::CAN_WRITE_MESSAGE_TO_COORDINATOR === $attribute;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
if (
$this->security->isGranted(RoleEnumType::ROLE_LIMITED_ACCESS)
|| $this->security->isGranted(RoleEnumType::ROLE_DEMO)
|| $this->security->isGranted(RoleEnumType::ROLE_INTRO_STUDENT)
|| $this->security->isGranted(RoleEnumType::ROLE_DEMO_INTENSIVE)
) {
return false;
}
if (!$this->dutyService->issetDutyCoordinators()) {
return false;
}
return true;
}
}