src/Platform/SecurityBundle/Controller/System/InstancesController.php line 40

Open in your IDE?
  1. <?php
  2. namespace Platform\SecurityBundle\Controller\System;
  3. use Cms\CoreBundle\Model\Scenes\DashboardScenes\DocumentScene;
  4. use Cms\CoreBundle\Util\Controller;
  5. use Cms\CoreBundle\Util\DateTimeUtils;
  6. use Platform\SecurityBundle\Entity\Identity\Account;
  7. use Platform\SecurityBundle\Form\Type\SignInType;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\HttpFoundation\Cookie;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\RouterInterface;
  14. /**
  15.  * Class LoginController
  16.  * @package Platform\SecurityBundle\Controller\System
  17.  */
  18. final class InstancesController extends Controller
  19. {
  20.     const ROUTES__PROMPT 'campussuite.platform.security.system.instances.prompt';
  21.     const ROUTES__PRIVACY_POLICY 'campussuite.platform.security.system.instances.privacy_policy';
  22.     const ROUTES__TERMS_OF_SERVICE 'campussuite.platform.security.system.instances.terms_of_service';
  23.     const COOKIE 'campussuite_auto_login_email';
  24.     const CODE '1234';
  25.     /**
  26.      * @param Request $request
  27.      * @return Response|DocumentScene
  28.      * @throws \Exception
  29.      *
  30.      * @Route(
  31.      *     "",
  32.      *     name = InstancesController::ROUTES__PROMPT
  33.      * )
  34.      */
  35.     public function promptAction(Request $request)
  36.     {
  37.         $instances = [];
  38.         // url generation helpers
  39.         $router $this->getRouter();
  40.         $context $router->getContext();
  41.         $host $context->getHost();
  42.         // check if we are bypassing the email check screen
  43.         // check for email cookie first
  44.         // if not present, try to pull from query string
  45.         // TODO: do we need the cookie stuff?
  46.         $email $request->cookies->get(self::COOKIE);
  47.         if ( ! empty($email)) {
  48.             $code self::CODE;
  49.         } else {
  50.             $email $request->query->get('email');
  51.             $code $request->query->get('code');
  52.         }
  53.         // determine if we need to generate a form or not
  54.         if ( ! empty($email) && $code === self::CODE) {
  55.             $form null;
  56.         } else {
  57.             $form $this->createForm(
  58.                 SignInType::class,
  59.                 array(
  60.                     'email' => $email,
  61.                 ),
  62.                 []
  63.             );
  64.         }
  65.         // process to get the email
  66.         if ( ! empty($form) && $this->handleForm($form)) {
  67.             $email $form->get('email')->getData();
  68.         }
  69.         // if we have an email, show the accounts
  70.         // do only if no form of the form is ok
  71.         if ((empty($form) || ($form->isSubmitted() && $form->isValid())) && ! empty($email)) {
  72.             // encrypt the email for obfuscation
  73.             $encryptedEmail openssl_encrypt(
  74.                 $email,
  75.                 'aes128',
  76.                 $this->getParameter('kernel.secret')
  77.             );
  78.             // get the accounts that match us
  79.             /** @var Account[] $accounts */
  80.             $accounts $this->getEntityManager()->getRepository(Account::class)
  81.                 ->createQueryBuilder('accounts')
  82.                 ->andWhere('accounts.email = :email')
  83.                 ->setParameter('email'$email)
  84.                 ->leftJoin('accounts.tenant''tenants')
  85.                 ->addSelect('tenants')
  86.                 ->orderBy('tenants.name''ASC')
  87.                 ->getQuery()
  88.                 ->getResult();
  89.             // branch on the amount of results
  90.             switch (true) {
  91.                 // none are found
  92.                 case count($accounts) === 0:
  93.                     break;
  94.                 // more than one, need to select which one to use
  95.                 case count($accounts) > 1:
  96.                     foreach ($accounts as $account) {
  97.                         $context->setHost(sprintf(
  98.                             '%s.%s',
  99.                             $account->getTenant()->getSlug(),
  100.                             $this->getParameter('dashboard.hostname')
  101.                         ));
  102.                         $instances[] = array(
  103.                             'name' => $account->getTenant()->getName(),
  104.                             'url' => $this->generateUrl(
  105.                                 'platform.security.login.default.select',
  106.                                 array(
  107.                                     'autofill' => $encryptedEmail,
  108.                                 ),
  109.                                 RouterInterface::ABSOLUTE_URL
  110.                             ),
  111.                         );
  112.                     }
  113.                     break;
  114.                 // only one, go ahead and do redirect
  115.                 case count($accounts) === 1:
  116.                     $context->setHost(sprintf(
  117.                         '%s.%s',
  118.                         $accounts[0]->getTenant()->getSlug(),
  119.                         $this->getParameter('dashboard.hostname')
  120.                     ));
  121.                     $response = new RedirectResponse($this->generateUrl(
  122.                         'platform.security.login.default.select',
  123.                         array(
  124.                             'autofill' => $encryptedEmail,
  125.                         ),
  126.                         RouterInterface::ABSOLUTE_URL
  127.                     ));
  128.                     $response->headers->setCookie(new Cookie(
  129.                         self::COOKIE,
  130.                         $email,
  131.                         DateTimeUtils::afterNow('P1Y')
  132.                     ));
  133.                     return $response;
  134.             }
  135.         }
  136.         // be sure to return url context to original state
  137.         $context->setHost($host);
  138.         return $this->view(array(
  139.             'form' => ( ! empty($form)) ? $form->createView() : null,
  140.             'instances' => $instances,
  141.         ));
  142.     }
  143.     /**
  144.      * @return DocumentScene
  145.      *
  146.      * @Route(
  147.      *     "/privacy-policy",
  148.      *     name = InstancesController::ROUTES__PRIVACY_POLICY
  149.      * )
  150.      */
  151.     public function privacyAction()
  152.     {
  153.         return $this->view();
  154.     }
  155.     /**
  156.      * @return DocumentScene
  157.      *
  158.      * @Route(
  159.      *     "/terms-of-service",
  160.      *     name = InstancesController::ROUTES__TERMS_OF_SERVICE
  161.      * )
  162.      */
  163.     public function termsAction()
  164.     {
  165.         return $this->view();
  166.     }
  167. }