src/Controller/SecurityController.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\SetPasswordType;
  4. use App\Repository\ClientProductRepository;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  9. use App\Repository\ClientRepository;
  10. use App\Repository\DomainRepository;
  11. use App\Repository\UserRepository;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  16. use Symfony\Component\HttpFoundation\Cookie;
  17. class SecurityController extends AbstractController
  18. {
  19.     /**
  20.      * @Route("/{clientSlug}/{clientSalt}/connexion-securisee", name="app_login")
  21.      */
  22.     public function login(Request $request$clientSlug$clientSaltAuthenticationUtils $authenticationUtilsClientRepository $clientRepositoryDomainRepository $domainRepositoryUserRepository $userRepositoryClientProductRepository $clientProductRepository): Response
  23.     {
  24.         if($request->query->get('fromRegister') == '1'){
  25.             $fromRegister true;
  26.         }else{
  27.             $fromRegister false;
  28.         }
  29.         
  30.         if ($request->isMethod('post')) {
  31.             $userExist $userRepository->findOneByEmail($request->request->get('email'));
  32.             if($userExist == null){
  33.                 return $this->redirectToRoute('app_register', ['clientSlug' => $clientSlug'clientSalt' => $clientSalt], Response::HTTP_SEE_OTHER);
  34.             }
  35.         }
  36.         if ($this->getUser()) {
  37.             return $this->redirectToRoute('app_main', ['clientSlug' => $clientSlug]);
  38.         }
  39.         
  40.         $error $authenticationUtils->getLastAuthenticationError();
  41.         $lastUsername $authenticationUtils->getLastUsername();
  42.         if($error != null){
  43.             $userExist $userRepository->findOneByEmail($lastUsername);
  44.             if($userExist == null){
  45.                 return $this->redirectToRoute('app_register', ['clientSlug' => $clientSlug'clientSalt' => $clientSalt], Response::HTTP_SEE_OTHER);
  46.             }
  47.         }
  48.         $client $clientRepository->findOneBySlug($clientSlug);
  49.         $authorizedDomains $domainRepository->findByClient($client);
  50.         $content $this->renderView('security/login.html.twig', [
  51.             'last_username' => $lastUsername,
  52.             'error' => $error,
  53.             'authorizedDomains' => $authorizedDomains,
  54.             'client' => $client,
  55.             'fromRegister' => $fromRegister
  56.         ]);
  57.         $viewWithCookie $this->setCookie($client->getSlug(), $content);
  58.         return $viewWithCookie;
  59.     }
  60.     /**
  61.      * @Route("/{clientSlug}/{clientSalt}/connexion", name="app_login_unsecure")
  62.      */
  63.     public function loginUnsecure(Request $request$clientSlug$clientSaltAuthenticationUtils $authenticationUtilsClientRepository $clientRepositoryDomainRepository $domainRepositoryUserRepository $userRepositoryClientProductRepository $clientProductRepository): Response
  64.     {
  65.         if($request->query->get('fromRegister') == '1'){
  66.             $fromRegister true;
  67.         }else{
  68.             $fromRegister false;
  69.         }
  70.         
  71.         if ($request->isMethod('post')) {
  72.             $userExist $userRepository->findOneByEmail($request->request->get('email'));
  73.             if($userExist == null){
  74.                 return $this->redirectToRoute('app_register', ['clientSlug' => $clientSlug'clientSalt' => $clientSalt], Response::HTTP_SEE_OTHER);
  75.             }
  76.         }
  77.         if ($this->getUser()) {
  78.             return $this->redirectToRoute('app_main', ['clientSlug' => $clientSlug]);
  79.         }
  80.         
  81.         $error $authenticationUtils->getLastAuthenticationError();
  82.         $lastUsername $authenticationUtils->getLastUsername();
  83.         if($error != null){
  84.             $userExist $userRepository->findOneByEmail($lastUsername);
  85.             if($userExist == null){
  86.                 return $this->redirectToRoute('app_register', ['clientSlug' => $clientSlug'clientSalt' => $clientSalt], Response::HTTP_SEE_OTHER);
  87.             }
  88.         }
  89.         $client $clientRepository->findOneBySlug($clientSlug);
  90.         $authorizedDomains $domainRepository->findByClient($client);
  91.         $content $this->renderView('security/login_unsecure.html.twig', [
  92.             'last_username' => $lastUsername,
  93.             'error' => $error,
  94.             'authorizedDomains' => $authorizedDomains,
  95.             'client' => $client,
  96.             'fromRegister' => $fromRegister
  97.         ]);
  98.         $viewWithCookie $this->setCookie($client->getSlug(), $content);
  99.         return $viewWithCookie;
  100.     }
  101.     /**
  102.      * @Route("/mon-profil/definir-mon-mot-de-passe", name="app_set_password")
  103.      * @Security("is_granted('ROLE_USER')")
  104.      */
  105.     public function setPassword(Request $requestEntityManagerInterface $entityManagerUserPasswordHasherInterface $userPasswordHasher): Response
  106.     {
  107.         $user $this->getUser();
  108.         $form $this->createForm(SetPasswordType::class);
  109.         $form->handleRequest($request);
  110.         if($form->isSubmitted() && $form->isValid()){
  111.             $user->setPassword(
  112.                 $userPasswordHasher->hashPassword(
  113.                     $user,
  114.                     $form->get('plainPassword')->getData()
  115.                 )
  116.                 );
  117.             $entityManager->persist($user);
  118.             $entityManager->flush();
  119.             return $this->redirectToRoute('app_main', [], Response::HTTP_SEE_OTHER);
  120.         }
  121.         return $this->render('security/set_password.html.twig', [
  122.             'form' => $form->createView()
  123.         ]);
  124.     }
  125.     /**
  126.      * @Route("/{clientSlug}/deconnexion", name="app_logout")
  127.      */
  128.     public function logout(): void
  129.     {
  130.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  131.     }
  132.     /**
  133.      * @Route("/login_check/test", name="login_check")
  134.      */
  135.     public function check()
  136.     {
  137.         throw new \LogicException('This code should never be reached');
  138.     }
  139.     public function setCookie($clientSlug$content){
  140.         switch ($_ENV['ACTUAL_ENV']) {
  141.             case 'local':
  142.                 $clientSlugCookie = new Cookie(
  143.                     'clientSlug',
  144.                     $clientSlug,
  145.                     strtotime('+1 year'),
  146.                     '/',
  147.                     'plateform.localhost',
  148.                     true,
  149.                     true
  150.                 );
  151.                 break;
  152.             case 'demo':
  153.                 $clientSlugCookie = new Cookie(
  154.                     'clientSlug',
  155.                     $clientSlug,
  156.                     strtotime('+1 year'),
  157.                     '/',
  158.                     'demo.plateform.jobinlive.fr',
  159.                     true,
  160.                     true
  161.                 );
  162.                 break;
  163.             case 'preprod':
  164.                 $clientSlugCookie = new Cookie(
  165.                     'clientSlug',
  166.                     $clientSlug,
  167.                     strtotime('+1 year'),
  168.                     '/',
  169.                     'preprod.plateform.jobinlive.fr',
  170.                     true,
  171.                     true
  172.                 );
  173.                 break;
  174.             case 'prod':
  175.                 $clientSlugCookie = new Cookie(
  176.                     'clientSlug',
  177.                     $clientSlug,
  178.                     strtotime('+1 year'),
  179.                     '/',
  180.                     'app-ween.fr',
  181.                     true,
  182.                     true
  183.                 );
  184.                 break;
  185.         }
  186.         $response = new Response();
  187.         $response->setContent($content);
  188.         $response->headers->setCookie($clientSlugCookie);
  189.         return $response;
  190.     }
  191. }