src/AppBundle/Controller/LoginController.php line 15

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  5. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  6. use Symfony\Component\HttpFoundation\Request;
  7. class LoginController extends Controller
  8. {
  9.     /**
  10.      * @Route("/admin/login", name="login")
  11.      */
  12.     public function loginAction(Request $request)
  13.     {
  14.         $authenticationUtils $this->get('security.authentication_utils');
  15.         // get the login error if there is one
  16.         $error $authenticationUtils->getLastAuthenticationError();
  17.         // last username entered by the user
  18.         $lastUsername $authenticationUtils->getLastUsername();
  19.         return $this->render(':default:login.html.twig', array(
  20.             'last_username' => $lastUsername,
  21.             'error'         => $error,
  22.         ));
  23.     }
  24.     private function mc_encrypt($encrypt) {
  25.         $encrypt $encrypt."_".(new \DateTime())->modify("+24 hours")->format('dmY');
  26.         // $mc_key = substr($this->container->getParameter('secret'),0,32);
  27.         // $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
  28.         // $passcrypt = trim(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $mc_key, trim($encrypt), MCRYPT_MODE_ECB, $iv));
  29.         $passcrypt $encrypt;
  30.         $encode base64_encode($passcrypt);
  31.         $encode urlencode($encode);
  32.         return $encode;
  33.     }
  34.     private function mc_decrypt($decrypt) {
  35.         $mc_key substr($this->container->getParameter('secret'),0,32);
  36.         $decrypt urldecode($decrypt);
  37.         $decoded base64_decode($decrypt);
  38.         // $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
  39.         // $decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $mc_key, trim($decoded), MCRYPT_MODE_ECB, $iv));
  40.         $decrypted $decoded;
  41.         $decrypted explode('_'$decrypted);
  42.         $date array_pop($decrypted);
  43.         $decrypted implode('_'$decrypted);
  44.         if ($date != (new \DateTime())->modify("+24 hours")->format('dmY') && $date != (new \DateTime())->format('dmY')) {
  45.             return "";
  46.         }
  47.         return $decrypted;
  48.     }
  49.     /**
  50.      * @Route("/account/reset/", name="request_password_reset")
  51.      */
  52.     public function resetPasswordAction(Request $request)
  53.     {
  54.         $em $this->getDoctrine()->getManager();
  55.         $resetForm $this->createForm('AppBundle\Form\ResetType');
  56.         $resetForm->handleRequest($request);
  57.         if ($resetForm->isSubmitted()) 
  58.             if ($resetForm->isValid()) {
  59.                 $user $em->getRepository('AppBundle:User')->findOneBy(array('email' => $resetForm->get('email')->getData()));
  60.                 if ($user) {
  61.                     $message = \Swift_Message::newInstance()
  62.                         ->setSubject('Your requested password reset token')
  63.                         ->setFrom($this->container->getParameter('site_from_email'))
  64.                         ->setTo($user->getEmail())
  65.                         ->setBody(
  66.                             $this->renderView(
  67.                                 'email/reset_token.html.twig',
  68.                                 array(
  69.                                     'user' => $user,
  70.                                     'token' => $this->mc_encrypt($user->getId())
  71.                                 )
  72.                             ),
  73.                             'text/html'
  74.                         );
  75.                     $this->get('mailer')->send($message);
  76.                 }
  77.                 $this->addFlash(
  78.                     'success',
  79.                     'A password reset token has been sent to you should an account with that email exist, it will be valid for 24 hours'
  80.                 );
  81.                 return $this->redirectToRoute('login', array());
  82.             
  83.             }  else {
  84.             
  85.             $this->addFlash(
  86.                 'success',
  87.                 'A password reset token has been sent to you should an account with that email exist, it will be valid for 24 hours'
  88.             );
  89.             return $this->redirectToRoute('login', array());
  90.         }
  91.         return $this->render(':default:password.html.twig', array(
  92.             'reset_form' => $resetForm->createView(),
  93.         ));
  94.     }
  95.     /**
  96.      * Displays a form to edit an existing user entity.
  97.      *
  98.      * @Route("/account/reset/{{token}}", name="password_reset_confirm")
  99.      * @Method({"POST","GET"})
  100.      */
  101.     public function resetPasswordConfirmAction(Request $request$token)
  102.     {
  103.         $em $this->getDoctrine()->getManager();
  104.         $user_id $this->mc_decrypt($token);
  105.         $user $em->getRepository('AppBundle:User')->findOneBy(array('id' => $user_id));
  106.         
  107.         if (!$user) {
  108.             $this->addFlash(
  109.                 'success',
  110.                 'That token has expired, please try resetting your password again'
  111.             );
  112.             
  113.             return $this->redirectToRoute('login', array());
  114.         }
  115.         $resetForm $this->createForm('AppBundle\Form\PasswordResetType'$user);
  116.         $resetForm->handleRequest($request);
  117.         if ($resetForm->isSubmitted() && $resetForm->isValid()) {
  118.                         
  119.             $encoder $this->container->get('security.password_encoder');
  120.             $encoded $encoder->encodePassword($user$resetForm->get('password')->getData());
  121.             $user->setPassword($encoded);
  122.             $this->getDoctrine()->getManager()->flush();
  123.             $this->addFlash(
  124.                 'success',
  125.                 'Your password has been successfully changed, please try logging in again'
  126.             );
  127.             return $this->redirectToRoute('login', array());
  128.         }
  129.         return $this->render('default/password.html.twig', array(
  130.             'reset_form' => $resetForm->createView(),
  131.         ));
  132.     }
  133. }