src/Security/Voter/SubscriberVoter.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Users\Child;
  4. use App\Repository\Management\SubscriptionRepository;
  5. use App\Service\StripeService;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  12. use Symfony\Component\Security\Core\User\UserInterface;
  13. class SubscriberVoter extends Voter
  14. {
  15.     public const ACCESS_TUTORAT 'ACCESS_TUTORAT';
  16.     public const ACCESS_LBP 'ACCESS_LBP';
  17.     public const ACCESS_FREE 'ACCESS_FREE';
  18.     public const ACCESS_ONLY_FREE 'ACCESS_ONLY_FREE';
  19.     private $subscriptionRepository;
  20.     private $router;
  21.     private $session;
  22.     private $stripe;
  23.     private $entityManager;
  24.     public function __construct(SubscriptionRepository $subscriptionRepository,UrlGeneratorInterface $router,SessionInterface $session,StripeService $stripeService,EntityManagerInterface $entityManager){
  25.         $this->subscriptionRepository $subscriptionRepository;
  26.         $this->router $router;
  27.         $this->session $session;
  28.         $this->stripe $stripeService;
  29.         $this->entityManager $entityManager;
  30.     }
  31.     protected function supports(string $attribute$subject): bool
  32.     {
  33.         // replace with your own logic
  34.         // https://symfony.com/doc/current/security/voters.html
  35.         return in_array($attribute, [self::ACCESS_LBPself::ACCESS_TUTORAT,self::ACCESS_FREE,self::ACCESS_ONLY_FREE]);
  36.     }
  37.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token)
  38.     {
  39.         $user $token->getUser();
  40.         // if the user is anonymous, do not grant access
  41.         if (!$user instanceof Child) {
  42.             return false;
  43.         }
  44.         $last $this->subscriptionRepository->getLastSubscriptionByChild($user);
  45.         if(!$last){
  46.             return false;
  47.         }
  48.         $now = new \DateTime('now');
  49.         switch ($attribute) {
  50.             case self::ACCESS_FREE:
  51.                 return !in_array("ROLE_FREE",$user->getRoles());
  52.                 break;
  53.             case self::ACCESS_LBP:
  54.                 if(!is_null($last->getUnSubscripted())){
  55.                     if($last->getUnSubscripted()<$now){
  56.                         //$this->session->getFlashBag()->add('notice','Vous n\'avez plus accès à ce service car vous êtes désabonné');
  57.                         return false;
  58.                     }
  59.                 }
  60.                 if($last->isInactif()){
  61.                     if($last->getInactifAt() && $last->getInactifAt()>$now){
  62.                         if($last->getStripeId()){
  63.                             $tmp  $this->stripe->getStripeClient()->subscriptions->retrieve($last->getStripeId());
  64.                             if($tmp && in_array($tmp->status,['active'])){
  65.                                 $last->setInactif(false);
  66.                                 $this->entityManager->flush();
  67.                                 return true;
  68.                             }
  69.                         }
  70.                         if(!is_null($last->getPaymentFutur())){
  71.                             $p $last->getPaymentFutur();
  72.                             if(!is_null($p->getInvoice())){
  73.                                 $inv $this->stripe->getStripeClient()->invoices->retrieve($p->getInvoice());
  74.                                 if($inv && $inv->paid){
  75.                                     $last->setInactif(false);
  76.                                     $this->entityManager->flush();
  77.                                     return true;
  78.                                 }
  79.                             }
  80.                         }
  81.                     }
  82.                     if($last->getInactifAt() && $last->getInactifAt()<$now){
  83.                         $last->setUnSubscripted($now);
  84.                         $this->entityManager->flush();
  85.                         if($last->getStripeId()){
  86.                             $this->stripe->getStripeClient()->subscriptions->cancel($last->getStripeId(),['invoice_now'=>false]);
  87.                         }
  88.                         return false;
  89.                     }
  90.                     return false;
  91.                 }
  92.                 if(is_null($last->getScriptSubscriptionCheckAt()) || $last->getScriptSubscriptionCheckAt() < $now){
  93.                     if($last->getStripeId() && is_null($last->getUnSubscripted()) && !$last->getIsMobile() && !$last->getB2b() && !$last->IsAccountTest() ){
  94.                         $tmp  $this->stripe->getStripeClient()->subscriptions->retrieve($last->getStripeId());
  95.                         $date $last->isTrial()?$last->getTrialDate():$last->getDateUnSubscribed();
  96.                         if($date){
  97.                             $date->modify('+2 days');
  98.                             $last->setScriptSubscriptionCheckAt($date);
  99.                             $this->entityManager->flush();
  100.                         }
  101.                         if(!$tmp || (!in_array($tmp->status,['active','trialing']))){
  102.                             $last->setInactif(true);
  103.                             $expired = (new \DateTime('now'))->setTime(23,59,59,59)->modify("+6 day");
  104.                             $last->setInactifAt($expired);
  105.                             $this->entityManager->flush();
  106.                             return false;
  107.                         }
  108.                     }
  109.                 }
  110.                 return true;
  111.                 // logic to determine if the user can EDIT
  112.                 // return true or false
  113.                 break;
  114.             case self::ACCESS_ONLY_FREE:
  115.                 // logic to determine if the user can VIEW
  116.                 return in_array("ROLE_FREE",$user->getRoles());
  117.                 break;
  118.         }
  119.         return false;
  120.     }
  121. }