src/Controller/SecurityController.php line 93

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Tools\UserHelper;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  9. use Symfony\Component\Mailer\MailerInterface;
  10. use Symfony\Component\Mime\Email;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  13. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. class SecurityController extends AbstractController
  16. {
  17.     /**
  18.      * @param AuthenticationUtils $authenticationUtils
  19.      * @param bool $snippet
  20.      * @return Response
  21.      * @Route("/login", name="app_login")
  22.      */
  23.     public function login(AuthenticationUtils $authenticationUtils$snippet false): Response
  24.     {
  25.         // get the login error if there is one
  26.         $error $authenticationUtils->getLastAuthenticationError();
  27.         // last username entered by the user
  28.         $lastUsername $authenticationUtils->getLastUsername();
  29.         if ($this->getUser()) {
  30.             if (!empty($_GET['referer'])){
  31.                 return $this->redirect($_GET['referer']);
  32.             }else{
  33.                 return $this->redirectToRoute('app_home');
  34.             }
  35.         }
  36.         if ($snippet) {
  37.             return $this->render(
  38.                 'snippets/login-form.html.twig',
  39.                 [
  40.                     'last_username' => $lastUsername,
  41.                     'error' => $error,
  42.                 ]
  43.             );
  44.         } else {
  45.             return $this->render(
  46.                 'security/login.html.twig',
  47.                 [
  48.                     'last_username' => $lastUsername,
  49.                     'error' => $error,
  50.                 ]
  51.             );
  52.         }
  53.     }
  54.     public function adminLogin(AuthenticationUtils $authenticationUtils): Response
  55.     {
  56.         // get the login error if there is one
  57.         $error $authenticationUtils->getLastAuthenticationError();
  58.         // last username entered by the user
  59.         $lastUsername $authenticationUtils->getLastUsername();
  60.         if ($this->getUser()) {
  61.             //dump($this->getUser());die;
  62.             return $this->redirectToRoute('admin_dashboard');
  63.         }
  64.         //dump($error,$this->getUser());die;
  65.         return $this->render(
  66.             'admin/security/login.html.twig',
  67.             [
  68.                 'last_username' => $lastUsername,
  69.                 'error' => $error,
  70.             ]
  71.         );
  72.     }
  73.     /**
  74.      * @param Request $request
  75.      * @param TranslatorInterface $translator
  76.      * @param UserHelper $userHelper
  77.      * @param MailerInterface $mailer
  78.      * @param UserPasswordEncoderInterface $passwordEncoder
  79.      * @return Response
  80.      * @Route("/forgot-password", name="app_forgot_password")
  81.      */
  82.     public function forgotPassword(Request $request,TranslatorInterface $translatorUserHelper $userHelperMailerInterface $mailer,UserPasswordEncoderInterface $passwordEncoder):Response{
  83.         if ($this->getUser()){
  84.             return $this->redirectToRoute('app_home');
  85.         }
  86.         $msg '';
  87.         $status false;
  88.         if (!is_null($request->get('btPassword'))){
  89.             $entityManager $this->getDoctrine()->getManager();
  90.             /** @var User $user */
  91.             $user $entityManager->getRepository(User::class)->findOneBy(['email'=>$request->get("email")]);
  92.             if (!empty($user)){
  93.                 $passwordGenerated $userHelper->randomPassword();
  94.                 $user->setPassword(
  95.                     $passwordEncoder->encodePassword(
  96.                         $user,
  97.                         $passwordGenerated
  98.                     )
  99.                 );
  100.                 $firsName = empty($user->getSalutation()) ? $user->getFirstName() : $user->getSalutation();
  101.                 $html_email $this->renderView('includes/forgot-password-mail.html.twig',[
  102.                     'firstName'=>$firsName,
  103.                     'lastName'=>$user->getLastName(),
  104.                     'email'=>$user->getEmail(),
  105.                     'password'=>$passwordGenerated
  106.                 ]);
  107.                 $email = (new Email())
  108.                     ->from($this->getParameter('mail_sender_address'))
  109.                     ->to($user->getEmail())
  110.                 ;
  111.                 $email->subject($this->getParameter('site_title') ." - ".$translator->trans("Passwort vergessen"))
  112.                         ->html($html_email);
  113.                 $msg '';
  114.                 try {
  115.                     $mailer->send($email);
  116.                     $status true;
  117.                     $msg $translator->trans("The password has been sent to your email address. Please check it.");
  118.                     $entityManager->flush();
  119.                 } catch (TransportExceptionInterface $e) {
  120.                     $msg $e->getMessage();
  121.                 }
  122.             }else{
  123.                 $status false;
  124.                 $msg $translator->trans("The user doesn't exists.");
  125.             }
  126.         }
  127.         return $this->render('security/forgot-password.html.twig',[
  128.             'status'=>$status,
  129.             'msg'=>$msg
  130.         ]);
  131.     }
  132. }