vendor/nen/kennisbank-platform/src/Nen/Controller/SubscriptionController.php line 58

Open in your IDE?
  1. <?php
  2. namespace Nen\Bundle\KennisbankPlatformBundle\Controller;
  3. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Doctrine\DBAL\Driver\Connection;
  10. use Nen\Bundle\KennisbankPlatformBundle\Entity\BoltPage;
  11. use Nen\Bundle\KennisbankPlatformBundle\Repository\BoltSubscriptionRepository;
  12. use Nen\Bundle\KennisbankPlatformBundle\Repository\UserRepository;
  13. use Nen\Bundle\KennisbankPlatformBundle\Repository\CompanyRepository;
  14. use Nen\Bundle\KennisbankPlatformBundle\Entity\BoltSubscription;
  15. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  16. use Nen\Bundle\KennisbankPlatformBundle\Form\RegistrationType;
  17. use Nen\Bundle\KennisbankPlatformBundle\Form\UpgradeLicenseType;
  18. use Nen\Bundle\KennisbankPlatformBundle\Form\UserPasswordType;
  19. use App\Entity\User;
  20. use Nen\Bundle\KennisbankPlatformBundle\Model\RegistrationModel;
  21. use Nen\Bundle\KennisbankPlatformBundle\Model\UpgradeLicenseModel;
  22. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  23. use Nen\Bundle\KennisbankPlatformBundle\Support\CodeGeneratorInterface;
  24. use Nen\Bundle\KennisbankPlatformBundle\Exceptions\InvalidTokenException;
  25. use App\Entity\Company;
  26. use Nen\Bundle\KennisbankPlatformBundle\Handler\SubscriptionHandler;
  27. use Nen\Bundle\KennisbankPlatformBundle\Handler\UserActivationHandler;
  28. use Symfony\Component\HttpFoundation\Session\Session;
  29. use Illuminate\Support\Collection;
  30. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  31. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  32. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  33. use function Symfony\Component\Translation\t;
  34. class SubscriptionController extends AbstractController
  35. {
  36.     private $params;
  37.     private $codeGenerator;
  38.     public function __construct(ParameterBagInterface $paramsCodeGeneratorInterface $codeGenerator)
  39.     {
  40.         $this->params $params;
  41.         $this->codeGenerator $codeGenerator;
  42.     }
  43.     /**
  44.      * @Route("/abonnementen", name="subscription_index")
  45.      *
  46.      * @Entity("page", expr="repository.findOneByGeneralSectionAndSlug('abonnementen')")
  47.      *
  48.      * @param BoltPage                   $page
  49.      * @param BoltSubscriptionRepository $subscriptionRepository
  50.      * @return Response
  51.      */
  52.     public function index(BoltPage $pageBoltSubscriptionRepository $subscriptionRepository)
  53.     {
  54.         if ($this->getUser()) {
  55.            return $this->redirectToRoute('gebruikers_omzetten');
  56.         }
  57.         $licensesConfig $this->params->get('licenses');
  58.         $licenses $subscriptionRepository->findBy([], ['sorting' => 'ASC']);
  59.         return $this->render('@KennisbankPlatform/pages/abonnementen.html.twig', [
  60.             'page' => $page,
  61.             'licenses' => $licenses
  62.         ]);
  63.     }
  64.     /**
  65.      * @Route("/abonnementen/{slug}", name="abonnementen_detail")
  66.      *
  67.      */
  68.     public function abonnementenDetail(
  69.         Request $request,
  70.         BoltSubscription $subscription,
  71.         SubscriptionHandler $subscriptionHandler,
  72.         UserRepository $userRepository,
  73.         EventDispatcherInterface $eventDispatcher
  74.     ) {
  75.         $isUpgrade = (bool) $request->query->get('upgrade'false);
  76.         if ($isUpgrade) {
  77.             if (! $user $this->getUser()) {
  78.                 $session $request->getSession();
  79.                 if ($id $session->get('expired')) {
  80.                     $user $userRepository->find($id);
  81.                 }
  82.                 if (! $user) {
  83.                     $this->addFlash('error't('Log in om uw abonnement om te zetten.'));
  84.                     return $this->redirectToRoute('home');
  85.                 }
  86.             }
  87.             $company $user->getCompany();
  88.             $model = new UpgradeLicenseModel();
  89.             $type UpgradeLicenseType::class;
  90.         } else {
  91.             $user = new User();
  92.             $company = new Company();
  93.             $model = new RegistrationModel();
  94.             $type RegistrationType::class;
  95.         }
  96.         $company->setLicenseAmount($request->query->get('userAmount'1));
  97.         $user->setCompany($company);
  98.         $options = [
  99.             'license' => $subscription
  100.         ];
  101.         $model->setUser($user);
  102.         if ($request->query->has('jwt')) {
  103.             $model->setJwt($request->query->get('jwt'));
  104.         }
  105.         if ($isUpgrade) {
  106.             $options['method'] = 'PATCH';
  107.         }
  108.         $form $this->createForm($type$model$options);
  109.         $form->handleRequest($request);
  110.         if ($form->isSubmitted() && $form->isValid()) {
  111.             if ($form->has('hash')) {
  112.                 $hash $form->get('hash')->getData();
  113.             } else {
  114.                 $hash null;
  115.             }
  116.             $subscriptionHandler->handle($user$subscription$hash$isUpgrade$model->getJwt());
  117.             if ($isUpgrade) {
  118.                 $this->addFlash('success't('Uw abonnement is succesvol omgezet.'));
  119. //                $token = new UsernamePasswordToken($user, $user->getPassword(), "public", $user->getRoles());
  120. //                // For older versions of Symfony, use security.context here
  121. //                $this->get("security.token_storage")->setToken($token);
  122. //                // Fire the login event
  123. //                // Logging the user in above the way we do it doesn't do this automatically
  124. //                $event = new InteractiveLoginEvent($request, $token);
  125. //                $eventDispatcher->dispatch("security.interactive_login", $event);
  126.                 return $this->redirectToRoute('mijnomgeving_account_index');
  127.             }
  128.             $this->addFlash('success't('U ontvangt een e-mail om uw gebruiker te activeren via %username%.', ['%username%' => $user->getUsername()]));
  129.             return $this->redirectToRoute('home');
  130.         }
  131.         return $this->render('@KennisbankPlatform/pages/abonnement.html.twig', [
  132.             // 'page' => $page,
  133.             'license' => $subscription,
  134.             'form' => $form->createView(),
  135.             'is_upgrade' => $isUpgrade
  136.         ]);
  137.     }
  138.     /**
  139.      * @Route("/activeren", name="activate")
  140.      */
  141.     public function activate(Request $requestUserRepository $userRepositoryUserPasswordHasherInterface $passwordHasherUserActivationHandler $userActivationHandler)
  142.     {
  143.         $token $request->query->get('token');
  144.         try {
  145.             $user $userRepository->validateToken($this->codeGenerator$token);
  146.         } catch (InvalidTokenException $e) {
  147.             return $this->render('@KennisbankPlatform/subscriptions/activate.html.twig', [
  148.             'error' => true,
  149.             'message' => 'Ongeldige token.'
  150.             ]);
  151.         }
  152.         if (!empty($user->getPasswordAt()) && empty($user->getActivationAt())) {
  153.             $userActivationHandler->handle($user);
  154.             $this->addFlash('success't('Uw abonnement is nu actief. Log rechtsboven in om gebruik te maken van uw abonnement.'));
  155.             return $this->redirectToRoute('home');
  156.         }
  157.         if (!empty($user->getActivationAt())) {
  158.             $this->addFlash('error't('Uw account is al geactiveerd. Bent u uw wachtwoord vergeten? Vraag dan opnieuw uw wachtwoord aan.'));
  159.             return $this->redirectToRoute('home');
  160.         }
  161.         $form $this->createForm(UserPasswordType::class, $user);
  162.         $form->handleRequest($request);
  163.         if ($form->isSubmitted() && $form->isValid()) {
  164.             $userActivationHandler->handle($user$form);
  165.             // encode the plain password
  166.             $user->setPassword(
  167.                 $passwordHasher->hashPassword(
  168.                     $user,
  169.                     $form->get('hash')->getData()
  170.                 )
  171.             );
  172.             $user->setActivationAt(new \DateTime("now"));
  173.             $entityManager $this->getDoctrine()->getManager();
  174.             $entityManager->persist($user);
  175.             $entityManager->flush();
  176.             $this->addFlash('success't('Uw abonnement is nu actief. Log rechtsboven in om gebruik te maken van uw abonnement.'));
  177.             return $this->redirectToRoute('home');
  178.         }
  179.         return $this->render('@KennisbankPlatform/subscriptions/activate.html.twig', [
  180.             'error' => false,
  181.             'passwordForm' => $form->createView()
  182.         ]);
  183.     }
  184.     /**
  185.      * @Route("/omzetten", name="gebruikers_omzetten")
  186.      */
  187.     public function omzetten(Request $requestUserRepository $userRepositoryBoltSubscriptionRepository $subscriptionRepository)
  188.     {
  189.         if ($request->getSession()->has('expired')) {
  190.             /** @var User $user */
  191.             $user $userRepository->find((int)$request->getSession()->get('expired'));
  192.         } else {
  193.             /** @var User $user */
  194.             $user $this->getUser();
  195.         }
  196.         $subscription $subscriptionRepository->findOneBy(['slug' => $user->getCompany()->getLicenseType()]);
  197.         // When a subscription was not found, fallback on the previous method
  198.         // of deciding is a subscription can be upgraded.
  199.         if (empty($subscription)) {
  200.             if ($user && !$user->getCompany()->canUpgradeLicense()) {
  201.                 $this->addFlash('error't('U kunt dit abonnement niet omzetten.'));
  202.                 return $this->redirectToRoute('home');
  203.             }
  204.         }
  205.         $convertSubscription $subscription->getConvertSubscription();
  206.         if (empty($convertSubscription)) {
  207.             $this->addFlash('error't('U kunt dit abonnement niet omzetten.'));
  208.             return $this->redirectToRoute('home');
  209.         }
  210.         $licenses $subscriptionRepository->findBy([
  211.             'slug' => $convertSubscription
  212.         ], ['sorting' => 'ASC']);
  213.         $licenses Collection::make($licenses)->keyBy(function ($o) {
  214.                 return $o->getSlug();
  215.             })->toArray();
  216.         unset($licenses['trial']);
  217.         return $this->render('@KennisbankPlatform/subscriptions/omzetten.twig', [
  218.             'record' => $user,
  219.             'licenses' => $licenses
  220.         ]);
  221.     }
  222. }