src/Controller/SecurityController.php line 78

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. use App\Entity\Organization;
  13. class SecurityController extends AbstractController
  14. {
  15.     
  16.     
  17.     private $passwordEncoder;
  18.     
  19.     public function __construct(UserPasswordEncoderInterface $passwordEncoder)
  20.     {
  21.         $this->passwordEncoder $passwordEncoder;
  22.     }
  23.         
  24.     /**
  25.      * @Route("/login/{name?main}", name="app_login")
  26.      */
  27.     public function login(AuthenticationUtils $authenticationUtilsRequest $requestEntityManagerInterface $entityManager): Response
  28.     {
  29.         $churchLogo false;
  30.         $churchName $request->get('name');
  31.         if($churchName)
  32.         {            
  33.             $churchFound $entityManager->getRepository(Organization::class)->findChurchByName($churchName);
  34.             if($churchFound)
  35.             {
  36.                 $churchLogo $churchFound['logo_path'];
  37.             }
  38.         }
  39.         
  40.         $logo "https://dev.laiglesiavirtual.com/assets/img/logo.png";
  41.         $register true;
  42.         
  43.         $serverName $_SERVER["SERVER_NAME"];        
  44.         /*if($serverName == 'somosalientodevida.com' || $serverName == 'www.somosalientodevida.com')
  45.         {      
  46.             $logo = "https://dev.laiglesiavirtual.com/public/uploads/logo-single-6227e76d12f57.png";
  47.             $register = false;
  48.         }*/   
  49.         
  50.         // if ($this->getUser()) {
  51.         //     return $this->redirectToRoute('target_path');
  52.         // }
  53.         // get the login error if there is one
  54.         $error $authenticationUtils->getLastAuthenticationError();
  55.         // last username entered by the user
  56.         $lastUsername $authenticationUtils->getLastUsername();
  57.         return $this->render('security/login.html.twig', [
  58.             'last_username' => $lastUsername
  59.             'error'         => $error
  60.             'logo'          => $logo
  61.             'register'      => $register,
  62.             'churchLogo'    => $churchLogo
  63.         ]);
  64.     }
  65.     /**
  66.      * @Route("/recovery", name="app_recovery")
  67.      */
  68.     public function recover(Request $requestHelperService $helperServiceUserRepository $userRepositoryEntityManagerInterface $entityManager): Response
  69.     {
  70.         $logoS "https://dev.laiglesiavirtual.com/assets/img/logo.png";
  71.         
  72.         $serverName $_SERVER["SERVER_NAME"];        
  73.         if($serverName == 'somosalientodevida.com' || $serverName == 'www.somosalientodevida.com')
  74.         {      
  75.             $logoS "https://dev.laiglesiavirtual.com/public/uploads/logo-single-6227e76d12f57.png";
  76.         }          
  77.         
  78.         if($request->get('recovery_email'))
  79.         {
  80.             
  81.                $userCheck $userRepository->findOneBy(['email' => $request->get('recovery_email'), 'status' => 'ACTIVO']);
  82.                if($userCheck)
  83.                {                   
  84.                    
  85.                    
  86.                    $plainpwd substr(md5(microtime()),rand(0,26),8)."*";
  87.                 $encoded  $this->passwordEncoder->encodePassword($userCheck$plainpwd);
  88.                 $userCheck->setPassword($encoded);     
  89.                 $entityManager->persist($userCheck);
  90.                 $entityManager->flush();                                          
  91.                    
  92.                    $organizationName $userCheck->getOrganization()->getName();
  93.                    
  94.                 $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>";
  95.                 $message .= "<p><b>Contraseña temporal:</b> $plainpwd</p>";            
  96.                 $message .= "<p>Una vez dentro de su cuenta, no olvide cambiar su contraseña.</p>";
  97.                             
  98.                 $mailTo  $request->get('recovery_email');
  99.                 $subject "Recuperación de contraseña";
  100.                 
  101.                 $paths $helperService->getPaths();
  102.                 $logo  $paths['uploads_path'].$userCheck->getOrganization()->getLogoPath();
  103.                 
  104.                 if($helperService->sendEmail($mailTo$subject$messagefalse$organizationName$logo))
  105.                 {
  106.                     $this->addFlash('success'"Se ha enviado un email con las instrucciones de recuperación");                        
  107.                 } else {
  108.                     $this->addFlash('error'"No se pudo enviar el email de recuperación. Contacte con soporte técnico.");                        
  109.                 }
  110.                     
  111.             } else {
  112.                 
  113.                 $this->addFlash('error'"El email no se encuentra registrado");                                    
  114.                 
  115.             }
  116.         }
  117.         return $this->render('security/recovery.html.twig', [
  118.             'logo' => $logoS
  119.         ]);
  120.     }
  121.     /**
  122.      * @Route("/logout", name="app_logout")
  123.      */
  124.     public function logout(): void
  125.     {
  126.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  127.     }
  128. }