<?php
namespace App\Security\Voter;
use App\Entity\CourseData;
use App\User\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class CourseDataVoter 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 CourseData;
}
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 CourseData $courseData */
$courseData = $subject;
$userClient = $user->getClient();
if (!$userClient || !$courseData->getClient()) {
return false;
}
if ($userClient->getId() !== $courseData->getClient()->getId()) {
return false;
}
return in_array('ROLE_MANAGER', $user->getRoles(), true);
}
}