src/Controller/LoginSecurityController.php line 89

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Service\MailService;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Mailer\MailerInterface;
  10. use Symfony\Component\Mime\Address;
  11. use Symfony\Component\Mime\Email;
  12. use Symfony\Component\Mime\NamedAddress;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  15. use Symfony\Component\Security\Core\Security;
  16. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  17. class LoginSecurityController extends AbstractController
  18. {
  19.     public function __construct(RequestStack $requestStackMailService $mailer)
  20.     {
  21.         $this->baseUrl $requestStack->getCurrentRequest()->getSchemeAndHttpHost();
  22.         $this->mailer $mailer;
  23.     }
  24.     /**
  25.      * @Route("/admin", name="app_login")
  26.      */
  27.     public function login(AuthenticationUtils $authenticationUtilsSecurity $security): Response
  28.     {
  29.         if ($security->isGranted('ROLE_ADMIN')) {
  30.             return $this->redirectToRoute('admin_home');
  31.         }
  32.         $error $authenticationUtils->getLastAuthenticationError();
  33.         $lastUsername $authenticationUtils->getLastUsername();
  34.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  35.     }
  36.     /**
  37.      * @Route("/owner", name="owner_login")
  38.      */
  39.     public function owner_login(AuthenticationUtils $authenticationUtilsSecurity $security): Response
  40.     {
  41.         if ($security->isGranted('ROLE_OWNER')) {
  42.             return $this->redirectToRoute('owner_home');
  43.         }
  44.         $error $authenticationUtils->getLastAuthenticationError();
  45.         //dump($error); die;
  46.         $lastUsername $authenticationUtils->getLastUsername();
  47.         return $this->render('security/owner_login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  48.     }
  49.     /**
  50.      * This is the route the user can use to logout.
  51.      *
  52.      * But, this will never be executed. Symfony will intercept this first
  53.      * and handle the logout automatically. See logout in config/packages/security.yaml
  54.      *
  55.      * @Route("/logout", name="security_logout")
  56.      */
  57.     public function logout(): void
  58.     {
  59.         throw new \Exception('This should never be reached!');
  60.     }
  61.     /**
  62.      * @Route("/login_success", name="login_success")
  63.      */
  64.     public function postLoginRedirectAction()
  65.     {
  66.         $user $this->getUser();
  67.         return $this->redirectToRoute("admin_home");
  68.     }
  69.     /**
  70.      * @Route("/", methods={"GET"}, name="homepage")
  71.      */
  72.     public function home(){
  73.         return $this->render('home.html.twig');
  74.     }
  75.     /**
  76.      * @Route("/esqueci-a-senha", methods={"GET"}, name="forgot_password")
  77.      */
  78.     public function forgot()
  79.     {
  80.         return $this->render('security/forgot.html.twig', [
  81.         ]);
  82.     }
  83.     /**
  84.      * @Route("/forgot-password-send", methods={"POST"}, name="forgot_password_send")
  85.      */
  86.     public function forgot_send(Request $request)
  87.     {
  88.         $email $request->request->get('email');
  89.         $user $this->getDoctrine()
  90.             ->getRepository(User::class)
  91.             ->findOneBy(array('email' => $email));
  92.         if(!$user){
  93.             $this->addFlash('danger''E-mail não encontrado!');
  94.             return $this->redirectToRoute('forgot_password');
  95.         }
  96.         $link $this->generat_link($user->getEmail());
  97.         $user->setRecover($link);
  98.         $this->getDoctrine()->getManager()->flush();
  99.         $this->send_link($user);
  100.         $this->addFlash('success''Link de recuperação enviado<br>Por favor, verifique seu e-mail.');
  101.         return $this->redirectToRoute('forgot_password');
  102.     }
  103.     /**
  104.      * @Route("/forgot-password-validation/{cod}", methods={"GET"}, name="forgot_password_validation")
  105.      */
  106.     public function forgot_validation($cod)
  107.     {
  108.         $user $this->getDoctrine()
  109.             ->getRepository(User::class)
  110.             ->findOneBy(array('recover' => $cod));
  111.         if(!$user){
  112.             $this->addFlash('danger''Link inválido!');
  113.             return $this->redirectToRoute('forgot_password');
  114.         }
  115.         return $this->render('security/recover.html.twig', [
  116.             'user' => $user
  117.         ]);
  118.     }
  119.     /**
  120.      * @Route("/forgot-password-update", methods={"POST"}, name="forgot_password_update")
  121.      */
  122.     public function forgot_update(Request $requestUserPasswordEncoderInterface $passwordEncoder)
  123.     {
  124.         $code $request->request->get('code');
  125.         $pass $request->request->get('pass');
  126.         $confirm_pass $request->request->get('confirm_pass');
  127.         $user $this->getDoctrine()
  128.             ->getRepository(User::class)
  129.             ->findOneBy(array('recover' => $code));
  130.         if(!$user){
  131.             $this->addFlash('danger''Usuário não encontrado!');
  132.             return $this->redirectToRoute('forgot_password_validation',['cod' => $code]);
  133.         }
  134.         if($pass!=$confirm_pass){
  135.             $this->addFlash('danger''As senhas não conferem!');
  136.             return $this->redirectToRoute('forgot_password_validation',['cod' => $code]);
  137.         }
  138.         $user->setPassword(
  139.             $passwordEncoder->encodePassword(
  140.                 $user,
  141.                 $pass
  142.             )
  143.         );
  144.         $user->setRecover("");
  145.         $this->getDoctrine()->getManager()->flush();
  146.         $this->addFlash('success''Alteração de senha realizada com sucesso!');
  147.         return $this->redirectToRoute('app_login');
  148.     }
  149.     protected function generat_link($email){
  150.         return sha1($email.time());
  151.     }
  152.     protected function send_link($user)
  153.     {
  154.         $message '<style>
  155.                             @import url(\'https://fonts.googleapis.com/css2?family=Roboto:wght@300&display=swap\');
  156.                         </style>
  157.                         <table style="width: 500px; text-align: center; font-family: \'Roboto\', sans-serif;">
  158.                             <tr>
  159.                                 <td style="height: 100px; background: linear-gradient(188.93deg, #FFFFFF 0%, #E2DFFF 0.01%, #FFFFFF 105.25%);">
  160.                                     <img src="https://admin.bnbguests.com.br/imgs/logo-email.png">
  161.                                 </td>
  162.                             </tr>
  163.                             <tr>
  164.                                 <td style="padding-top: 20px">
  165.                                     <p><b>Solicitação de recuperação de senha</b></p>
  166.                                     <p>Alguém solicitou a recuperação da sua senha bnbguests</p>
  167.                                     <p>Se você não fez essa solicitação, ignore este e-mail. Se você deseja continuar:</p>
  168.                                     <p><a href="'.$this->baseUrl.$this->generateUrl('forgot_password_validation', array('cod'=>$user->getRecover())).'">Click aqui para cadastrar uma nova senha</a></p>
  169.                                     <p>Att,<br>Equipe bnbguests</p>
  170.                                     <p style="color: #cccccc"><i>*Não responda este e-mail*</i></p>
  171.                                 </td>
  172.                             </tr>
  173.                         </table>';
  174.         $this->mailer->sendMail($user->getEmail(),'Recuperação de senha - bnbguests'$message);
  175.     }
  176. }