<?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 Twig\Environment;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Address;
use App\Service\HelperService;
use App\Entity\SystemConfiguration;
use App\Entity\MailLog;
use App\Entity\User;
use App\Repository\UserRepository;
class SecurityController extends AbstractController
{
private $passwordEncoder;
private $twig;
private $mailer;
public function __construct(
UserPasswordEncoderInterface $passwordEncoder,
Environment $twig,
MailerInterface $mailer
) {
$this->passwordEncoder = $passwordEncoder;
$this->twig = $twig;
$this->mailer = $mailer;
}
/**
* @Route("/login", name="app_login")
*/
public function login(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]);
}
/**
* @Route("/recover-password", name="app_recover_password")
*/
public function recoverPassword(Request $request, AuthenticationUtils $authenticationUtils, EntityManagerInterface $entityManager, HelperService $helperService): Response
{
if ($request->isMethod('POST')) {
if($request->get("email_recover")){
$email = $request->get("email_recover");
$userObj = $entityManager->getRepository(User::class)->findOneBy([
'email' => $email
]);
$systemConfiguration = $entityManager->getRepository(SystemConfiguration::class)->findOneBy([
'is_active' => 1
]);
$rootWebUrl = $systemConfiguration->getImgUrl();
if($userObj){
$pass = $helperService->randomChars(8);
$encoded = $this->passwordEncoder->encodePassword($userObj, $pass);
$userObj->setPassword($encoded);
$entityManager->persist($userObj);
$entityManager->flush();
$email_receipt = $email;
$subject = 'Su contraseña ha sido restablecida exitosamente';
$text_email = "Recientemente solicitó restablecer su contraseña para su cuenta de Disatel. La contraseña temporal se muestra a continuación:
<br><b><h3>".$pass."</h3></b>
";
$message = $this->twig->render('_mail_template.html.twig', [
'emailTitle' => $subject,
'rootWebUrl' => $rootWebUrl."/public",
'message' => $text_email
]);
$email = (new Email())
->from(new Address("notificaciones@grupodisatel.com", 'DISATEL'))
->to($email_receipt)
->subject($subject)
->html($message);
$response_mail = $this->mailer->send($email);
$mailLog = new MailLog();
$mailLog->setEmail($email_receipt);
$mailLog->setResponse($response_mail);
$mailLog->setContent($text_email);
$mailLog->setCreatedAt(new \DateTime());
$entityManager->persist($mailLog);
$entityManager->flush();
$this->addFlash('success', $this->getParameter('form_new_success'));
}
return $this->redirectToRoute('app_login', [], Response::HTTP_SEE_OTHER);
}
}
}
/**
* @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.');
}
}