<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
class LoginController extends Controller
{
/**
* @Route("/admin/login", name="login")
*/
public function loginAction(Request $request)
{
$authenticationUtils = $this->get('security.authentication_utils');
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render(':default:login.html.twig', array(
'last_username' => $lastUsername,
'error' => $error,
));
}
private function mc_encrypt($encrypt) {
$encrypt = $encrypt."_".(new \DateTime())->modify("+24 hours")->format('dmY');
// $mc_key = substr($this->container->getParameter('secret'),0,32);
// $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
// $passcrypt = trim(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $mc_key, trim($encrypt), MCRYPT_MODE_ECB, $iv));
$passcrypt = $encrypt;
$encode = base64_encode($passcrypt);
$encode = urlencode($encode);
return $encode;
}
private function mc_decrypt($decrypt) {
$mc_key = substr($this->container->getParameter('secret'),0,32);
$decrypt = urldecode($decrypt);
$decoded = base64_decode($decrypt);
// $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
// $decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $mc_key, trim($decoded), MCRYPT_MODE_ECB, $iv));
$decrypted = $decoded;
$decrypted = explode('_', $decrypted);
$date = array_pop($decrypted);
$decrypted = implode('_', $decrypted);
if ($date != (new \DateTime())->modify("+24 hours")->format('dmY') && $date != (new \DateTime())->format('dmY')) {
return "";
}
return $decrypted;
}
/**
* @Route("/account/reset/", name="request_password_reset")
*/
public function resetPasswordAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$resetForm = $this->createForm('AppBundle\Form\ResetType');
$resetForm->handleRequest($request);
if ($resetForm->isSubmitted())
if ($resetForm->isValid()) {
$user = $em->getRepository('AppBundle:User')->findOneBy(array('email' => $resetForm->get('email')->getData()));
if ($user) {
$message = \Swift_Message::newInstance()
->setSubject('Your requested password reset token')
->setFrom($this->container->getParameter('site_from_email'))
->setTo($user->getEmail())
->setBody(
$this->renderView(
'email/reset_token.html.twig',
array(
'user' => $user,
'token' => $this->mc_encrypt($user->getId())
)
),
'text/html'
);
$this->get('mailer')->send($message);
}
$this->addFlash(
'success',
'A password reset token has been sent to you should an account with that email exist, it will be valid for 24 hours'
);
return $this->redirectToRoute('login', array());
} else {
$this->addFlash(
'success',
'A password reset token has been sent to you should an account with that email exist, it will be valid for 24 hours'
);
return $this->redirectToRoute('login', array());
}
return $this->render(':default:password.html.twig', array(
'reset_form' => $resetForm->createView(),
));
}
/**
* Displays a form to edit an existing user entity.
*
* @Route("/account/reset/{{token}}", name="password_reset_confirm")
* @Method({"POST","GET"})
*/
public function resetPasswordConfirmAction(Request $request, $token)
{
$em = $this->getDoctrine()->getManager();
$user_id = $this->mc_decrypt($token);
$user = $em->getRepository('AppBundle:User')->findOneBy(array('id' => $user_id));
if (!$user) {
$this->addFlash(
'success',
'That token has expired, please try resetting your password again'
);
return $this->redirectToRoute('login', array());
}
$resetForm = $this->createForm('AppBundle\Form\PasswordResetType', $user);
$resetForm->handleRequest($request);
if ($resetForm->isSubmitted() && $resetForm->isValid()) {
$encoder = $this->container->get('security.password_encoder');
$encoded = $encoder->encodePassword($user, $resetForm->get('password')->getData());
$user->setPassword($encoded);
$this->getDoctrine()->getManager()->flush();
$this->addFlash(
'success',
'Your password has been successfully changed, please try logging in again'
);
return $this->redirectToRoute('login', array());
}
return $this->render('default/password.html.twig', array(
'reset_form' => $resetForm->createView(),
));
}
}