src/Controller/SecurityController.php line 74

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 Symfony\Component\EventDispatcher\EventDispatcherInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  11. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. use App\Entity\User;
  15. use App\Form\UserType;
  16. use App\Helper\EmailHelper;
  17. class SecurityController extends AbstractController
  18. {
  19.     /**
  20.     * @Route({
  21.      *     "en": "/signin",
  22.      *     "es": "/ingresar",
  23.      *     "de": "/signin_de",
  24.      *     "fr": "/signin_fr",
  25.      *     "it": "/signin_it",
  26.      *     "pt": "/signin_pt",
  27.      *     "cn": "/signin_cn",
  28.      *     "cn2": "/signin_cn2",
  29.      * }, name="app_login")
  30.      */    
  31.     public function login(AuthenticationUtils $authenticationUtils): Response
  32.     {
  33.         // if ($this->getUser()) {
  34.         //     return $this->redirectToRoute('target_path');
  35.         // }
  36.         // get the login error if there is one
  37.         $error $authenticationUtils->getLastAuthenticationError();
  38.         // last username entered by the user
  39.         $lastUsername $authenticationUtils->getLastUsername();
  40.         return $this->render('jelly/security/login.html.twig', [
  41.             'last_username' => $lastUsername
  42.             'error' => $error
  43.         ]);
  44.     }
  45.     /**
  46.      * @Route("/logout", name="logout", methods={"GET"})
  47.      */
  48.     public function logout()
  49.     {
  50.         // controller can be blank: it will never be executed!
  51.         throw new \Exception('Don\'t forget to activate logout in security.yaml');
  52.     }
  53.     /**
  54.     * @Route({
  55.      *     "en": "/signup",
  56.      *     "es": "/registro",
  57.      *     "de": "/signup_de",
  58.      *     "fr": "/signup_fr",
  59.      *     "it": "/signup_it",
  60.      *     "pt": "/signup_pt",
  61.      *     "cn": "/signup_cn",
  62.      *     "cn2": "/signup_cn2",
  63.      * }, name="signup")
  64.      */       
  65.     public function signup(Request $requestUserPasswordEncoderInterface $passwordEncoderEventDispatcherInterface $eventDispatcherEmailHelper $emailHelperTranslatorInterface $translator): Response
  66.     {  
  67.           
  68.         $form $this->createForm(UserType::class, new User(), ['validation_groups' => ['Registration']] );
  69.         $form->handleRequest($request);
  70.         if ($form->isSubmitted() && $form->isValid()) {
  71.             $user $form->getData();
  72.                 
  73.             $user->setUsername($user->getEmail());
  74.             $user->setPassword($passwordEncoder->encodePassword(
  75.                          $user,
  76.                          $user->getPlainPassword()
  77.                      ));        
  78.             $user->eraseCredentials();
  79.             $em $this->getDoctrine()->getManager();
  80.             $em->persist($user);
  81.             $em->flush();
  82.             // Here, "public" is the name of the firewall in your security.yml
  83.             $token = new UsernamePasswordToken($user$user->getPassword(), "public"$user->getRoles());
  84.             // For older versions of Symfony, use security.context here
  85.             $this->get("security.token_storage")->setToken($token);
  86.             // Fire the login event
  87.             // Logging the user in above the way we do it doesn't do this automatically
  88.             $event = new InteractiveLoginEvent($request$token);
  89.             $eventDispatcher->dispatch($event"security.interactive_login");
  90.             $emailHelper->sendMail$user->getEmail(), $translator->trans("¡Bienvenido a beconnected!"), "email/registration.html.twig", ['firstName' => $user->getFirstName(), "user_locale" => $request->getLocale() ]  );
  91.             return $this->redirectToRoute('home', ['locale' => $request->getLocale()]);
  92.         }
  93.         return $this->render('jelly/security/signup.html.twig', ['form' => $form->createView()]);
  94.     }
  95.     /**
  96.     * @Route({
  97.      *     "en": "/profile",
  98.      *     "es": "/perfil",
  99.      *     "de": "/profil",
  100.      *     "fr": "/profil_fr",
  101.      *     "it": "/profilo",
  102.      *     "pt": "/perfil_pt",
  103.      *     "cn": "/profile_cn",
  104.      *     "cn2": "/profile_cn2",
  105.      * }, name="profile")
  106.      * @IsGranted("ROLE_USER")
  107.      */
  108.     public function profile(Request $requestUserPasswordEncoderInterface $passwordEncoderEventDispatcherInterface $eventDispatcherEmailHelper $emailHelper): Response
  109.     {  
  110.           
  111.         $form $this->createForm(UserType::class, $this->getUser(), ['validation_groups' => ['Profile']] );
  112.         $form->handleRequest($request);
  113.         if ($form->isSubmitted() && $form->isValid()) {
  114.             $user $form->getData();
  115.                 
  116.             if($user->getPlainPassword()){
  117.                 $user->setPassword($passwordEncoder->encodePassword(
  118.                              $user,
  119.                              $user->getPlainPassword()
  120.                          ));        
  121.                 $user->eraseCredentials();
  122.             }
  123.             $user->setIsAuthenticationless(false);
  124.             
  125.             $em $this->getDoctrine()->getManager();
  126.             $em->persist($user);
  127.             $em->flush();
  128.             return $this->redirectToRoute('home');
  129.         }
  130.         return $this->render('jelly/security/profile.html.twig', ['form' => $form->createView()]);
  131.     }
  132.     /**
  133.      * @Route("/admin/login", name="admin_app_login")
  134.      */
  135.     public function adminLogin(AuthenticationUtils $authenticationUtils): Response
  136.     {
  137.         // get the login error if there is one
  138.         $error $authenticationUtils->getLastAuthenticationError();
  139.         // last username entered by the user
  140.         $lastUsername $authenticationUtils->getLastUsername();
  141.         return $this->render('angle/security/login.html.twig', [
  142.             'last_username' => $lastUsername
  143.             'error' => $error
  144.         ]);
  145.     }
  146. }