src/Controller/SecurityController.php line 71

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Repository\UserRepository;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  11. use Symfony\Component\Security\Core\Security;
  12. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  13. class SecurityController extends AbstractController
  14. {
  15.     /**
  16.      * @Route("/", name="login")
  17.      */
  18.     public function login(AuthenticationUtils $authenticationUtils)
  19.     {
  20.         //Check if user connected, redirect to Project
  21.         if ($this->getUser()) {
  22.             return $this->redirectToRoute("files_users_manage_home_admin");
  23.         }
  24.         $error $authenticationUtils->getLastAuthenticationError();
  25.         $lastUsername $authenticationUtils->getLastUsername();
  26.         return $this->render('security/login.html.twig', [
  27.             'current_menu' => 'login',
  28.             'last_username' => $lastUsername,
  29.             'error' => $error
  30.         ]);
  31.     }
  32.     /**
  33.      * @Route("/login_success", name="login_success")
  34.      */
  35.     public function postLoginRedirectAction(Security $security)
  36.     {
  37.         $user $security->getUser();
  38.         if(!$user->getIsActive()){
  39.             //check if userIsActive == true
  40.             $this->addFlash('error','Your account has been disabled ');
  41.             return $this->redirectToRoute("logout");
  42.         } else if ($user->checkIfAllInformationsAreFullfilled()) {
  43.             //On vĂ©rifie que l'utilisateur a rempli toutes ces informations
  44.             // return $this->redirectToRoute("your_tasks");  //////  CHANGED  TO  /////         
  45.             //return $this->redirectToRoute("admin_files_users",[]);
  46.             //list_tasks_user
  47.             return $this->redirectToRoute("files_users_manage_home_admin",[]);
  48.            //return $this->redirectToRoute("files_users_home_admin",[]);
  49.         } else{
  50.             return $this->redirectToRoute("register_infos");
  51.         }
  52.     }
  53.     /**
  54.      * @Route("/logout", name="logout")
  55.      */
  56.     public function logout(AuthenticationUtils $authenticationUtils)
  57.     {
  58.         return $this->render('');
  59.     }
  60.     /**
  61.      * @Route("/reset-password", name="reset_password")
  62.      */
  63.     public function resetPassword(Request $request\Swift_Mailer $mailerUserRepository $userRepositoryUserPasswordEncoderInterface $encoderEntityManagerInterface $entityManager)
  64.     {
  65.         $form $this->createFormBuilder()
  66.             ->add('email'EmailType::class)
  67.             ->getForm();
  68.         $form->handleRequest($request);
  69.         if($form->isSubmitted() && $form->isValid())
  70.         {
  71.             $email $form->get('email')->getData();
  72.             //generate new password
  73.             $newPassword substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyz"5)), 05);
  74.             //we get the user with email selected
  75.             $user $userRepository->findUserWithEmail($email);
  76.             //if no user has this email we redirect to login with an error message
  77.             if($user == null){
  78.                 $this->addFlash('error','Impossible to reset the password. No account with this email. ');
  79.                 return $this->redirectToRoute('login');
  80.             }
  81.             //change password in the database for this user
  82.             $user->setPassword($encoder->encodePassword($user,$newPassword));
  83.             $entityManager->flush();
  84.             //Send mail reset password
  85.             $message = (new \Swift_Message('Your Metask account'))
  86.                 // sender
  87.                 ->setFrom("no-reply@meridec.ch")
  88.                 // recipient
  89.                 ->setTo($email)
  90.                 // body email
  91.                 ->setBody(
  92.                     $this->renderView(
  93.                         'emails/reset_password.html.twig', [
  94.                             'email'=> $email,
  95.                             'password' => $newPassword
  96.                         ]
  97.                     ),
  98.                     'text/html'
  99.                 )
  100.             ;
  101.             $mailer->send($message);
  102.             $this->addFlash('success','Your password has been reset with success ! An email has been sent to '.$email);
  103.             return $this->redirectToRoute('login');
  104.         }
  105.         return $this->render('security/reset_password.html.twig', [
  106.             'form'=>$form->createView()
  107.         ]);
  108.     }
  109. }