<?php
namespace App\Security\Voter;
use App\Entity\Course;
use App\Repository\OAuth\ClientRepository;
use App\User\Entity\Client;
use App\User\Entity\User;
use League\Bundle\OAuth2ServerBundle\Security\User\NullUser;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
/**
* Security voter to control access to Course entities based on client ownership.
* Note: This is separate from CourseVoter which handles Provider access.
*/
class CourseSecurityVoter extends Voter
{
public const VIEW = 'view';
public const EDIT = 'edit';
public const DELETE = 'delete';
private ClientRepository $clientRepository;
public function __construct(ClientRepository $clientRepository)
{
$this->clientRepository = $clientRepository;
}
protected function supports(string $attribute, $subject): bool
{
return in_array($attribute, [self::VIEW, self::EDIT, self::DELETE], true)
&& $subject instanceof Course;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
// Determine the client based on user type
$userClient = null;
if ($user instanceof User) {
$userClient = $user->getClient();
// ROLE_SUPER_USER can access everything
if (in_array('ROLE_SUPER_USER', $user->getRoles(), true)) {
return true;
}
} elseif ($user instanceof NullUser) {
// OAuth2 client credentials flow - get client from token
$oauthClientId = $token->getAttribute('oauth_client_id');
if ($oauthClientId) {
$oauthClient = $this->clientRepository->find($oauthClientId);
if ($oauthClient) {
$userClient = $oauthClient->getApplicationClient();
}
}
} else {
// Unknown user type
return false;
}
/** @var Course $course */
$course = $subject;
// Check if we have a valid user client
if (!$userClient) {
return false;
}
// Get the client of the course (direct FK)
$courseClient = $course->getClient();
if (!$courseClient) {
return false;
}
// Check if both belong to the same client
if ($userClient->getId() !== $courseClient->getId()) {
return false;
}
// For OAuth2 NullUser (API access), if client matches, allow VIEW access
if ($user instanceof NullUser && $attribute === self::VIEW) {
return true;
}
// For regular User, check roles
if ($user instanceof User) {
// ROLE_MANAGER and ROLE_ADMIN can access all courses of their client
if (in_array('ROLE_MANAGER', $user->getRoles(), true) || in_array('ROLE_ADMIN', $user->getRoles(), true)) {
return true;
}
// ROLE_SPEAKER can access courses they are assigned to as speaker
// Speakers are assigned to CourseOccurrences, not directly to Course
if (in_array('ROLE_SPEAKER', $user->getRoles(), true)) {
// Check if speaker is assigned to any occurrence of this course
foreach ($course->getOccurrences() as $occurrence) {
foreach ($occurrence->getSpeakers() as $speaker) {
if ($speaker->getUser() && $speaker->getUser()->getId() === $user->getId()) {
return true;
}
}
}
}
}
return false;
}
}