vendor/sulu/community-bundle/Controller/PasswordController.php line 27

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Bundle\CommunityBundle\Controller;
  11. use Sulu\Bundle\CommunityBundle\DependencyInjection\Configuration;
  12. use Sulu\Bundle\CommunityBundle\Manager\UserManagerInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. /**
  16.  * Handles password forget and reset pages.
  17.  */
  18. class PasswordController extends AbstractController
  19. {
  20.     /**
  21.      * Handles the forget form.
  22.      */
  23.     public function forgetAction(Request $request): Response
  24.     {
  25.         $communityManager $this->getCommunityManager($this->getWebspaceKey());
  26.         // Create Form
  27.         $form $this->createForm(
  28.             $communityManager->getConfigTypeProperty(Configuration::TYPE_PASSWORD_FORGETConfiguration::FORM_TYPE),
  29.             [],
  30.             $communityManager->getConfigTypeProperty(
  31.                 Configuration::TYPE_PASSWORD_FORGET,
  32.                 Configuration::FORM_TYPE_OPTIONS
  33.             )
  34.         );
  35.         $form->handleRequest($request);
  36.         $success false;
  37.         if ($form->isSubmitted() && $form->isValid()) {
  38.             // Handle form success
  39.             $emailUsername $form->get('email_username')->getData();
  40.             // Handle Password forget
  41.             $communityManager->passwordForget($emailUsername);
  42.             // Save User
  43.             $this->saveEntities();
  44.             // Redirect
  45.             $redirectTo $communityManager->getConfigTypeProperty(
  46.                 Configuration::TYPE_PASSWORD_FORGET,
  47.                 Configuration::REDIRECT_TO
  48.             );
  49.             if ($redirectTo) {
  50.                 return $this->redirect($redirectTo);
  51.             }
  52.             $success true;
  53.         }
  54.         return $this->renderTemplate(
  55.             Configuration::TYPE_PASSWORD_FORGET,
  56.             [
  57.                 'form' => $form,
  58.                 'success' => $success,
  59.             ]
  60.         );
  61.     }
  62.     /**
  63.      * Handles the reset password form.
  64.      */
  65.     public function resetAction(Request $requeststring $token): Response
  66.     {
  67.         $communityManager $this->getCommunityManager($this->getWebspaceKey());
  68.         // Check valid token
  69.         $user $this->getUserManager()->findByPasswordResetToken($token);
  70.         if (!$user) {
  71.             return $this->renderTemplate(
  72.                 Configuration::TYPE_PASSWORD_RESET,
  73.                 [
  74.                     'form' => null,
  75.                     'success' => false,
  76.                 ]
  77.             );
  78.         }
  79.         // Create Form
  80.         $form $this->createForm(
  81.             $communityManager->getConfigTypeProperty(Configuration::TYPE_PASSWORD_RESETConfiguration::FORM_TYPE),
  82.             $user,
  83.             $communityManager->getConfigTypeProperty(
  84.                 Configuration::TYPE_PASSWORD_RESET,
  85.                 Configuration::FORM_TYPE_OPTIONS
  86.             )
  87.         );
  88.         $form->handleRequest($request);
  89.         $success false;
  90.         // Handle Form Success
  91.         if ($form->isSubmitted() && $form->isValid()) {
  92.             // Set Password and Salt
  93.             $user $this->setUserPasswordAndSalt($form->getData(), $form);
  94.             // Save User with new Password
  95.             $user $communityManager->passwordReset($user);
  96.             // Save User
  97.             $this->saveEntities();
  98.             // Login
  99.             if ($this->checkAutoLogin(Configuration::TYPE_PASSWORD_RESET)) {
  100.                 $communityManager->login($user$request);
  101.             }
  102.             // Redirect
  103.             $redirectTo $communityManager->getConfigTypeProperty(
  104.                 Configuration::TYPE_PASSWORD_RESET,
  105.                 Configuration::REDIRECT_TO
  106.             );
  107.             if ($redirectTo) {
  108.                 return $this->redirect($redirectTo);
  109.             }
  110.             $success true;
  111.         }
  112.         return $this->renderTemplate(
  113.             Configuration::TYPE_PASSWORD_RESET,
  114.             [
  115.                 'form' => $form,
  116.                 'success' => $success,
  117.             ]
  118.         );
  119.     }
  120.     protected function getUserManager(): UserManagerInterface
  121.     {
  122.         return $this->container->get('sulu_community.user_manager');
  123.     }
  124.     /**
  125.      * @return array<string|int, string>
  126.      */
  127.     public static function getSubscribedServices(): array
  128.     {
  129.         $subscribedServices parent::getSubscribedServices();
  130.         $subscribedServices['sulu_community.user_manager'] = UserManagerInterface::class;
  131.         return $subscribedServices;
  132.     }
  133. }