<?php
namespace App\Controller;
use App\Entity\User;
use App\Tools\UserHelper;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Contracts\Translation\TranslatorInterface;
class SecurityController extends AbstractController
{
/**
* @param AuthenticationUtils $authenticationUtils
* @param bool $snippet
* @return Response
* @Route("/login", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils, $snippet = false): Response
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
if ($this->getUser()) {
if (!empty($_GET['referer'])){
return $this->redirect($_GET['referer']);
}else{
return $this->redirectToRoute('app_home');
}
}
if ($snippet) {
return $this->render(
'snippets/login-form.html.twig',
[
'last_username' => $lastUsername,
'error' => $error,
]
);
} else {
return $this->render(
'security/login.html.twig',
[
'last_username' => $lastUsername,
'error' => $error,
]
);
}
}
public function adminLogin(AuthenticationUtils $authenticationUtils): Response
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
if ($this->getUser()) {
//dump($this->getUser());die;
return $this->redirectToRoute('admin_dashboard');
}
//dump($error,$this->getUser());die;
return $this->render(
'admin/security/login.html.twig',
[
'last_username' => $lastUsername,
'error' => $error,
]
);
}
/**
* @param Request $request
* @param TranslatorInterface $translator
* @param UserHelper $userHelper
* @param MailerInterface $mailer
* @param UserPasswordEncoderInterface $passwordEncoder
* @return Response
* @Route("/forgot-password", name="app_forgot_password")
*/
public function forgotPassword(Request $request,TranslatorInterface $translator, UserHelper $userHelper, MailerInterface $mailer,UserPasswordEncoderInterface $passwordEncoder):Response{
if ($this->getUser()){
return $this->redirectToRoute('app_home');
}
$msg = '';
$status = false;
if (!is_null($request->get('btPassword'))){
$entityManager = $this->getDoctrine()->getManager();
/** @var User $user */
$user = $entityManager->getRepository(User::class)->findOneBy(['email'=>$request->get("email")]);
if (!empty($user)){
$passwordGenerated = $userHelper->randomPassword();
$user->setPassword(
$passwordEncoder->encodePassword(
$user,
$passwordGenerated
)
);
$firsName = empty($user->getSalutation()) ? $user->getFirstName() : $user->getSalutation();
$html_email = $this->renderView('includes/forgot-password-mail.html.twig',[
'firstName'=>$firsName,
'lastName'=>$user->getLastName(),
'email'=>$user->getEmail(),
'password'=>$passwordGenerated
]);
$email = (new Email())
->from($this->getParameter('mail_sender_address'))
->to($user->getEmail())
;
$email->subject($this->getParameter('site_title') ." - ".$translator->trans("Passwort vergessen"))
->html($html_email);
$msg = '';
try {
$mailer->send($email);
$status = true;
$msg = $translator->trans("The password has been sent to your email address. Please check it.");
$entityManager->flush();
} catch (TransportExceptionInterface $e) {
$msg = $e->getMessage();
}
}else{
$status = false;
$msg = $translator->trans("The user doesn't exists.");
}
}
return $this->render('security/forgot-password.html.twig',[
'status'=>$status,
'msg'=>$msg
]);
}
}