vendor/pimcore/portal-engine/src/Controller/AuthController.php line 35

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under following license:
  6.  * - Pimcore Commercial License (PCL)
  7.  *
  8.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  9.  *  @license    http://www.pimcore.org/license     PCL
  10.  */
  11. namespace Pimcore\Bundle\PortalEngineBundle\Controller;
  12. use Pimcore\Bundle\PortalEngineBundle\Exception\OutputErrorException;
  13. use Pimcore\Bundle\PortalEngineBundle\Form\LoginForm;
  14. use Pimcore\Bundle\PortalEngineBundle\Form\RecoverPasswordForm;
  15. use Pimcore\Bundle\PortalEngineBundle\Model\View\Notification;
  16. use Pimcore\Bundle\PortalEngineBundle\Service\Content\HeadTitleService;
  17. use Pimcore\Bundle\PortalEngineBundle\Service\Frontend\FrontendNotificationService;
  18. use Pimcore\Bundle\PortalEngineBundle\Service\PortalConfig\PortalConfigService;
  19. use Pimcore\Bundle\PortalEngineBundle\Service\Security\Authentication\User\PasswordChangeableService;
  20. use Pimcore\Bundle\PortalEngineBundle\Service\Security\Authentication\User\RecoverPasswordService;
  21. use Pimcore\Tool;
  22. use Symfony\Component\Form\FormFactoryInterface;
  23. use Symfony\Component\Form\FormInterface;
  24. use Symfony\Component\HttpFoundation\Request;
  25. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  26. use Symfony\Component\Routing\Annotation\Route;
  27. use Symfony\Contracts\Translation\TranslatorInterface;
  28. /**
  29.  * @Route("/auth", condition="request.attributes.get('isPortalEngineSite')")
  30.  */
  31. class AuthController extends AbstractSiteController
  32. {
  33.     /**
  34.      * @Route("/login",
  35.      *     name="pimcore_portalengine_auth_login"
  36.      * )
  37.      */
  38.     public function loginAction(Request $requestFormFactoryInterface $formFactoryHeadTitleService $headTitleServiceTranslatorInterface $translatorPortalConfigService $portalConfigServicePasswordChangeableService $passwordChangeableService)
  39.     {
  40.         /** @var string $locale */
  41.         $locale $request->getPreferredLanguage(Tool::getValidLanguages());
  42.         $translator->setLocale($locale);
  43.         $request->setLocale($locale);
  44.         $portalName $portalConfigService->getPortalName();
  45.         $headTitleService->setTitle($translator->trans('portal-engine.content.title.auth-login', ['%name%' => $portalName]));
  46.         $loginForm $formFactory->create(LoginForm::class);
  47.         return $this->renderTemplate('@PimcorePortalEngine/auth/login.html.twig', [
  48.             'form' => $loginForm->createView(),
  49.             'loginFailed' => (bool)$request->query->get('loginFailed'),
  50.             'portalName' => $portalName,
  51.             'showRecoverPassword' => $passwordChangeableService->isPasswordChangeable()
  52.         ]);
  53.     }
  54.     /**
  55.      * @Route("/logout",
  56.      *     name="pimcore_portalengine_auth_logout"
  57.      * )
  58.      */
  59.     public function logoutAction(Request $request)
  60.     {
  61.     }
  62.     /**
  63.      * @Route("/recover-password",
  64.      *     name="pimcore_portalengine_auth_recover_password"
  65.      * )
  66.      */
  67.     public function recoverPasswordAction(Request $requestTranslatorInterface $translatorFormFactoryInterface $formFactoryFrontendNotificationService $frontendNotificationServiceRecoverPasswordService $recoverPasswordServicePasswordChangeableService $passwordChangeableServiceHeadTitleService $headTitleService)
  68.     {
  69.         if (!$passwordChangeableService->isPasswordChangeable()) {
  70.             throw new NotFoundHttpException('password not changeable');
  71.         }
  72.         /** @var string $locale */
  73.         $locale $request->getPreferredLanguage(Tool::getValidLanguages());
  74.         $translator->setLocale($locale);
  75.         $request->setLocale($locale);
  76.         $headTitleService->setTitle($translator->trans('portal-engine.content.title.auth-recover-password'));
  77.         /** @var FormInterface $recoverPasswordForm */
  78.         $recoverPasswordForm $formFactory
  79.             ->create(RecoverPasswordForm::class)
  80.             ->handleRequest($request);
  81.         if ($request->isMethod(Request::METHOD_POST) && $recoverPasswordForm->isSubmitted() && $recoverPasswordForm->isValid()) {
  82.             try {
  83.                 /** @var string $userIdentifier */
  84.                 $userIdentifier = (string)$recoverPasswordForm->get('userIdentifier')->getData();
  85.                 if (empty($userIdentifier)) {
  86.                     throw new OutputErrorException($translator->trans('portal-engine.auth.user-not-found'));
  87.                 }
  88.                 $recoverPasswordService->recoverPassword($userIdentifier);
  89.                 $frontendNotificationService
  90.                     ->addNotification($translator->trans('portal-engine.auth.password-recover-email'), Notification::HTML_CLASS_SUCCESS);
  91.             } catch (\Exception $e) {
  92.                 if ($e instanceof OutputErrorException) {
  93.                     $frontendNotificationService->addNotification($e->getMessage(), Notification::HTML_CLASS_DANGER);
  94.                 }
  95.             }
  96.         }
  97.         return $this->renderTemplate('@PimcorePortalEngine/auth/recover_password.html.twig', [
  98.             'recoverPasswordForm' => $recoverPasswordForm->createView(),
  99.             'notification' => $frontendNotificationService->getNotification()
  100.         ]);
  101.     }
  102. }