src/Controller/SecurityController.php line 60

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  8. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  9. use App\Service\HelperService;
  10. use App\Repository\UserRepository;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. class SecurityController extends AbstractController
  13. {
  14.     
  15.     
  16.     private $passwordEncoder;
  17.     
  18.     public function __construct(UserPasswordEncoderInterface $passwordEncoder)
  19.     {
  20.         $this->passwordEncoder $passwordEncoder;
  21.     }
  22.         
  23.     /**
  24.      * @Route("/login", name="app_login")
  25.      */
  26.     public function login(AuthenticationUtils $authenticationUtils): Response
  27.     {
  28.         
  29.         $logo "https://dev.laiglesiavirtual.com/assets/img/logo.png";
  30.         $register true;
  31.         
  32.         $serverName $_SERVER["SERVER_NAME"];        
  33.         if($serverName == 'somosalientodevida.com' || $serverName == 'www.somosalientodevida.com')
  34.         {      
  35.             $logo "https://dev.laiglesiavirtual.com/public/uploads/logo-single-6227e76d12f57.png";
  36.             $register false;
  37.         }          
  38.         
  39.         // if ($this->getUser()) {
  40.         //     return $this->redirectToRoute('target_path');
  41.         // }
  42.         // get the login error if there is one
  43.         $error $authenticationUtils->getLastAuthenticationError();
  44.         // last username entered by the user
  45.         $lastUsername $authenticationUtils->getLastUsername();
  46.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error'logo' => $logo'register' => $register]);
  47.     }
  48.     /**
  49.      * @Route("/recovery", name="app_recovery")
  50.      */
  51.     public function recover(Request $requestHelperService $helperServiceUserRepository $userRepositoryEntityManagerInterface $entityManager): Response
  52.     {
  53.         $logo "https://dev.laiglesiavirtual.com/assets/img/logo.png";
  54.         
  55.         $serverName $_SERVER["SERVER_NAME"];        
  56.         if($serverName == 'somosalientodevida.com' || $serverName == 'www.somosalientodevida.com')
  57.         {      
  58.             $logo "https://dev.laiglesiavirtual.com/public/uploads/logo-single-6227e76d12f57.png";
  59.         }          
  60.         
  61.         if($request->get('recovery_email'))
  62.         {
  63.             
  64.                $userCheck $userRepository->findOneBy(['email' => $request->get('recovery_email'), 'status' => 'ACTIVO']);
  65.                if($userCheck)
  66.                {                   
  67.                    
  68.                    
  69.                    $plainpwd substr(md5(microtime()),rand(0,26),8)."*";
  70.                 $encoded  $this->passwordEncoder->encodePassword($userCheck$plainpwd);
  71.                 $userCheck->setPassword($encoded);     
  72.                 $entityManager->persist($userCheck);
  73.                 $entityManager->flush();                                          
  74.                    
  75.                    $organizationName $userCheck->getOrganization()->getName();
  76.                    
  77.                 $message "<p>Ha solicitado recuperar la contraseña para su cuenta en la Plataforma de $organizationName. Puede utilizar el siguiente password para iniciar sesión.</p>";
  78.                 $message .= "<p><b>Contraseña temporal:</b> $plainpwd</p>";            
  79.                 $message .= "<p>Una vez dentro de su cuenta, no olvide cambiar su contraseña.</p>";
  80.                             
  81.                 $mailTo  $request->get('recovery_email');
  82.                 $subject "Recuperación de contraseña";
  83.                 
  84.                 $paths $helperService->getPaths();
  85.                 $logo  $paths['uploads_path'].$userCheck->getOrganization()->getLogoPath();
  86.                 
  87.                 if($helperService->sendEmail($mailTo$subject$messagefalse$organizationName$logo))
  88.                 {
  89.                     $this->addFlash('success'"Se ha enviado un email con las instrucciones de recuperación");                        
  90.                 } else {
  91.                     $this->addFlash('error'"No se pudo enviar el email de recuperación. Contacte con soporte técnico.");                        
  92.                 }
  93.                     
  94.             } else {
  95.                 
  96.                 $this->addFlash('error'"El email no se encuentra registrado");                                    
  97.                 
  98.             }
  99.         }
  100.         return $this->render('security/recovery.html.twig', [
  101.             'logo' => $logo
  102.         ]);
  103.     }
  104.     /**
  105.      * @Route("/logout", name="app_logout")
  106.      */
  107.     public function logout(): void
  108.     {
  109.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  110.     }
  111. }