src/Hitso/Bundle/CommonBundle/Controller/Front/UserController.php line 143

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Hitso\Bundle\CommonBundle\Controller\Front;
  4. use Hitso\Bundle\CommonBundle\Controller\Controller;
  5. use Hitso\Bundle\CommonBundle\Entity\User;
  6. use Hitso\Bundle\CommonBundle\Event\UserChangePasswordEvent;
  7. use Hitso\Bundle\CommonBundle\Event\UserResetPasswordEvent;
  8. use Hitso\Bundle\CommonBundle\Form\Front\ChangePasswordFormType;
  9. use Hitso\Bundle\CommonBundle\Form\Front\RegistrationFormType;
  10. use Hitso\Bundle\CommonBundle\Form\Front\ResettingFormType;
  11. use Hitso\Bundle\CommonBundle\Manager\UserManager;
  12. use Hitso\Bundle\CommonBundle\Security\FormAuthenticator;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  17. use Symfony\Component\Security\Core\Security;
  18. use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
  19. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  20. use WhiteOctober\BreadcrumbsBundle\Model\Breadcrumbs;
  21. class UserController extends Controller
  22. {
  23.     /**
  24.      * @var UserManager
  25.      */
  26.     protected $manager;
  27.     /**
  28.      * @var GuardAuthenticatorHandler
  29.      */
  30.     protected $guard;
  31.     /**
  32.      * @var AuthenticationUtils
  33.      */
  34.     protected $authenticationUtils;
  35.     /**
  36.      * @var EventDispatcherInterface
  37.      */
  38.     protected $eventDispatcher;
  39.     /**
  40.      * @var FormAuthenticator
  41.      */
  42.     protected $formAuthenticator;
  43.     public function __construct(
  44.         UserManager $manager,
  45.         AuthenticationUtils $authenticationUtils,
  46.         GuardAuthenticatorHandler $guard,
  47.         EventDispatcherInterface $eventDispatcher,
  48.         FormAuthenticator $formAuthenticator
  49.     ) {
  50.         $this->manager             $manager;
  51.         $this->guard               $guard;
  52.         $this->authenticationUtils $authenticationUtils;
  53.         $this->eventDispatcher     $eventDispatcher;
  54.         $this->formAuthenticator   $formAuthenticator;
  55.     }
  56.     public function registerAction(Request $requestBreadcrumbs $breadcrumbs)
  57.     {
  58.         // Breadcrumbs
  59.         $breadcrumbs->addRouteItem('Sign up''register');
  60.         $user $this->getUser();
  61.         if ($user instanceof User) {
  62.             return $this->redirectToRoute('profile');
  63.         }
  64.         $this->updateSeoPageBySlug('register');
  65.         /** @var User $user */
  66.         $user $this->manager->initResource();
  67.         $user->setEnabled(true);
  68.         $user->setDefaultLocale($request->getLocale());
  69.         $form $this->manager->initForm($user, ['validation_groups' => ['PasswordSet''Default']], RegistrationFormType::class);
  70.         $form->handleRequest($request);
  71.         if ($form->isSubmitted() && $form->isValid()) {
  72.             $user->setUsername($user->getEmail());
  73.             $this->manager->createResource($user);
  74.             $this->guard->authenticateUserAndHandleSuccess($user$request$this->formAuthenticator'frontend');
  75.             return $this->redirectToRoute('profile');
  76.         }
  77.         return $this->displayTemplate('register', [
  78.             'form' => $form->createView(),
  79.         ]);
  80.     }
  81.     public function profileAction(Request $request): Response
  82.     {
  83.         $user $this->getUser();
  84.         if (!$user instanceof User) {
  85.             return $this->redirectToAction('login');
  86.         }
  87.         $this->updateSeoPageBySlug('profile');
  88.         return $this->displayTemplate('profile');
  89.     }
  90.     public function loginAction(Request $requestBreadcrumbs $breadcrumbs)
  91.     {
  92.         // Breadcrumbs
  93.         $breadcrumbs->addRouteItem('Sign in''login');
  94.         $user $this->getUser();
  95.         if ($user instanceof User) {
  96.             return $this->redirectToRoute('profile');
  97.         }
  98.         $this->updateSeoPageBySlug('login');
  99.         $error $request->getSession()->get(Security::AUTHENTICATION_ERROR);
  100.         if (null !== $error) {
  101.             $request->getSession()->remove(Security::AUTHENTICATION_ERROR);
  102.             $request->getSession()->set(Security::AUTHENTICATION_ERROR, new AuthenticationException($error->getMessage()));
  103.         }
  104.         return $this->displayTemplate('login', [
  105.             'error'        => $this->authenticationUtils->getLastAuthenticationError(),
  106.             'lastUsername' => $this->authenticationUtils->getLastUsername(),
  107.         ]);
  108.     }
  109.     public function logoutAction()
  110.     {
  111.     }
  112.     public function passwordResetAction(Request $request)
  113.     {
  114.         $user $this->getUser();
  115.         if ($user instanceof User) {
  116.             return $this->redirectToRoute('profile');
  117.         }
  118.         $this->updateSeoPageBySlug('password_reset');
  119.         $form $this->createForm(ResettingFormType::class, null, ['validation_groups' => ['FrontPasswordReset']]);
  120.         $form->handleRequest($request);
  121.         if ($form->isSubmitted() && $form->isValid()) {
  122.             $email $form->getData()['email'];
  123.             $user  $this->manager->getRepository()->findOneBy(['email' => $email]);
  124.             
  125.             if ($user instanceof User) {
  126.                 $event = new UserResetPasswordEvent($user);
  127.                 $this->eventDispatcher->dispatch(UserResetPasswordEvent::EVENT_NAME$event);
  128.                 $this->manager->updateResource($user);
  129.                 $success true;
  130.             } else {
  131.                 $success false;
  132.             }
  133.             return $this->displayTemplate('password_reset', [
  134.                 'success' => $success,
  135.             ]);
  136.         }
  137.         return $this->displayTemplate('password_reset', [
  138.             'form' => $form->createView(),
  139.         ]);
  140.     }
  141.     public function passwordChangeAction(Request $requeststring $token)
  142.     {
  143.         $user $this->getUser();
  144.         if ($user instanceof User) {
  145.             return $this->redirectToRoute('profile');
  146.         }
  147.         $user $this->manager->getRepository()->findOneBy(['confirmationToken' => $token]);
  148.         if (!$user instanceof User) {
  149.             return $this->redirectToRoute('password_reset');
  150.         }
  151.         $form $this->createForm(ChangePasswordFormType::class, $user, ['validation_groups' => ['PasswordSet']]);
  152.         $form->handleRequest($request);
  153.         if ($form->isSubmitted() && $form->isValid()) {
  154.             $event = new UserChangePasswordEvent($user);
  155.             $this->eventDispatcher->dispatch(UserChangePasswordEvent::EVENT_NAME$event);
  156.             $this->manager->updateResource($user);
  157.             return $this->displayTemplate('password_change', [
  158.                 'success' => true,
  159.             ]);
  160.         }
  161.         return $this->displayTemplate('password_change', [
  162.             'form' => $form->createView(),
  163.         ]);
  164.     }
  165.     protected function updateSeoPageBySlug(string $slug): void
  166.     {
  167.         $page $this->getPageBySlug($slug);
  168.         if (!empty($page)) {
  169.             $this->updateSeoPage($page);
  170.         }
  171.     }
  172. }