<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use App\Service\HelperService;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
class SecurityController extends AbstractController
{
private $passwordEncoder;
public function __construct(UserPasswordEncoderInterface $passwordEncoder)
{
$this->passwordEncoder = $passwordEncoder;
}
/**
* @Route("/login", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
$logo = "https://dev.laiglesiavirtual.com/assets/img/logo.png";
$register = true;
$serverName = $_SERVER["SERVER_NAME"];
if($serverName == 'somosalientodevida.com' || $serverName == 'www.somosalientodevida.com')
{
$logo = "https://dev.laiglesiavirtual.com/public/uploads/logo-single-6227e76d12f57.png";
$register = false;
}
// 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, 'logo' => $logo, 'register' => $register]);
}
/**
* @Route("/recovery", name="app_recovery")
*/
public function recover(Request $request, HelperService $helperService, UserRepository $userRepository, EntityManagerInterface $entityManager): Response
{
$logo = "https://dev.laiglesiavirtual.com/assets/img/logo.png";
$serverName = $_SERVER["SERVER_NAME"];
if($serverName == 'somosalientodevida.com' || $serverName == 'www.somosalientodevida.com')
{
$logo = "https://dev.laiglesiavirtual.com/public/uploads/logo-single-6227e76d12f57.png";
}
if($request->get('recovery_email'))
{
$userCheck = $userRepository->findOneBy(['email' => $request->get('recovery_email'), 'status' => 'ACTIVO']);
if($userCheck)
{
$plainpwd = substr(md5(microtime()),rand(0,26),8)."*";
$encoded = $this->passwordEncoder->encodePassword($userCheck, $plainpwd);
$userCheck->setPassword($encoded);
$entityManager->persist($userCheck);
$entityManager->flush();
$organizationName = $userCheck->getOrganization()->getName();
$message = "<p>Ha solicitado recuperar la contraseña para su cuenta en la Plataforma de $organizationName. Puede utilizar el siguiente password para iniciar sesión.</p>";
$message .= "<p><b>Contraseña temporal:</b> $plainpwd</p>";
$message .= "<p>Una vez dentro de su cuenta, no olvide cambiar su contraseña.</p>";
$mailTo = $request->get('recovery_email');
$subject = "Recuperación de contraseña";
$paths = $helperService->getPaths();
$logo = $paths['uploads_path'].$userCheck->getOrganization()->getLogoPath();
if($helperService->sendEmail($mailTo, $subject, $message, false, $organizationName, $logo))
{
$this->addFlash('success', "Se ha enviado un email con las instrucciones de recuperación");
} else {
$this->addFlash('error', "No se pudo enviar el email de recuperación. Contacte con soporte técnico.");
}
} else {
$this->addFlash('error', "El email no se encuentra registrado");
}
}
return $this->render('security/recovery.html.twig', [
'logo' => $logo
]);
}
/**
* @Route("/logout", name="app_logout")
*/
public function logout(): void
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
}