<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use App\Form\ClientesResetPassType;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Clientes;
use App\Service\SendMailTemplate;
class SecurityController extends AbstractController {
/**
* @Route("/admin/login", name="app_login_admin")
*/
public function login2Admin(AuthenticationUtils $authenticationUtils): Response {
// if ($this->getUser()) {
// return $this->redirectToRoute('target_path');
// }
// 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', ['last_username' => $lastUsername, 'error' => $error, 'login_tipo' => 'Administrador', 'restaurarPwd' => false]);
}
/**
* @Route("/admin/logout", name="app_logout_admin")
*/
public function logoutAdmin(): void {
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
/**
* @Route("/clientes/login", name="app_login_clientes")
*/
public function loginClientes(AuthenticationUtils $authenticationUtils): Response {
if ($this->getUser()) {
return $this->redirectToRoute('index_cliente');
}
// 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', ['last_username' => $lastUsername, 'error' => $error, 'login_tipo' => 'Clientes', 'restaurarPwd' => true]);
}
/**
* @Route("/clientes/logout", name="app_logout_clientes")
*/
public function logoutClientes(): void {
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
/**
* @Route("/clientes/loginlink", name="app_login_clientes_link_check")
*/
public function loginlinkClientes() {
throw new \LogicException('This code should never be reached');
}
public function generatePassword($length) {
$key = "";
$pattern = "1234567890abcdefghijklmnopqrstuvwxyz";
$max = strlen($pattern) - 1;
for ($i = 0; $i < $length; $i++) {
$key .= substr($pattern, mt_rand(0, $max), 1);
}
return $key;
}
/**
* @Route("/clientes/recuperarpwd", name="recuperar_pwd_cliente")
*/
public function RecuperarPwdCliente(Request $request, EntityManagerInterface $entityManager, SendMailTemplate $smt): Response {
$cliente = new Clientes();
$form = $this->createForm(ClientesResetPassType::class, $cliente);
$form->handleRequest($request);
$error = NULL;
$valido = false;
if ($form->isSubmitted() && $form->isValid()) {
$cliente_buscar = $entityManager->getRepository(Clientes::class)->findOneBy(['clienteEmail' => $cliente->getClienteEmail()]);
if (!empty($cliente_buscar)) {
$clienteTokenPass = sha1(rand());
$cliente_buscar->setClienteTokenPass($clienteTokenPass);
$cliente_buscar->setClienteSolicitaPass(1);
$entityManager->flush();
$smt->envioRecuperarPass($cliente_buscar);
$valido = "Hemos enviado un email con un enlace para que puedas restaurar tu contraseña.";
} else {
$error = "No se ha encontrado ningún cliente con ese correo electrónico. <a href='https://amprogramacion.es/soporte' target='_blank'>Abrir un ticket de soporte.</a>";
}
}
return $this->renderForm('security/recuperarpwd.html.twig', [
'form' => $form,
'login_tipo' => 'cliente',
'error' => $error,
'valido' => $valido
]);
}
/**
* @Route("/clientes/confirmarpwd/{token}", name="cliente_confirmar_pwd")
*/
public function confirmarPwd($token, EntityManagerInterface $entityManager, SendMailTemplate $smt) {
$cliente_buscar = $entityManager->getRepository(Clientes::class)->findOneBy(['clienteTokenPass' => $token, 'clienteSolicitaPass' => '1']);
$error = NULL;
$valido = false;
if (!empty($cliente_buscar)) {
$nuevapass = $this->generatePassword(8);
$cliente_buscar->setClientePass(password_hash($nuevapass, PASSWORD_DEFAULT));
$cliente_buscar->setClienteTokenPass(NULL);
$cliente_buscar->setClienteSolicitaPass(NULL);
$entityManager->flush();
$smt->envioPassGenerada($cliente_buscar, $nuevapass);
$valido = "<strong>Hemos verificado tu solicitud de cambio de contraseña.</strong> La recibirás en tu correo electrónico en unos minutos.";
} else {
$error = "No se puede recuperar la contraseña porque el cliente no la ha solicitado o el enlace es incorrecto.";
}
return $this->renderForm('security/recuperarpwd.html.twig', [
'form' => NULL,
'login_tipo' => 'cliente',
'error' => $error,
'valido' => $valido
]);
}
}