src/Bidcoz/Bundle/FrontendBundle/Controller/OrganizationRegistrationController.php line 24

Open in your IDE?
  1. <?php
  2. namespace Bidcoz\Bundle\FrontendBundle\Controller;
  3. use Bidcoz\Bundle\CoreBundle\Controller\CoreController;
  4. use Bidcoz\Bundle\CoreBundle\Entity\Organization;
  5. use Bidcoz\Bundle\CoreBundle\Event\CoreEvents;
  6. use Bidcoz\Bundle\CoreBundle\Event\OrganizationEvent;
  7. use Bidcoz\Bundle\CoreBundle\Services\MailchimpManager;
  8. use Bidcoz\Bundle\FrontendBundle\Form\Type\OrganizationType;
  9. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  10. use Symfony\Component\Form\FormError;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. /**
  14.  * @Route("/organization")
  15.  */
  16. class OrganizationRegistrationController extends CoreController
  17. {
  18.     /**
  19.      * @Route("/register/new", name="organization_register")
  20.      */
  21.     public function register(Request $request)
  22.     {
  23.         $user $this->getUser();
  24.         // Restored (CAU-253 #15). setupOrganization() type-hints User, so reaching a
  25.         // POST without one is a TypeError 500 rather than anything a person can act on.
  26.         if (!$user) {
  27.             $this->messageAccessDeniedException('You must login or create an account to register a new organization');
  28.         }
  29.         $organization $this->createOrganization();
  30.         $form         $this->createForm(OrganizationType::class, $organization);
  31.         if ('POST' === $request->getMethod()) {
  32.             $form->handleRequest($request);
  33.             if ($form->isSubmitted() && $form->isValid()) {
  34.                 // CAU-417 (Option 1): the real organizationType is now persisted from the
  35.                 // form. To keep behaviour identical to before, every self-serve org stays
  36.                 // flagged non_profit=true regardless of type (previously done implicitly by
  37.                 // the hidden 'non-profit' input). Mapping type -> nonprofit/fundraising
  38.                 // eligibility is deferred to the future org.canFundraise work.
  39.                 $organization->setNonProfit(true);
  40.                 $this->getEntityManager()->persist($organization);
  41.                 try {
  42.                     $organization->ensureSlugLowercase();
  43.                     $this->getOrganizationManager()->setupOrganization($organization$user);
  44.                     // Safety net (CAU-253 #15). The organization_admins row created here
  45.                     // is the ONLY thing that makes the new account reachable — the manage
  46.                     // dashboard is @IsGranted("MANAGE"), and OrganizationVoter grants that
  47.                     // solely off this link. If it ever goes missing, the signup still looks
  48.                     // successful and the failure surfaces one redirect later as a bare 403
  49.                     // on an account the person just created, with nothing in the logs
  50.                     // pointing back here. Assert it twice so it fails at its origin instead:
  51.                     // once in memory, BEFORE the flush, so a broken link aborts without
  52.                     // stranding a half-created account...
  53.                     $linked false;
  54.                     foreach ($organization->getAdmins() as $admin) {
  55.                         if ($admin->getUser() === $user) {
  56.                             $linked true;
  57.                             break;
  58.                         }
  59.                     }
  60.                     if (!$linked) {
  61.                         throw new \RuntimeException(sprintf(
  62.                             'Organization signup aborted: no admin link was created for user %s on "%s". Creating the account would have produced an org nobody can open.',
  63.                             $user->getId(),
  64.                             $organization->getSlug()
  65.                         ));
  66.                     }
  67.                     $event $this->createEvent($organization);
  68.                     $this->getEventDispatcher()->dispatch($eventCoreEvents::ORGANIZATION_CREATED);
  69.                     $this->getEntityManager()->flush();
  70.                     // ...and once against the database after it, since an ORPHANED
  71.                     // organization is only fixable by hand and we need to know which one.
  72.                     // Read through the manager's repository (not its isOrganizationAdmin
  73.                     // cache) so this is a real query, not a memoized answer.
  74.                     if (!$this->getOrganizationManager()->getOrganizationAdmin($organization$user)) {
  75.                         throw new \RuntimeException(sprintf(
  76.                             'Organization "%s" (id %s) was created but no organization_admins row exists for user %s. The account is orphaned and unreachable.',
  77.                             $organization->getSlug(),
  78.                             $organization->getId(),
  79.                             $user->getId()
  80.                         ));
  81.                     }
  82.                     $this->addFlash('success''Organization Created');
  83.                     $this->getMailChimpManager()->addUserToOnboardingJourney($user,[MailchimpManager::TAG_NEW_CUSTOMER],$organization);
  84.                     // If creating a new org, remove any traces to previous campaigns/orgs
  85.                     $this->getSession()->remove('viewed_campaign');
  86.                     $this->getSession()->remove('viewed_organization');
  87.                     return $this->redirectToRoute('organization_manage_dashboard', [
  88.                         'orgSlug'     => $organization->getSlug(),
  89.                         'welcome'     => 1,
  90.                         '_first_load' => 1,
  91.                     ]);
  92.                 } catch (UniqueConstraintViolationException $e) {
  93.                     $form->get('slug')->addError(new FormError('This account URL is already taken'));
  94.                 }
  95.             } else {
  96.                 $this->addFlash('danger''There was an error creating your account');
  97.             }
  98.         }
  99.         return $this->render('@BidcozFrontend/Marketing/register.html.twig', [
  100.             'form' => $form->createView(),
  101.         ]);
  102.     }
  103.     protected function createEvent(Organization $organization)
  104.     {
  105.         return new OrganizationEvent($organization);
  106.     }
  107.     protected function createOrganization()
  108.     {
  109.         return $this->getOrganizationManager()->createOrganization();
  110.     }
  111. }