<?php
namespace App\Security\Voter;
use App\Entity\CourseType;
use App\User\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class CourseTypeVoter extends Voter
{
public const VIEW = 'view';
public const EDIT = 'edit';
public const DELETE = 'delete';
protected function supports(string $attribute, $subject): bool
{
return in_array($attribute, [self::VIEW, self::EDIT, self::DELETE], true)
&& $subject instanceof CourseType;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
if (in_array('ROLE_SUPER_USER', $user->getRoles(), true)) {
return true;
}
/** @var CourseType $courseType */
$courseType = $subject;
$userClient = $user->getClient();
if (!$userClient) {
return false;
}
$typeClient = $courseType->getClient();
if (!$typeClient) {
return false;
}
if ($userClient->getId() !== $typeClient->getId()) {
return false;
}
return in_array('ROLE_MANAGER', $user->getRoles(), true)
|| in_array('ROLE_ADMIN', $user->getRoles(), true);
}
}