<?php
namespace App\Security\Voter;
use App\Entity\Collective;
use App\Entity\ContractLine;
use App\Entity\User;
use LogicException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class ContractLineVoter extends Voter
{
public const EDIT = 'contract-line-edit';
/**
* @inheritDoc
*/
protected function supports($attribute, $subject): bool
{
// if the attribute isn't one we support, return false
if ($attribute != self::EDIT) {
return false;
}
if (!$subject instanceof ContractLine) {
return false;
}
return true;
}
/**
* @inheritDoc
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
// the user must be logged in; if not, deny access
return false;
}
switch ($attribute) {
case self::EDIT:
return $this->canEdit($user, $subject);
}
throw new LogicException('This code should not be reached!');
}
/**
* @param User $user
* @param ContractLine $line
* @return bool
*/
private function canEdit(User $user, ContractLine $line): bool
{
return ($user->hasRole('ROLE_ATCLIENTE_ADMIN') && $line->getEnabledRelevantPeriod());
}
}