<?php
namespace App\Security\Voter\Mentor;
use App\Entity\Course\Course;
use App\Entity\User\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class CourseMentorVoter extends Voter
{
public const VIEW = 'MENTOR_IS_COURSE_MENTOR';
protected function supports(string $attribute, $subject): bool
{
return in_array($attribute, [self::VIEW])
&& $subject instanceof Course;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
/** @var User $user */
$user = $user;
/** @var Course $course */
$course = $subject;
switch ($attribute) {
case self::VIEW:
return $course->getLmsCourse()->getMentor() === $user;
}
return false;
}
}