src/Controller/SecurityController.php line 316

Open in your IDE?
  1. <?php
  2.     namespace App\Controller;
  3.     use App\Form\LoginConstituentInfoForm;
  4.     use App\Form\LoginForm;
  5.     use App\Form\LoginTokenForm;
  6.     use App\Form\ResetpassForm;
  7.     use App\Service\TessituraBundle\CartService;
  8.     use App\Service\TessituraBundle\ConstituentService;
  9.     use App\Service\TessituraBundle\TessituraUserProvider;
  10.     use App\Service\TessituraSDK\Entity\User;
  11.     use App\Service\TessituraSDK\Resource\Web\Session;
  12.     use App\Service\TessituraSDK\TessituraClient;
  13.     use App\Service\TessituraSDK\TessituraClientException;
  14.     use Psr\Container\ContainerInterface;
  15.     use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  16.     use Symfony\Component\Routing\Annotation\Route;
  17.     use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  18.     use Symfony\Component\Form\FormError;
  19.     use Symfony\Component\HttpFoundation\RedirectResponse;
  20.     use Symfony\Component\HttpFoundation\Request;
  21.     use Symfony\Component\HttpKernel\KernelInterface;
  22. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  23. use Symfony\Component\Security\Core\Security;
  24. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  25.     use Symfony\Contracts\Translation\TranslatorInterface;
  26.     class SecurityController extends AbstractController
  27.     {
  28.         /**
  29.          * @var TessituraClient
  30.          */
  31.         protected $client;
  32.         /**
  33.          * @var User
  34.          */
  35.         protected $user;
  36.         /**
  37.          * @var TranslatorInterface
  38.          */
  39.         private $translator;
  40.         /**
  41.          * @var ParameterBagInterface
  42.          */
  43.         private $params;
  44.         /**
  45.          * SecurityController constructor.
  46.          *
  47.          * @param TessituraClient $client
  48.          * @param TranslatorInterface $translator
  49.          * @param ParameterBagInterface $params
  50.          */
  51.         public function __construct(TessituraClient $clientTranslatorInterface $translatorParameterBagInterface $params)
  52.         {
  53.             $this->client     $client;
  54.             $this->translator $translator;
  55.             $this->params     $params;
  56.         }
  57.         /**
  58.          * @Route(
  59.          *     "/{_locale}/login/",
  60.          *     name="login",
  61.          *     defaults={
  62.          *          "_locale": "fi"
  63.          *     },
  64.          *     requirements={
  65.          *          "_locale": "fi|sv|en"
  66.          *     }
  67.          * )
  68.          * @param Request $request
  69.          *
  70.          * @return \Symfony\Component\HttpFoundation\Response
  71.          */
  72.         public function loginAction(Request $requestCartService $cartServiceAuthenticationUtils $authenticationUtils)
  73.         {
  74.             /**
  75.              * Get previous authentication data
  76.              */
  77.             /** @var AuthenticationUtils $authenticationUtils */
  78.             //$authenticationUtils = $this->get('security.authentication_utils');
  79.             $error               $authenticationUtils->getLastAuthenticationError();
  80.             $lastUsername        $authenticationUtils->getLastUsername();
  81.             /**
  82.              * Get LoginForm, the form is handled by TessituraGuardAuthenticator
  83.              */
  84.             $form $this->createForm(LoginForm::class, [
  85.                 '_email' => $lastUsername
  86.             ]);
  87.             if ($error) {
  88.                 $form->addError(new FormError($error->getMessage()));
  89.             }
  90.             $fromStage24 = !!preg_match('/stage24/'$request->getUri());
  91.             /**
  92.              * Render
  93.              */
  94.             return $this->render('page/login.html.twig', [
  95.                 'form' => $form->createView(),
  96.         'source' => $fromStage24 'stage24' null,
  97.             ]);
  98.         }
  99.         /**
  100.          * @Route(
  101.          *     "/{_locale}/login/constituent/",
  102.          *     name="login-constituent",
  103.          *     defaults={
  104.          *          "_locale": "fi"
  105.          *     },
  106.          *     requirements={
  107.          *          "_locale": "fi|sv|en"
  108.          *     }
  109.          * )
  110.          * @param Request $request
  111.          * @param KernelInterface $kernel
  112.          * @param AuthenticationUtils $authenticationUtils
  113.          * @return \Symfony\Component\HttpFoundation\Response
  114.          */
  115.         public function loginConstituentInfoAction(Request $requestKernelInterface $kernelAuthenticationUtils $authenticationUtils)
  116.         {
  117.             // if ($kernel->getEnvironment() !== 'dev') {
  118.             // NOT SUPPOSED TO BE USED ANYWHERE
  119.             throw $this->createNotFoundException();
  120.             // }
  121.             /**
  122.              * Get previous authentication data
  123.              */
  124.             //$authenticationUtils = $this->get('security.authentication_utils');
  125.             $error               $authenticationUtils->getLastAuthenticationError();
  126.             /**
  127.              * Get LoginForm, the form is handled by TessituraGuardAuthenticator
  128.              */
  129.             $form $this->createForm(LoginConstituentInfoForm::class, []);
  130.             if ($error) {
  131.                 $form->addError(new FormError($this->translator->trans("user_invalid")));
  132.             }
  133.             /**
  134.              * Render
  135.              */
  136.             return $this->render('page/login.html.twig', [
  137.                 'form' => $form->createView()
  138.             ]);
  139.         }
  140.         /**
  141.          * @Route(
  142.          *     "/{_locale}/login/external/",
  143.          *     name="login-external",
  144.          *     defaults={
  145.          *          "_locale": "fi"
  146.          *     },
  147.          *     requirements={
  148.          *          "_locale": "fi|sv|en"
  149.          *     }
  150.          * )
  151.          * @param Request $request
  152.          *
  153.          * @return \Symfony\Component\HttpFoundation\Response
  154.          * @throws TessituraClientException
  155.          */
  156.         public function loginExternalAction(Request $request)
  157.         {
  158.             $redirect $request->get('redirect');
  159.             $validRedirectDomains = [
  160.                 $request->getSchemeAndHttpHost(),
  161.                 $this->params->get('wp.domain'),
  162.                 $this->params->get('osajaot.domain'),
  163.             ];
  164.             $valid false;
  165.             foreach ($validRedirectDomains as $domain) {
  166.                 if (strpos($redirect$domain) === 0) {
  167.                     $valid true;
  168.                 }
  169.             }
  170.             if (strpos($redirect'login/external')) {
  171.                 $valid false;
  172.             }
  173.             if ($valid) {
  174.                 return new RedirectResponse($redirect);
  175.             } else {
  176.                 return $this->redirectToRoute('login');
  177.             }
  178.         }
  179.         /**
  180.          * @Route(
  181.          *     "/{_locale}/login/token/",
  182.          *     name="login-token",
  183.          *     defaults={
  184.          *          "_locale": "fi"
  185.          *     },
  186.          *     requirements={
  187.          *          "_locale": "fi|sv|en"
  188.          *     }
  189.          * )
  190.          * @param Request $request
  191.          * @param TessituraUserProvider $userProvider
  192.          * @param AuthenticationUtils $authenticationUtils
  193.          * @return \Symfony\Component\HttpFoundation\Response
  194.          */
  195.         public function tokenLoginAction(Request $requestTessituraUserProvider $userProviderAuthenticationUtils $authenticationUtils)
  196.         {
  197.             //return $this->redirectToRoute('login');
  198.             /**
  199.              * Get previous authentication data
  200.              */
  201.             //$authenticationUtils = $this->get('security.authentication_utils');
  202.             $error               $authenticationUtils->getLastAuthenticationError();
  203.             /**
  204.              * Get LoginForm, the form is handled by TessituraGuardAuthenticator
  205.              */
  206.             $form $this->createForm(LoginTokenForm::class, [
  207.             ]);
  208.             if ($error) {
  209.                 $form->addError(new FormError($this->translator->trans("user_invalid")));
  210.             }
  211.             /**
  212.              * Render
  213.              */
  214.             return $this->render('page/login-token.html.twig', [
  215.                 'form' => $form->createView()
  216.             ]);
  217.         }
  218.         /**
  219.          * @Route(
  220.          *     "/{_locale}/account/logout/",
  221.          *     name="account-logout",
  222.          *     defaults={
  223.          *          "_locale": "fi"
  224.          *     },
  225.          *     requirements={
  226.          *          "_locale": "fi|sv|en"
  227.          *     }
  228.          * )
  229.          * @param Request $request
  230.          * @param ConstituentService $constituentService
  231.          * @param CartService $cartService
  232.          *
  233.          * @param ContainerInterface $container
  234.          * @return \Symfony\Component\HttpFoundation\RedirectResponse
  235.          * @throws TessituraClientException
  236.          */
  237.         public function logoutAction(
  238.             Request $request,
  239.             ConstituentService $constituentService,
  240.             CartService $cartService,
  241.             ContainerInterface $container,
  242.             Security $security,
  243.             TokenStorageInterface $tokenStorage,
  244.         ) {
  245.             /** @var User $user */
  246.             $user $security->getUser();
  247.             $cache $container->get('cache.app');
  248.             $cache->invalidateTags([$constituentService->getCacheKey(), $cartService->getCacheKey()]);
  249.             /**
  250.              * Logout in Tessitura
  251.              */
  252.             $session $request->getSession();
  253.             if (!$session->has('tessitura_session')) {
  254.                 $this->client->request(Session::logout($session->get('tessitura_session')));
  255.             }
  256.             /**
  257.              * Invalidate token and session storage
  258.              */
  259.             $tokenStorage->setToken(null);
  260.             // $this->get('security.token_storage')->setToken(null);
  261.             $request->getSession()->invalidate();
  262.             /**
  263.              * Redirect to login
  264.              */
  265.             return $this->redirectToRoute('login');
  266.         }
  267.         /**
  268.          * @Route(
  269.          *     "/{_locale}/resetpass/",
  270.          *     name="resetpass",
  271.          *     defaults={
  272.          *          "_locale": "fi"
  273.          *     },
  274.          *     requirements={
  275.          *          "_locale": "fi|sv|en"
  276.          *     }
  277.          * )
  278.          * @param Request $request
  279.          * @param AuthenticationUtils $authenticationUtils
  280.          * @return \Symfony\Component\HttpFoundation\Response
  281.          * @throws TessituraClientException
  282.          */
  283.         public function resetPassAction(Request $requestAuthenticationUtils $authenticationUtils)
  284.         {
  285.             // TODO: Handler Resetpass
  286.             /** @var User $user */
  287.             $user $this->get('security.token_storage')->getToken()->getUser();
  288.             /**
  289.              * Get previous authentication data
  290.              */
  291.             //$authenticationUtils = $this->get('security.authentication_utils');
  292.             $error               $authenticationUtils->getLastAuthenticationError();
  293.             $lastUsername        $authenticationUtils->getLastUsername();
  294.             /**
  295.              * Get LoginForm, the form is handled by TessituraGuardAuthenticator
  296.              */
  297.             $form $this->createForm(ResetpassForm::class, [
  298.                 'email' => $lastUsername
  299.             ]);
  300.             $form->handleRequest($request);
  301.             if ($form->isSubmitted() && $form->isValid()) {
  302.                 try {
  303.                     $data     $form->getData();
  304.                     $response $this->client->request(Session::sendLoginCredentials($user->getSessionKey(), [
  305.                         'EmailAddress' => $data['email'],
  306.                         'LoginTypeId'  => $this->params->get('tessitura.logintype'),
  307.                     ]));
  308.                     $form->addError(new FormError($this->translator->trans("resetpass_sent")));
  309.                 } catch (TessituraClientException $exception) {
  310.                     switch ($exception->getCode()) {
  311.                         case TessituraClientException::TESSITURA_LOGIN_NOT_FOUND:
  312.                             $form->addError(new FormError($this->translator->trans("resetpass_invalid")));
  313.                             break;
  314.                         default:
  315.                             throw $exception;
  316.                     }
  317.                 }
  318.             }
  319.             return $this->render('page/resetpass.html.twig', [
  320.                 'form' => $form->createView()
  321.             ]);
  322.         }
  323.     }