src/Controller/SecurityController.php line 55

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Event\UserForgotPasswordEvent;
  5. use App\Event\UserResetPasswordEvent;
  6. use Doctrine\ODM\MongoDB\DocumentManager;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  16. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  17. use Symfony\Component\Security\Core\Security;
  18. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  19. class SecurityController extends AbstractController
  20. {
  21.     /**
  22.      * @Route("/login", name="login", methods={"GET", "POST"})
  23.      * @param AuthenticationUtils $authenticationUtils
  24.      * @param AuthorizationCheckerInterface $authorizationChecker
  25.      * @return RedirectResponse|Response
  26.      */
  27.     public function login(AuthenticationUtils $authenticationUtilsAuthorizationCheckerInterface $authorizationChecker)
  28.     {
  29.         if ($authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY')) {
  30.             return $this->redirectToRoute("index");
  31.         }
  32.         // get the login error if there is one
  33.         $error $authenticationUtils->getLastAuthenticationError();
  34.         // last username entered by the user
  35.         $lastUsername $authenticationUtils->getLastUsername();
  36.         $view $this->renderView('administration/security/login.html.twig', [
  37.             'last_username' => $lastUsername,
  38.             'error'         => $error,
  39.         ]);
  40.         return new Response($viewResponse::HTTP_OK, ['X-login-page' => '1']);
  41.     }
  42.     /**
  43.      * @Route("/recovery", name="recovery", methods={"GET", "POST"})
  44.      * @return RedirectResponse|Response
  45.      */
  46.     public function recovery()
  47.     {
  48.         $view $this->renderView('administration/security/recovery.html.twig');
  49.         return new Response($view);
  50.     }
  51.     /**
  52.      * @Route("/password_recovery",
  53.      *     condition="request.isXmlHttpRequest()",
  54.      *     name="password_recovery",
  55.      *     methods={"POST"},
  56.      *     options = { "expose" = true }
  57.      * )
  58.      *
  59.      * @param Request $request
  60.      * @param EntityManagerInterface $dm
  61.      * @param EventDispatcherInterface $eventDispatcher
  62.      * @return JsonResponse
  63.      * @throws \Exception
  64.      */
  65.     public function passwordRecovery(Request $requestEntityManagerInterface $dmEventDispatcherInterface $eventDispatcher)
  66.     {
  67.         $email $request->get('_username'false);
  68.         if (false === $email || !filter_var($emailFILTER_VALIDATE_EMAIL)) {
  69.             return new JsonResponse ([
  70.                 'status' => 'error',
  71.                 'messages' => 'INVALID_MAIL_ERROR'
  72.             ]);
  73.         }
  74.         if (!$user $dm->getRepository(User::class)->findOneBy(['email' => $email])) {
  75.             return new JsonResponse ([
  76.                 'status' => 'error',
  77.                 'messages' => 'MAIL_NOT_FOUND_ERROR'
  78.             ]);
  79.         }
  80.         $token rtrim(chunk_split(strtolower(md5(uniqid(random_bytes(32), true))), 8'-'), '-');
  81.         $user->setRegeneratePasswordToken($token);
  82.         $dm->persist($user);
  83.         $dm->flush();
  84.         $eventDispatcher->dispatch(new UserForgotPasswordEvent($user), UserForgotPasswordEvent::NAME);
  85.         return new JsonResponse([
  86.                 'status' => 'ok'
  87.             ]
  88.         );
  89.     }
  90.     /**
  91.      * @Route("/password_reset/{token}",
  92.      *     name="reset_user_password_with_token",
  93.      *     methods={"GET"},
  94.      *     options = { "expose" = true }
  95.      * )
  96.      *
  97.      * @param Request $request
  98.      * @param EntityManagerInterface $dm
  99.      * @return Response
  100.      * @throws \Exception
  101.      */
  102.     public function passwordReset(Request $requestEntityManagerInterface $dm$token)
  103.     {
  104.         $tokenExists true;
  105.         if (!$user $dm->getRepository(User::class)->findOneBy(['regeneratePasswordToken' => $token])) {
  106.             $tokenExists false;
  107.         }
  108.         $view $this->renderView('administration/security/reset.html.twig', ['token'=> $token'tokenExists' => $tokenExists]);
  109.         return new Response($view);
  110.     }
  111.     /**
  112.      * @Route("/password_reset_action/{token}",
  113.      *     condition="request.isXmlHttpRequest()",
  114.      *     name="password_reset_action",
  115.      *     methods={"POST"},
  116.      *     options = { "expose" = true }
  117.      * )
  118.      *
  119.      * @param Request $request
  120.      * @param EntityManagerInterface $dm
  121.      * @param EventDispatcherInterface $eventDispatcher
  122.      * @return JsonResponse
  123.      * @throws \Exception
  124.      */
  125.     public function passwordResetAction(Request $requestEntityManagerInterface $dmEventDispatcherInterface $eventDispatcherUserPasswordEncoderInterface $passwordEncoder,  $token)
  126.     {
  127.         $passwordOne $request->get('_password_1'false);
  128.         $passwordTwo $request->get('_password_2'false);
  129.         if ($passwordOne !== $passwordTwo){
  130.             return new JsonResponse ([
  131.                 'status' => 'error',
  132.                 'messages' => 'PASSWORD_MISMATCH'
  133.             ]);
  134.         }
  135.         if (!$user $dm->getRepository(User::class)->findOneBy(['regeneratePasswordToken' => $token])) {
  136.             return new JsonResponse ([
  137.                 'status' => 'error',
  138.                 'messages' => 'USER_NOT_FOUND_ERROR'
  139.             ]);
  140.         }
  141.         $user->setRegeneratePasswordToken('');
  142.         $user->setPassword($passwordEncoder->encodePassword($user$passwordOne));
  143.         $dm->persist($user);
  144.         $dm->flush();
  145.         $eventDispatcher->dispatch(new UserResetPasswordEvent($user), UserResetPasswordEvent::NAME);
  146.         return new JsonResponse([
  147.                 'status' => 'ok'
  148.             ]
  149.         );
  150.     }
  151.     /**
  152.      * @Route("/admin/change_password",
  153.      *     condition="request.isXmlHttpRequest()",
  154.      *     name="password_change_action",
  155.      *     methods={"POST"},
  156.      *     options = { "expose" = true }
  157.      * )
  158.      *
  159.      * @param Request $request
  160.      * @param EntityManagerInterface $dm
  161.      * @param EventDispatcherInterface $eventDispatcher
  162.      * @param UserPasswordEncoderInterface $passwordEncoder
  163.      * @param Security $security
  164.      * @return JsonResponse
  165.      */
  166.     public function passwordChangeAction(Request $requestEntityManagerInterface $dmEventDispatcherInterface $eventDispatcherUserPasswordEncoderInterface $passwordEncoderSecurity $security)
  167.     {
  168.         $oldPassword $request->get('_oldPassword'false);
  169.         $passwordOne $request->get('_password_1'false);
  170.         $passwordTwo $request->get('_password_2'false);
  171.         $user $security->getToken()->getUser();
  172.         if (!$passwordEncoder->isPasswordValid($user$oldPassword)){
  173.             return new JsonResponse ([
  174.                 'status' => 'error',
  175.                 'messages' => 'CURRENT_PASSWORD_INVALID'
  176.             ]);
  177.         }
  178.         if ($passwordOne !== $passwordTwo){
  179.             return new JsonResponse ([
  180.                 'status' => 'error',
  181.                 'messages' => 'PASSWORD_MISMATCH'
  182.             ]);
  183.         }
  184.         $user->setPassword($passwordEncoder->encodePassword($user$passwordOne));
  185.         $dm->persist($user);
  186.         $dm->flush();
  187.         $eventDispatcher->dispatch(new UserResetPasswordEvent($user), UserResetPasswordEvent::NAME);
  188.         return new JsonResponse([
  189.                 'status' => 'ok'
  190.             ]
  191.         );
  192.     }
  193. }