<?php
namespace App\Security\Voter;
use App\Entity\Educational\Lesson\WorkList;
use App\Entity\Users\User;
use App\Repository\Management\SubscriptionRepository;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
class AccessPlaylistVoter extends \Symfony\Component\Security\Core\Authorization\Voter\Voter
{
public const ACCESS_PLAYLIST_SUBSCRIBER = 'ACCESS_ONLY_SUBSCRIBER';
public function __construct(SubscriptionRepository $subscriptionRepository,UrlGeneratorInterface $router,SessionInterface $session){
$this->subscriptionRepository = $subscriptionRepository;
$this->router = $router;
$this->session = $session;
}
/**
* @inheritDoc
*/
protected function supports(string $attribute, $subject)
{
if (!in_array($attribute, [self::ACCESS_PLAYLIST_SUBSCRIBER,])) {
return false;
}
// only vote on `Post` objects
if (!$subject instanceof WorkList) {
return false;
}
return true;
}
/**
* @inheritDoc
*/
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token)
{
// TODO: Implement voteOnAttribute() method.
$user = $token->getUser();
if (!$user instanceof User) {
// the user must be logged in; if not, deny access
return false;
}
$last = $this->subscriptionRepository->getLastSubscriptionByChild($user);
if(!$last){
return false;
}
$now = new \DateTime('now');
$limit = \DateTime::createFromFormat('Y-m-d H:i:s',$now->format('Y').'-09-01 00:00:00', null);
if(
(
(is_null($last->getUnSubscripted()) || (!is_null($last->getUnSubscripted()) && $last->getUnSubscripted()>$now))
&& !in_array("ROLE_FREE",$user->getRoles())
&& (is_null($last->isDateSuspended()) || (!is_null($last->isDateSuspended()) && $limit<$now) )
)
){
return true;
}else{
return $subject instanceof WorkList && $subject->getFreeAccess();
}
}
}