<?php
/*
* This file is part of the nellapp package.
*
* (c) Benjamin Georgeault
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Security\Voter\Scholar\Front;
use App\Entity\Scholar\Training\Training;
use App\Service\Scholar\ScholarNavigationSecurityService;
use Nellapp\Bundle\SDKBundle\Auth\Entity\UserInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
class TrainingVoter implements VoterInterface
{
public function __construct(
private ScholarNavigationSecurityService $scholarNavigationSecurityService,
)
{
}
private function isSubjectValid($subject): bool
{
if (!is_array($subject)) {
return false;
}
if (
!array_key_exists('training', $subject)
|| array_key_exists('module', $subject)
|| array_key_exists('lesson', $subject)
) {
return false;
}
$training = $subject['training'];
return $training instanceof Training;
}
public function vote(TokenInterface $token, $subject, array $attributes): int
{
$user = $token->getUser();
if (!$user instanceof UserInterface) {
return false;
}
if (!$this->isSubjectValid($subject)) {
return self::ACCESS_ABSTAIN;
}
if (!in_array('FRONT_ACCESS', $attributes)) {
return self::ACCESS_ABSTAIN;
}
return $this->scholarNavigationSecurityService
->canAccessTraining($subject['training']) ? self::ACCESS_GRANTED : self::ACCESS_DENIED;
}
}