src/Controller/SecurityController.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  11. use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;
  12. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  13. class SecurityController extends AbstractController
  14. {
  15.     /**
  16.      * @Route("/login", name="app_login")
  17.      */
  18.     public function login(AuthenticationUtils $authenticationUtils): Response
  19.     {
  20.         // get the login error if there is one
  21.         $error $authenticationUtils->getLastAuthenticationError();
  22.         // last username entered by the user
  23.         $lastUsername $authenticationUtils->getLastUsername();
  24.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  25.     }
  26.     /**
  27.      * @Route("/login-modal", name="app_login_modal",options={"expose"=true})
  28.      */
  29.     public function loginModal(AuthenticationUtils $authenticationUtils): Response
  30.     {
  31.         // get the login error if there is one
  32.         $error $authenticationUtils->getLastAuthenticationError();
  33.         // last username entered by the user
  34.         $lastUsername $authenticationUtils->getLastUsername();
  35.         return $this->render('front/myaccount/login-modal.html.twig', ['last_username' => $lastUsername'error' => $error]);
  36.     }
  37.     /**
  38.      * @Route("/connect/google", name="google_connect")
  39.      */
  40.     public function connectGoole(ClientRegistry $clientRegistry): Response
  41.     {
  42.         $client $clientRegistry->getClient('google');
  43.         return $client->redirect(['profile''email']);
  44.     }
  45.     /**
  46.      * @Route("/connect/microsoft", name="microsoft_connect")
  47.      */
  48.     public function connectMicrosoft(ClientRegistry $clientRegistry): Response
  49.     {
  50.         $client $clientRegistry->getClient('microsoft');
  51.         return $client->redirect(['wl.basic''wl.signin','wl.emails']);
  52.     }
  53.     /**
  54.      * @Route("/connect/apple", name="apple_connect")
  55.      */
  56.     public function connectApple(ClientRegistry $clientRegistry): Response
  57.     {
  58.         $client $clientRegistry->getClient('apple');
  59.         return $client->redirect(['name''email']);
  60.     }
  61.     /**
  62.      * @Route("/logout", name="app_logout", methods={"GET"})
  63.      */
  64.     public function logout()
  65.     {
  66.         // controller can be blank: it will never be executed!
  67.         throw new \Exception('Don\'t forget to activate logout in security.yaml');
  68.     }
  69.     /**
  70.      * @Route("/mot-de-passe-oublie", name="app_forgotten_password")
  71.      */
  72.     public function forgottenPassword(Request $requestUserPasswordEncoderInterface $encoder, \Swift_Mailer $mailerTokenGeneratorInterface $tokenGenerator): Response
  73.     {
  74.         if ($request->isMethod('POST')) {
  75.             $email $request->request->get('email');
  76.             $entityManager $this->getDoctrine()->getManager();
  77.             $user $entityManager->getRepository(User::class)->findOneByEmail($email);
  78.             /* @var $user User */
  79.             if ($user === null) {
  80.                 $this->addFlash('danger''Email Inconnu');
  81.                 return $this->redirectToRoute('app_login');
  82.             }
  83.             $token $tokenGenerator->generateToken();
  84.             try{
  85.                 $user->setResetToken($token);
  86.                 $entityManager->flush();
  87.             } catch (\Exception $e) {
  88.                 $this->addFlash('warning'$e->getMessage());
  89.                 return $this->redirectToRoute('app_login');
  90.             }
  91.             $url $this->generateUrl('app_reset_password', array('token' => $token), UrlGeneratorInterface::ABSOLUTE_URL);
  92.             $html $this->renderView(
  93.                 'emails/forgotten_password.html.twig',
  94.                 [
  95.                     'user' => $user,
  96.                     'url' => $url
  97.                 ]
  98.             );
  99.             $message = (new \Swift_Message('Mot de passe oublié'))
  100.                 ->setFrom([$this->getParameter('admin_email') => 'COPEES'])
  101.                 ->setTo($user->getEmail())
  102.                 ->setBody(
  103.                     $html,
  104.                     'text/html'
  105.                 );
  106.             $mailer->send($message);
  107.             $this->addFlash('notice''Mail envoyé');
  108.             return $this->redirectToRoute('app_login');
  109.         }
  110.         return $this->render('security/forgotten_password.html.twig');
  111.     }
  112.     /**
  113.      * @Route("/changer-mot-de-passe/{token}", name="app_reset_password")
  114.      */
  115.     public function resetPassword(Request $requeststring $tokenUserPasswordEncoderInterface $passwordEncoder)
  116.     {
  117.         if ($request->isMethod('POST')) {
  118.             $entityManager $this->getDoctrine()->getManager();
  119.             $user $entityManager->getRepository(User::class)->findOneByResetToken($token);
  120.             /* @var $user User */
  121.             if ($user === null) {
  122.                 $this->addFlash('danger''Token Inconnu');
  123.                 return $this->redirectToRoute('app_login');
  124.             }
  125.             $user->setResetToken(null);
  126.             $user->setPassword($passwordEncoder->encodePassword($user$request->request->get('password')));
  127.             $entityManager->flush();
  128.             $this->addFlash('notice''Mot de passe mis à jour');
  129.             return $this->redirectToRoute('app_login');
  130.         }else {
  131.             return $this->render('security/reset_password.html.twig', ['token' => $token]);
  132.         }
  133.     }
  134. }