<?php
declare(strict_types=1);
namespace App\Security\Voter;
use App\Entity\{TimeNormalization\Day, User};
use Symfony\Component\Security\Core\{Authentication\Token\TokenInterface, Authorization\Voter\Voter, Security};
/**
* Does user can change the hours in day?
*/
class DayHoursChangeVoter extends Voter
{
private Security $security;
public function __construct(Security $security)
{
$this->security = $security;
}
protected function supports(string $attribute, $subject): bool
{
return $attribute === 'DAY_HOURS_EDIT' && $subject instanceof Day;
}
/**
* @param Day|mixed $subject
*/
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
return $this->security->isGranted('ROLE_ADMIN');
}
}