src/AppBundle/Controller/SecurityController.php line 20

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use AppBundle\Form\RequestPasswordType;
  6. use AppBundle\Entity\Website;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  9. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Symfony\Component\Mailer\MailerInterface;
  12. use Symfony\Component\Mime\Email;
  13. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  14. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  15. class SecurityController extends AbstractController
  16. {
  17.     public function loginAction(Request $requestAuthenticationUtils $authenticationUtils)
  18.     {
  19.         $template $this->renderProperTemplate($request$authenticationUtils);
  20.         return $this->render($template[0], $template[1]);
  21.     }
  22.     public function loginCheckAction()
  23.     {
  24.     }
  25.     public function authenticateTokenAction(Request $requestEntityManagerInterface $em$authenticationToken$tuto false)
  26.     {
  27.         $website $em->getRepository(Website::class)->findOneByAuthenticationToken($authenticationToken);
  28.         
  29.         if(!$website) {
  30.             throw $this->createNotFoundException("Cet utilisateur n'existe pas.");
  31.         }
  32.         $token = new UsernamePasswordToken($website''"website_secured_area"$website->getRoles());
  33.         $this->get("security.token_storage")->setToken($token);
  34.      
  35.         $website->setAuthenticationToken(null);
  36.         $this->getDoctrine()->getManager()->persist($website);
  37.         $this->getDoctrine()->getManager()->flush();
  38.         //now dispatch the login event
  39.         // $event = new InteractiveLoginEvent($request, $token);
  40.         // $this->get("event_dispatcher")->dispatch("security.interactive_login", $event);
  41.         if($this->getParameter('current_website_by_domain') === true) {
  42.             if($tuto === "tuto") {
  43.                 $url $this->generateUrl('website_home_intro');
  44.             }
  45.             else {
  46.                 $url $this->generateUrl('website_home');
  47.             }
  48.         }
  49.         else {
  50.             if($tuto === "tuto") {
  51.                 $url $this->generateUrl('website_home_intro', ['website' => $website->getSlug()]);
  52.             }
  53.             else { 
  54.                 $url $this->generateUrl('website_home', ['website' => $website->getSlug()]);
  55.             }
  56.         }
  57.         return $this->redirect($url);
  58.     }
  59.     /**
  60.      * Renders proper template
  61.      *
  62.      * @param Request $request
  63.      *
  64.      * @return array
  65.      */
  66.     private function renderProperTemplate(Request $requestAuthenticationUtils $authenticationUtils)
  67.     {
  68.         $session $request->getSession();
  69.         
  70.         $route $request->attributes->get('_route');
  71.         $routeTemplatesArray = ['admin_login'      => 'Admin/login.html.twig''webmaster_login'       => 'Website/login.html.twig'];
  72.         // get the login error if there is one
  73.         $error $authenticationUtils->getLastAuthenticationError();
  74.         // last username entered by the user
  75.         $lastUsername $authenticationUtils->getLastUsername();
  76.         $form null;
  77.         if($route == "webmaster_login") {
  78.             $form $this->createForm(RequestPasswordType::class);
  79.             $form $form->createView();
  80.         }
  81.         return [$routeTemplatesArray[$route], ['error' => $error'last_username' => $lastUsername'form' => $form]];
  82.     }
  83.     public function requestNewPasswordAction(Request $requestUserPasswordHasherInterface $encoderMailerInterface $mailer)
  84.     {
  85.         $error null;
  86.         $form $this->createForm(RequestPasswordType::class);
  87.         $form->handleRequest($request);
  88.         if($form->isSubmitted() && $form->isValid()) {
  89.             $email $form->get('email')->getData();
  90.             $websiteUser $this->getDoctrine()->getRepository(Website::class)->findOneByEmail($email);
  91.             if(!$websiteUser) {
  92.                 $error "Aucun utilisateur n'existe avec cet email.";
  93.             }
  94.             else {
  95.                 // generating new password and encoding it
  96.                 $newPassword substr(base_convert(uniqid('pass'true), 1036), 05);
  97.                 $encoded $encoder->hashPassword($websiteUser$newPassword);
  98.                 $websiteUser->setPassword($encoded);
  99.                 $em $this->getDoctrine()->getManager();
  100.                 $em->persist($websiteUser);
  101.                 $em->flush();
  102.                 $this->sendNewPasswordEmail($websiteUser$newPassword$mailer);
  103.                 $this->addFlash('success''Votre mot de passe a bien été mis à jour, il vous a été envoyé par email');
  104.                 return $this->redirectToRoute('homepage');
  105.             }
  106.         }
  107.         return $this->render('Security/requestNewPassword.html.twig', [
  108.             'form' => $form->createView(),
  109.             'error' => $error
  110.         ]);
  111.     }
  112.     private function sendNewPasswordEmail(Website $website$newPasswordMailerInterface $mailer)
  113.     {
  114.         $mailFrom $this->getParameter('mail_from');
  115.         $email = (new Email())
  116.             ->from($mailFrom)
  117.             ->to($website->getEmail())
  118.             ->subject('FFS - Les informations de connexion à l’administration de votre site')
  119.             ->html($this->renderView('Mail/requestPassword.html.twig', ['newPassword' => $newPassword'website' => $website]));
  120.         $mailer->send($email);
  121.     }
  122.     public function switchUserAction($userId)
  123.     {
  124.         $this->get('session')->set('userid_to_switch'$userId);
  125.         
  126.         return $this->redirectToRoute('user_switch');
  127.     }
  128.     public function switchAction(Request $request)
  129.     {
  130.         if ($this->get('session')->get('userid_to_switch'))
  131.         {
  132.             $user $this->getDoctrine()->getEntityManager()->find(Website::class, $this->get('session')->get('userid_to_switch'));
  133.             if ($user)
  134.             {
  135.                 $token = new UsernamePasswordToken($user'''website_secured_area'$user->getRoles());
  136.                 $this->get('security.token_storage')->setToken($token);
  137.                 $event = new InteractiveLoginEvent($request$token);
  138.                 $this->get('event_dispatcher')->dispatch('security.interactive_login'$event);
  139.                 if($this->getParameter('current_website_by_domain') === true) {
  140.                     $url $this->generateUrl('website_home');
  141.                 }
  142.                 else {
  143.                     $url $this->generateUrl('website_home', ['website' => $user->getSlug()]);
  144.                 }
  145.                 return $this->redirect($url);
  146.             }
  147.         }
  148.         return $this->redirectToRoute('sonata_admin_dashboard');
  149.     }
  150.     public function exitSwitchAction()
  151.     {
  152.         $this->get('session')->set('userid_to_switch'null);
  153.         $this->get('security.token_storage')->setToken(null);
  154.         return $this->redirectToRoute('sonata_admin_dashboard');
  155.     }
  156. }