<?php
namespace App\Controller;
use App\Entity\User;
use Symfony\Component\Mime\Email;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
/**
* @Route("/login", name="login")
*/
public function loginAction(Request $request, AuthenticationUtils $authenticationUtils){
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render(
'Security/login.html.twig',
array(
// last username entered by the user
'last_username' => $lastUsername,
'error' => $error,
)
);
}
/**
* @Route("/login_check", name="login_check")
*/
public function loginCheck()
{
}
private function sendRecoveryEmail(Request $request, EntityManagerInterface $em, MailerInterface $mailer, $user)
{
$host = $request->getHost();
$token = $user->newRecoveryToken();
$em->persist($user);
$em->flush();
$message = (new Email())
->subject('Recuperacion de contraseña')
->to($user->getEmail())
->html(
"Recuperacion de contraseña: https://$host/recovery_pass?rec_token=$token",
'text/html'
);
$mailer->send($message);
}
/**
* @Route("/logout", name="app_logout", methods={"GET"})
*/
public function logout(): void
{
// controller can be blank: it will never be called!
throw new \Exception('Don\'t forget to activate logout in security.yaml');
}
/**
* @Route("/recovery_pass", name="recovery_pass")
*/
public function recoveryPassAction(Request $request, ManagerRegistry $doctrine, EntityManagerInterface $em, MailerInterface $mailer)
{
$requestData = $request->request;
$userRepo = $doctrine->getRepository(User::class);
$mode = 'email';
if ($request->query->has('rec_token')) {
$mode = 'password';
$rec_token = $request->query->get('rec_token');
$user = $userRepo->findOneBy(['recoveryToken' => $rec_token]);
if ($user === null || $user->isRecoveryTokenExpired()) {
$info_msg = 'El enlace de recuperacion no es valido';
$mode = 'none';
}
}
if ($requestData->has('password') && $user) {
$newPassword = $request->get('password');
$error_msg = $this->checkPassword($newPassword, $requestData->get('password_c'), $user);
if (empty($error_msg)) {
$user->setPassword($newPassword);
$em->persist($user);
$em->flush();
$info_msg = "Contraseña actualizada correctamente";
$to_login = true;
$mode = 'none';
}
}
if ($requestData->has('email')) {
$user = $userRepo->findOneBy(['email' => $requestData->get('email')]);
if ($user !== null) {
$this->sendRecoveryEmail($request, $em, $mailer, $user);
$info_msg = "Si el correo esta en el sistema se enviara un correo de recuperacion";
$mode = 'none';
}
}
return $this->render('Security/passwordRecovery.html.twig', [
'error_msg' => $error_msg ?? null,
'info_msg' => $info_msg ?? null,
'mode' => $mode,
'rec_token' => $rec_token ?? null,
'to_login' => $to_login ?? false
]);
}
private function checkPassword($password, $passwordConfirm, $user)
{
$error_msg = null;
if ($password !== $passwordConfirm) {
$error_msg = "Las contraseñas no son iguales";
}
if (strlen($password) < 8) {
$error_msg = 'La contraseña debe ser como minimo de 8 caracteres';
}
if ($user->comparePassword($password)) {
$error_msg = 'No puede ser la misma clave';
}
return $error_msg;
}
public function loginCheckAction()
{
}
/**
* @Route("/setup_auth", name="setup_auth")
*/
public function updatePasswordAction(Request $request)
{
$newPassword = $request->get('password', null);
$newPasswordConfirm = $request->get('password_c', null);
$email = $request->get('email', null);
$error_msg = null;
$user = $this->getUser();
if ($user->needsPassSetup) {
$error_msg = $this->checkPassword($newPassword, $newPasswordConfirm, $user);
}
if ($user->needsEmailSetup) {
if (empty($email)) {
$error_msg = 'El email no puede estar vacio';
}
}
if (empty($error_msg)) {
if ($user->needsPassSetup) {
$user->setPassword($newPassword);
}
if ($user->needsEmailSetup) {
$user->setEmail($email);
}
$em = $this->doctrine->getManager();
$em->persist($user);
$em->flush();
return $this->redirectToRoute('home', [], 301);
}
return $this->render('Security/requiredUserSetup.html.twig', ['error_msg' => $error_msg]);
}
}