src/Security/Voter/AccessPlaylistVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Educational\Lesson\WorkList;
  4. use App\Entity\Users\User;
  5. use App\Repository\Management\SubscriptionRepository;
  6. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  7. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. class AccessPlaylistVoter extends \Symfony\Component\Security\Core\Authorization\Voter\Voter
  10. {
  11.     public const ACCESS_PLAYLIST_SUBSCRIBER 'ACCESS_ONLY_SUBSCRIBER';
  12.     public function __construct(SubscriptionRepository $subscriptionRepository,UrlGeneratorInterface $router,SessionInterface $session){
  13.         $this->subscriptionRepository $subscriptionRepository;
  14.         $this->router $router;
  15.         $this->session $session;
  16.     }
  17.     /**
  18.      * @inheritDoc
  19.      */
  20.     protected function supports(string $attribute$subject)
  21.     {
  22.         if (!in_array($attribute, [self::ACCESS_PLAYLIST_SUBSCRIBER,])) {
  23.             return false;
  24.         }
  25.         // only vote on `Post` objects
  26.         if (!$subject instanceof WorkList) {
  27.             return false;
  28.         }
  29.         return true;
  30.     }
  31.     /**
  32.      * @inheritDoc
  33.      */
  34.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token)
  35.     {
  36.         // TODO: Implement voteOnAttribute() method.
  37.         $user $token->getUser();
  38.         if (!$user instanceof User) {
  39.             // the user must be logged in; if not, deny access
  40.             return false;
  41.         }
  42.         $last $this->subscriptionRepository->getLastSubscriptionByChild($user);
  43.         if(!$last){
  44.             return false;
  45.         }
  46.         $now = new \DateTime('now');
  47.         $limit \DateTime::createFromFormat('Y-m-d H:i:s',$now->format('Y').'-09-01 00:00:00'null);
  48.         if(
  49.             (
  50.                 (is_null($last->getUnSubscripted()) || (!is_null($last->getUnSubscripted()) && $last->getUnSubscripted()>$now))
  51.                 && !in_array("ROLE_FREE",$user->getRoles())
  52.                 && (is_null($last->isDateSuspended()) || (!is_null($last->isDateSuspended()) && $limit<$now) )
  53.             )
  54.         ){
  55.             return true;
  56.         }else{
  57.             return $subject instanceof WorkList && $subject->getFreeAccess();
  58.         }
  59.     }
  60. }