<?php
namespace App\Controller;
use App\Entity\User;
use App\Service\MailService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\NamedAddress;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class LoginSecurityController extends AbstractController
{
public function __construct(RequestStack $requestStack, MailService $mailer)
{
$this->baseUrl = $requestStack->getCurrentRequest()->getSchemeAndHttpHost();
$this->mailer = $mailer;
}
/**
* @Route("/admin", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils, Security $security): Response
{
if ($security->isGranted('ROLE_ADMIN')) {
return $this->redirectToRoute('admin_home');
}
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
/**
* @Route("/owner", name="owner_login")
*/
public function owner_login(AuthenticationUtils $authenticationUtils, Security $security): Response
{
if ($security->isGranted('ROLE_OWNER')) {
return $this->redirectToRoute('owner_home');
}
$error = $authenticationUtils->getLastAuthenticationError();
//dump($error); die;
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/owner_login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
/**
* This is the route the user can use to logout.
*
* But, this will never be executed. Symfony will intercept this first
* and handle the logout automatically. See logout in config/packages/security.yaml
*
* @Route("/logout", name="security_logout")
*/
public function logout(): void
{
throw new \Exception('This should never be reached!');
}
/**
* @Route("/login_success", name="login_success")
*/
public function postLoginRedirectAction()
{
$user = $this->getUser();
return $this->redirectToRoute("admin_home");
}
/**
* @Route("/", methods={"GET"}, name="homepage")
*/
public function home(){
return $this->render('home.html.twig');
}
/**
* @Route("/esqueci-a-senha", methods={"GET"}, name="forgot_password")
*/
public function forgot()
{
return $this->render('security/forgot.html.twig', [
]);
}
/**
* @Route("/forgot-password-send", methods={"POST"}, name="forgot_password_send")
*/
public function forgot_send(Request $request)
{
$email = $request->request->get('email');
$user = $this->getDoctrine()
->getRepository(User::class)
->findOneBy(array('email' => $email));
if(!$user){
$this->addFlash('danger', 'E-mail não encontrado!');
return $this->redirectToRoute('forgot_password');
}
$link = $this->generat_link($user->getEmail());
$user->setRecover($link);
$this->getDoctrine()->getManager()->flush();
$this->send_link($user);
$this->addFlash('success', 'Link de recuperação enviado<br>Por favor, verifique seu e-mail.');
return $this->redirectToRoute('forgot_password');
}
/**
* @Route("/forgot-password-validation/{cod}", methods={"GET"}, name="forgot_password_validation")
*/
public function forgot_validation($cod)
{
$user = $this->getDoctrine()
->getRepository(User::class)
->findOneBy(array('recover' => $cod));
if(!$user){
$this->addFlash('danger', 'Link inválido!');
return $this->redirectToRoute('forgot_password');
}
return $this->render('security/recover.html.twig', [
'user' => $user
]);
}
/**
* @Route("/forgot-password-update", methods={"POST"}, name="forgot_password_update")
*/
public function forgot_update(Request $request, UserPasswordEncoderInterface $passwordEncoder)
{
$code = $request->request->get('code');
$pass = $request->request->get('pass');
$confirm_pass = $request->request->get('confirm_pass');
$user = $this->getDoctrine()
->getRepository(User::class)
->findOneBy(array('recover' => $code));
if(!$user){
$this->addFlash('danger', 'Usuário não encontrado!');
return $this->redirectToRoute('forgot_password_validation',['cod' => $code]);
}
if($pass!=$confirm_pass){
$this->addFlash('danger', 'As senhas não conferem!');
return $this->redirectToRoute('forgot_password_validation',['cod' => $code]);
}
$user->setPassword(
$passwordEncoder->encodePassword(
$user,
$pass
)
);
$user->setRecover("");
$this->getDoctrine()->getManager()->flush();
$this->addFlash('success', 'Alteração de senha realizada com sucesso!');
return $this->redirectToRoute('app_login');
}
protected function generat_link($email){
return sha1($email.time());
}
protected function send_link($user)
{
$message = '<style>
@import url(\'https://fonts.googleapis.com/css2?family=Roboto:wght@300&display=swap\');
</style>
<table style="width: 500px; text-align: center; font-family: \'Roboto\', sans-serif;">
<tr>
<td style="height: 100px; background: linear-gradient(188.93deg, #FFFFFF 0%, #E2DFFF 0.01%, #FFFFFF 105.25%);">
<img src="https://admin.bnbguests.com.br/imgs/logo-email.png">
</td>
</tr>
<tr>
<td style="padding-top: 20px">
<p><b>Solicitação de recuperação de senha</b></p>
<p>Alguém solicitou a recuperação da sua senha bnbguests</p>
<p>Se você não fez essa solicitação, ignore este e-mail. Se você deseja continuar:</p>
<p><a href="'.$this->baseUrl.$this->generateUrl('forgot_password_validation', array('cod'=>$user->getRecover())).'">Click aqui para cadastrar uma nova senha</a></p>
<p>Att,<br>Equipe bnbguests</p>
<p style="color: #cccccc"><i>*Não responda este e-mail*</i></p>
</td>
</tr>
</table>';
$this->mailer->sendMail($user->getEmail(),'Recuperação de senha - bnbguests', $message);
}
}