src/Controller/UserController.php line 50

Open in your IDE?
  1. <?php
  2. /**
  3.  * User controller class
  4.  *
  5.  * PHP version 7.4
  6.  *
  7.  * @category   App
  8.  * @package    App\Controller
  9.  * @author     Momcilo Radotic <m.radotic@outlook.com>
  10.  * @copyright  2021 MoravaPro
  11.  * @license    MoravaPro
  12.  */
  13. namespace App\Controller;
  14. use App\Entity\Organization;
  15. use App\Entity\User;
  16. use App\Entity\UserStatus;
  17. use App\Helper\UserUtils;
  18. use App\Validation\Form\ChangePasswordForm;
  19. use App\Validation\Form\LoginForm;
  20. use App\Validation\Form\LostPasswordForm;
  21. use App\Validation\Form\UserConfirmationForm;
  22. use FOS\RestBundle\Controller\Annotations as Rest;
  23. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  24. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  25. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  26. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  27. use Symfony\Component\Form\Extension\Core\Type\TextType;
  28. use Symfony\Component\HttpFoundation\Request;
  29. use Symfony\Component\HttpFoundation\Response;
  30. /**
  31.  * User controller class
  32.  *
  33.  * @category   App
  34.  * @package    App\Controller
  35.  */
  36. class UserController extends BaseController
  37. {
  38.     /**
  39.      * Login action
  40.      *
  41.      * @Rest\Post("/login", name="user_login_post")
  42.      * @Rest\Get("/login", name="user_login_get")
  43.      *
  44.      * @return Response
  45.      */
  46.     public function loginAction() : Response
  47.     {
  48.         /** @var User $user */
  49.         $user $this->getUser();
  50.         if ($user) {
  51.             $page $user->getOrganization()->getStatus() == Organization::REGISTERED 'complete-registration' 'dashboard';
  52.             return $this->redirectToRoute(
  53.                 'index_application', ['page' => $page]
  54.             );
  55.         }
  56.         $loginForm = new LoginForm();
  57.         $formBuilder $this->createFormBuilder($loginForm);
  58.         $form $formBuilder->add('username'TextType::class)
  59.             ->add('password'PasswordType::class)
  60.             ->getForm();
  61.         return $this->render('view/login.html.twig', array('form' => $form->createView(), 'message' => '''language' => 'en'));
  62.     }
  63.     /**
  64.      * Action for rendering lost password form
  65.      *
  66.      * @Rest\Get("/lost-password", name="user_lost_password_get")
  67.      * @Rest\Post("/lost-password", name="user_lost_password_post")
  68.      *
  69.      * @param Request $request
  70.      * @param UserUtils $userUtils
  71.      *
  72.      * @return Response
  73.      */
  74.     public function lostPasswordAction(Request $requestUserUtils $userUtils) : Response
  75.     {
  76.         $lostPasswordForm = new LostPasswordForm();
  77.         $formBuilder $this->createFormBuilder($lostPasswordForm);
  78.         $form $formBuilder->add('email'EmailType::class)->getForm();
  79.         $form->handleRequest($request);
  80.         $errorMessage null;
  81.         if ($form->isSubmitted() && $form->isValid()) {
  82.             $user $this->getDoctrine()->getManager()->getRepository(User::class)->findOneBy(array('email' => strtolower($lostPasswordForm->getEmail())));
  83.             if (isset($user)) {
  84.                 $changePasswordKey $userUtils->generateUserHash($lostPasswordForm->getEmail() . time());
  85.                 $user->setPasswordKey($changePasswordKey);
  86.                 $ttl time() + $this->getParameter('change_password_ttl');
  87.                 $user->setPasswordKeyTtl((new \DateTime())->setTimestamp($ttl));
  88.                 $this->getDoctrine()->getManager()->flush();
  89.                 $result $this->mailer->sendSystemMail(
  90.                     $this->translator->trans('Password recovery'),
  91.                     $lostPasswordForm->getEmail(),
  92.                     $this->renderView(
  93.                         'view/email/lost-password.html.twig',
  94.                         array(
  95.                             'subject' => $this->translator->trans('Password recovery'),
  96.                             'content'=> $this->utils->getBaseUrl() . '/change-password/?active_key=' $changePasswordKey,
  97.                             'base_url' => $this->utils->getBaseUrl()
  98.                         )
  99.                     )
  100.                 );
  101.                 if ($result 0) {
  102.                     return $this->redirectToRoute('user_reset_password_email_sent');
  103.                 } else {
  104.                     $errorMessage $this->translator->trans('Failed to send email to email address');
  105.                 }
  106.             } else {
  107.                 $this->logger->info($this->translator->trans("No user found")." ".$lostPasswordForm->getEmail());
  108.                 $errorMessage $this->translator->trans('Invalid email');
  109.             }
  110.         }
  111.         return $this->render(
  112.             'view/lost-password.html.twig',
  113.             array(
  114.                 'form' => $form->createView(),
  115.                 'error_message' => $errorMessage
  116.             )
  117.         );
  118.     }
  119.     /**
  120.      * Action for rendering change password form
  121.      *
  122.      * @Rest\Get("/change-password", name="user_change_password_get")
  123.      * @Rest\Post("/change-password", name="user_change_password_post")
  124.      *
  125.      * @param Request $request
  126.      * @param UserUtils $userUtils
  127.      *
  128.      * @return Response
  129.      */
  130.     public function changePasswordAction(Request $requestUserUtils $userUtils) : Response
  131.     {
  132.         $changePasswordForm = new ChangePasswordForm();
  133.         $formBuilder $this->createFormBuilder($changePasswordForm);
  134.         $passwordKey $request->query->get('active_key');
  135.         $user $this->getDoctrine()->getManager()->getRepository(User::class)
  136.             ->findOneBy(['password_key' => $passwordKey]);
  137.         if (!$user || $user->getPasswordKeyTtl() < new \DateTime()) {
  138.             if ($user) {
  139.                 $user->setPasswordKey(null);
  140.                 $user->setPasswordKeyTtl(null);
  141.                 $this->getDoctrine()->getManager()->flush();
  142.             }
  143.             $errorMessage $user 'Change password key has expired.' 'Invalid request';
  144.             return $this->render(
  145.                 'view/error-400.html.twig',
  146.                 ['error_message' => $errorMessage]
  147.             );
  148.         }
  149.         $form $formBuilder->add('active_key'HiddenType::class, array('attr' => array('value' => $passwordKey)))
  150.             ->add('password'RepeatedType::class,
  151.                 array(
  152.                     'type' => PasswordType::class,
  153.                     'invalid_message' => 'The password fields must match',
  154.                     'first_options'  => array('label' => 'Password'),
  155.                     'second_options' => array('label' => 'Repeat Password'),
  156.                 )
  157.             )->getForm();
  158.         $form->handleRequest($request);
  159.         $errorMessage null;
  160.         if ($form->isSubmitted() && $form->isValid()) {
  161.             $password $userUtils->generatePassword($user$changePasswordForm->getPassword());
  162.             $user->setPassword($password);
  163.             $user->setModified(new \DateTime());
  164.             $this->getDoctrine()->getManager()->flush();
  165.             $content "Your password is changed, click on this link to recover ...";
  166.             $this->mailer->sendSystemMail(
  167.                 $this->translator->trans('Change password'),
  168.                 $user->getEmail(),
  169.                 $this->renderView(
  170.                     'view/email/changed-password.html.twig',
  171.                     array(
  172.                         'subject' => $this->translator->trans('Password changed'),
  173.                         'content'=>$content,
  174.                         'base_url' => $this->utils->getBaseUrl()
  175.                     )
  176.                 ),
  177.             );
  178.             return $this->redirectToRoute('user_password_changed');
  179.         }
  180.         return $this->render(
  181.             'view/change-password.html.twig',
  182.             array('form' => $form->createView(),
  183.                 'error_message' => $errorMessage,
  184.             )
  185.         );
  186.     }
  187.     /**
  188.      * User confirmation
  189.      *
  190.      * @Rest\Get("/user/confirm", name="user_confirm" )
  191.      * @Rest\Post("/user/confirm", name="user_confirm_post" )
  192.      *
  193.      * @param Request $request
  194.      * @param UserUtils $userUtils
  195.      *
  196.      * @return Response
  197.      */
  198.     public function inviteConfirmAction(Request $requestUserUtils $userUtils) : Response
  199.     {
  200.         $activeKey $request->query->get('active_key');
  201.         $user $this->getDoctrine()->getManager()->getRepository(User::class)
  202.             ->findOneBy(array('activation_key' => $activeKey'status' => UserStatus::REGISTERED));
  203.         if (isset($user)) {
  204.             $userConfirmationForm = new UserConfirmationForm();
  205.             $userConfirmationForm->setEmail($user->getEmail());
  206.             $userConfirmationForm->setFirstName($user->getFirstName());
  207.             $userConfirmationForm->setLastName($user->getLastName());
  208.             $formGenerator = function() use ($userConfirmationForm$user) {
  209.                 $formBuilder $this->createFormBuilder($userConfirmationForm);
  210.                 return $formBuilder->add(
  211.                     'password',
  212.                     RepeatedType::class,
  213.                     array(
  214.                         'type' => PasswordType::class,
  215.                         'invalid_message' => $this->translator->trans('The password fields must match'),
  216.                         'first_options'  => array('label' => $this->translator->trans('Password')),
  217.                         'second_options' => array('label' => $this->translator->trans('Repeat Password')),
  218.                     )
  219.                 )->add('email'EmailType::class, ['attr'=> [ 'readonly' => true'disabled' => true]])
  220.                     ->add('first_name'TextType::class)
  221.                     ->add('last_name'TextType::class)
  222.                     ->getForm();
  223.             };
  224.             $errorMessage null;
  225.             $form $formGenerator();
  226.             $form->handleRequest($request);
  227.             if ($form->isSubmitted() && $form->isValid()) {
  228.                 $user->setPassword($userUtils->generatePassword($user$userConfirmationForm->getPassword()));
  229.                 $user->setModified(new \DateTime());
  230.                 $user->setFirstName($userConfirmationForm->getFirstName());
  231.                 $user->setLastName($userConfirmationForm->getLastName());
  232.                 $user->setStatus($this->getDoctrine()->getManager()->getRepository(UserStatus::class)->find(UserStatus::ACTIVE));
  233.                 $this->getDoctrine()->getManager()->flush();
  234.                 return $this->redirectToRoute('app_logout');
  235.             } else if ($form->isSubmitted()) {
  236.                 $errorMessage $this->translator->trans('Error during user confirmation');
  237.                 $userConfirmationForm->setEmail($user->getEmail());
  238.                 $form $formGenerator();
  239.             }
  240.             return $this->render(
  241.                 'view/user-confirmation.html.twig',
  242.                 array(
  243.                     'form' => $form->createView(),
  244.                     'error_message' => $errorMessage
  245.                 )
  246.             );
  247.         } else {
  248.             return $this->render(
  249.                 'view/error-400.html.twig',
  250.                 ['error_message' => 'Invalid request']
  251.             );
  252.         }
  253.     }
  254.     /**
  255.      * Reset email sent
  256.      *
  257.      * @Rest\Get("/reset-password-email-sent", name="user_reset_password_email_sent")
  258.      */
  259.     public function resetEmailSentAction()
  260.     {
  261.         return $this->render('view/email-sent.html.twig');
  262.     }
  263.     /**
  264.      * Password successfully changed
  265.      *
  266.      * @Rest\Get("/password-changed", name="user_password_changed")
  267.      */
  268.     public function passwordChangedAction()
  269.     {
  270.         return $this->render('view/password-changed.html.twig');
  271.     }
  272. }