src/Controller/SecurityController.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Service\UserService;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  10. class SecurityController extends AbstractController
  11. {
  12.     /**
  13.      * @Route("/", name="app_login")
  14.      * @param AuthenticationUtils $authenticationUtils
  15.      * @return Response
  16.      */
  17.     public function login(AuthenticationUtils $authenticationUtils): Response
  18.     {
  19.         // get the login error if there is one
  20.         $error $authenticationUtils->getLastAuthenticationError();
  21.         // last username entered by the user
  22.         $lastUsername $authenticationUtils->getLastUsername();
  23.         $securityContext $this->container->get('security.authorization_checker');
  24.         if ($securityContext->isGranted('IS_AUTHENTICATED_FULLY')) {
  25.             return $this->redirectToRoute('app_homepage');
  26.         }
  27.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  28.     }
  29.     /**
  30.      * @Route("/logout", name="app_logout", methods={"GET"})
  31.      */
  32.     public function logout()
  33.     {
  34.         // controller can be blank: it will never be executed!
  35.         throw new \Exception('Don\'t forget to activate logout in security.yaml');
  36.     }
  37.     /**
  38.      * @Route("/forgottenPassword", name="forgotten_pass")
  39.      * @param AuthenticationUtils $authenticationUtils
  40.      * @return Response
  41.      */
  42.     public function forgottenPassword(AuthenticationUtils $authenticationUtils): Response
  43.     {
  44.         // last username entered by the user
  45.         $lastUsername $authenticationUtils->getLastUsername();
  46.         return $this->render('security/password.html.twig', ['last_username' => $lastUsername]);
  47.     }
  48.     /**
  49.      * @Route("/newPassword", name="new_pass")
  50.      * @param UserServiceController $userService
  51.      * @param AuthenticationUtils $authenticationUtils
  52.      * @return Response
  53.      */
  54.     public function makeNewPassword(UserService $userServiceAuthenticationUtils $authenticationUtils){
  55.         $lastUsername $authenticationUtils->getLastUsername();
  56.         // On check si l'utilisateur existe en base, sinon on retourne un flash message
  57.         $pseudo $_POST['pseudo'];
  58.         $pass $_POST['password'];
  59.         $pass password_hash($passPASSWORD_DEFAULT);
  60.         $existsUser $this->getDoctrine()->getRepository(User::class)->getUserByPseudo($pseudo);
  61.         if(count($existsUser) === 0){
  62.             $this->addFlash('error''Pseudonyme inconnu, veuillez ré-essayer !');
  63.         }else{
  64.             // Génération du nouveau mot de passe
  65.             $userService->updatePasswordByPseudo($pseudo$pass);
  66.             $this->addFlash('success''Votre mot de passe a bien été modifié !');
  67.         }
  68.         return $this->render('security/password.html.twig', ['last_username' => $lastUsername]);
  69.     }
  70.     /**
  71.      * @Route("/changePasswordFirstConnexion", name="change_pass_first_connexion")
  72.      * @param UserServiceController $userService
  73.      * @return RedirectResponse
  74.      */
  75.     public function changePasswordFirstConnexion(UserService $userService){
  76.         $pass $_POST['password'];
  77.         $pass password_hash($passPASSWORD_DEFAULT);
  78.         $userId $this->getUser()->getId();
  79.         $userService->updatePasswordActiveById($userId,$pass);
  80.         $this->addFlash('success''Vos informations ont été mises à jour, veuillez vous ré-identifier !');
  81.         return $this->redirectToRoute('app_login');
  82.     }
  83.     /**
  84.      * @Route("/makeNewPassAjax", name="make_new_pass_ajax")
  85.      */
  86.     public function makeNewPassAjax(){
  87.         $userId $_POST['userId'];
  88.         $password $_POST['password'];
  89.         $user $this->getDoctrine()->getRepository(User::class)->find($userId);
  90.         $user->setPassword(password_hash($passwordPASSWORD_BCRYPT));
  91.         $em $this->getDoctrine()->getManager();
  92.         $em->persist($user);
  93.         $em->flush();
  94.         return $this->redirect('user_gestion');
  95.     }
  96.     /**
  97.      * @Route("/setPassDefault", name="set_pass_default")
  98.      */
  99.     public function setPassDefault(){
  100.         // Fonction de mise à jour des mots de passe à "ABIOLAB" si jamais connectés
  101.         $aUsers $this->getDoctrine()->getRepository(User::class)->findBy(['active' => 0]);
  102.         foreach ($aUsers as $iValue) {
  103.             $user $iValue;
  104.             $em $this->getDoctrine()->getManager();
  105.             $user->setPassword(password_hash('abiolab'PASSWORD_BCRYPT));
  106.             $em->persist($user);
  107.             $em->flush();
  108.         }
  109.         return $this->redirectToRoute('user_gestion');
  110.     }
  111. }