src/Bidcoz/Bundle/FrontendBundle/Controller/CreateOrganizationController.php line 121

Open in your IDE?
  1. <?php
  2. namespace Bidcoz\Bundle\FrontendBundle\Controller;
  3. use Bidcoz\Bundle\CoreBundle\Constants;
  4. use Bidcoz\Bundle\CoreBundle\Controller\CoreController;
  5. use Bidcoz\Bundle\CoreBundle\Entity\Campaign;
  6. use Bidcoz\Bundle\CoreBundle\Entity\Organization;
  7. use Bidcoz\Bundle\CoreBundle\Entity\PaymentGateway\Account\Account;
  8. use Bidcoz\Bundle\CoreBundle\Entity\Proxy\StripeCreditCardProxy;
  9. use Bidcoz\Bundle\CoreBundle\Entity\User;
  10. use Bidcoz\Bundle\CoreBundle\Form\Type\StripeCreditCardType;
  11. use Bidcoz\Bundle\UserBundle\Event\BidcozFilterUserResponseEvent;
  12. use Bidcoz\Bundle\UserBundle\Event\BidcozGetResponseUserEvent;
  13. use FOS\UserBundle\Event\FormEvent;
  14. use FOS\UserBundle\FOSUserEvents;
  15. use Karser\Recaptcha3Bundle\Form\Recaptcha3Type;
  16. use Karser\Recaptcha3Bundle\Validator\Constraints\Recaptcha3;
  17. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  18. use Symfony\Component\Form\Form;
  19. use Symfony\Component\HttpFoundation\RedirectResponse;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. /**
  23.  * @Route("/{orgSlug}", requirements={"orgSlug" = Constants::RESERVED_SLUG_REGEX})
  24.  */
  25. class CreateOrganizationController extends CoreController
  26. {
  27.     # TODO: New authenticator behaves differently for @Security and @IsGranted
  28.     # Issue: "Symfony\Component\Security\Core\Exception\AccessDeniedException: No user token or you forgot to put your controller behind a firewall while using a @Security tag
  29.     # PR fixes this (still "Open"): https://github.com/sensiolabs/SensioFrameworkExtraBundle/pull/763
  30.     # Docs: https://symfony.com/doc/5.2/security/experimental_authenticators.html#enabling-the-system
  31.     # Notes: When PR gets merged, switch back to using the @Security tag: @Security("is_granted('VIEW', organization)")
  32.     /**
  33.      * @Route("/register", name="organization_register_user")
  34.      * @IsGranted("VIEW", subject="organization")
  35.      */
  36.     public function organizationAction(Request $requestOrganization $organization)
  37.     {
  38.         $userManager $this->getUserManager();
  39.         $dispatcher  $this->getEventDispatcher();
  40.         $user $userManager->createUser();
  41.         $user->setEnabled(true);
  42.         $event = new BidcozGetResponseUserEvent($user$request);
  43.         $event->setOrganization($organization);
  44.         $dispatcher->dispatch($eventFOSUserEvents::REGISTRATION_INITIALIZE);
  45.         if (null !== $event->getResponse()) {
  46.             return $event->getResponse();
  47.         }
  48.         $email '';
  49.         $form $this->getRegistrationFormFactory()->createForm();
  50.         $form->add('captcha'Recaptcha3Type::class, [
  51.             'constraints' => new Recaptcha3(),
  52.             'action_name' => 'organization_register_user',
  53.         ]);
  54.         $form->setData($user);
  55.         $hasEmailAlreadyInUserError false;
  56.         if ('POST' === $request->getMethod()) {
  57.             $form->handleRequest($request);
  58.             if ($form->isSubmitted() && $form->isValid()) {
  59.                 $event = new FormEvent($form$request);
  60.                 $dispatcher->dispatch($eventFOSUserEvents::REGISTRATION_SUCCESS);
  61.                 $user->setLoggedIn();
  62.                 $userManager->updateUser($user);
  63.                 $contact $this->getContactManager()->findOrCreateContact($organization$usertrue);
  64.                 if (null === $response $event->getResponse()) {
  65.                     $url $this->getRouter()->generate('organization_home', [
  66.                         'orgSlug' => $organization->getSlug(),
  67.                     ]);
  68.                     $response = new RedirectResponse($url);
  69.                 }
  70.                 $event = new BidcozFilterUserResponseEvent($user$request$response);
  71.                 $event->setOrganization($organization);
  72.                 $dispatcher->dispatch($eventFOSUserEvents::REGISTRATION_COMPLETED);
  73.                 return $response;
  74.             } else {
  75.                 // This is a hack
  76.                 $emailForm $form->get('email');
  77.                 foreach ($emailForm->getErrors() as $error) {
  78.                     if ('fos_user.email.already_used' === $error->getMessageTemplate()) {
  79.                         $hasEmailAlreadyInUserError true;
  80.                     }
  81.                 }
  82.                 $email $emailForm->getData();
  83.             }
  84.         }
  85.         return $this->render('@BidcozFrontend/Registration/organization.html.twig', [
  86.             'organization'               => $organization,
  87.             'form'                       => $form->createView(),
  88.             'hasEmailAlreadyInUserError' => $hasEmailAlreadyInUserError,
  89.             'last_username'              => $email,
  90.         ]);
  91.     }
  92.     # TODO: New authenticator behaves differently for @Security and @IsGranted
  93.     # Issue: "Symfony\Component\Security\Core\Exception\AccessDeniedException: No user token or you forgot to put your controller behind a firewall while using a @Security tag
  94.     # PR fixes this (still "Open"): https://github.com/sensiolabs/SensioFrameworkExtraBundle/pull/763
  95.     # Docs: https://symfony.com/doc/5.2/security/experimental_authenticators.html#enabling-the-system
  96.     # Notes: When PR gets merged, switch back to using the @Security tag: @Security("is_granted('VIEW', organization) and is_granted('FRONT_END', campaign)")
  97.     /**
  98.      * @Route("/{campaignSlug}/register", name="campaign_register_user")
  99.      * @IsGranted("VIEW", subject="organization")
  100.      * @IsGranted("FRONT_END", subject="campaign")
  101.      */
  102.     public function campaignAction(Request $requestOrganization $organizationCampaign $campaign)
  103.     {
  104.         $userManager $this->getUserManager();
  105.         $dispatcher  $this->getEventDispatcher();
  106.         $user $userManager->createUser();
  107.         $user->setEnabled(true);
  108.         $event = new BidcozGetResponseUserEvent($user$request);
  109.         $event->setOrganization($organization);
  110.         $event->setCampaign($campaign);
  111.         $dispatcher->dispatch($eventFOSUserEvents::REGISTRATION_INITIALIZE);
  112.         if (null !== $event->getResponse()) {
  113.             return $event->getResponse();
  114.         }
  115.         $email   '';
  116.         $ccProxy = new StripeCreditCardProxy();
  117.         $ccForm  $this->getCCForm($ccProxy);
  118.         $form    $this->getRegistrationFormFactory()->createForm();
  119.         $form->setData($user);
  120.         $hasEmailAlreadyInUserError false;
  121.         $withCc $organization->isStripeAllowed()
  122.             && $organization->hasPaymentGatewayAccountType(Account::STRIPE)
  123.             && $campaign->getCollectCcInfo();
  124.         if ('POST' === $request->getMethod()) {
  125.             $form->handleRequest($request);
  126.             $ccForm->handleRequest($request);
  127.             if (($form->isSubmitted() && $form->isValid()) && (!$withCc || $customer $this->ccCheck($campaign$user$ccForm))) {
  128.                 $event = new FormEvent($form$request);
  129.                 $dispatcher->dispatch($eventFOSUserEvents::REGISTRATION_SUCCESS);
  130.                 $user->setLoggedIn();
  131.                 $userManager->updateUser($user);
  132.                 $contact        $this->getContactManager()->findOrCreateContact($organization$usertrue);
  133.                 $campaignDetail $this->getContactManager()->findOrCreateCampaignDetail($campaign$contactfalse);
  134.                 if ($withCc && $customer) {
  135.                     $cc $this->getCcManager()->createCC($campaign$user$customer->id);
  136.                     $this->getCcManager()->setCcInfo($cc$customer$ccProxy);
  137.                     $this->getEntityManager()->flush();
  138.                 }
  139.                 $url $this->getRouter()->generate('campaign_home', [
  140.                     'orgSlug'      => $organization->getSlug(),
  141.                     'campaignSlug' => $campaign->getSlug(),
  142.                 ]);
  143.                 if (null === $response $event->getResponse()) {
  144.                     $response = new RedirectResponse($url);
  145.                 }
  146.                 if ($request->isXmlHttpRequest()) {
  147.                     $response $this->returnJsonSuccess(['redirect_url' => $url]);
  148.                 }
  149.                 $event = new BidcozFilterUserResponseEvent($user$request$response);
  150.                 $event->setOrganization($organization);
  151.                 $event->setCampaign($campaign);
  152.                 $dispatcher->dispatch($eventFOSUserEvents::REGISTRATION_COMPLETED);
  153.                 return $response;
  154.             } else {
  155.                 $emailForm $form->get('email')->get('first'); // for first email
  156.                 $errors    $emailForm->getErrors();
  157.                 foreach ($emailForm->getErrors() as $error) {
  158.                     if ('fos_user.email.already_used' === $error->getMessageTemplate()) {
  159.                         $hasEmailAlreadyInUserError true;
  160.                     }
  161.                 }
  162.                 $email $emailForm->getData();
  163.             }
  164.         }
  165.         return $this->render('@BidcozFrontend/Registration/campaign.html.twig', [
  166.             'organization'               => $organization,
  167.             'campaign'                   => $campaign,
  168.             'form'                       => $form->createView(),
  169.             'ccForm'                     => $ccForm->createView(),
  170.             'withCc'                     => $withCc,
  171.             'hasEmailAlreadyInUserError' => $hasEmailAlreadyInUserError,
  172.             'last_username'              => $email,
  173.         ]);
  174.     }
  175.     protected function getCCForm(StripeCreditCardProxy $proxy)
  176.     {
  177.         return $this->createForm(StripeCreditCardType::class, $proxy);
  178.     }
  179.     protected function ccCheck(Campaign $campaignUser $userForm &$ccForm)
  180.     {
  181.         if (!($ccForm->isSubmitted() && $ccForm->isValid())) {
  182.             $this->addFlash('danger''Credit Card Required to Participate');
  183.             return false;
  184.         }
  185.         $token $ccForm->get('token')->getData();
  186.         try {
  187.             $customer $this->getStripeManager()->createOrgCustomer($campaign->getOrganization(), $user$token);
  188.         } catch (\Stripe\Error\Base $e) {
  189.             $this->addFlash('danger'$e->getMessage());
  190.             $ccProxy = new StripeCreditCardProxy();
  191.             $ccForm  $this->getCCForm($ccProxy);
  192.             return false;
  193.         }
  194.         return $customer;
  195.     }
  196. }