src/Controller/AccountController.php line 116

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Admin;
  4. use App\Form\AdminType;
  5. use App\Entity\SetPassword;
  6. use App\Entity\ResetPassword;
  7. use App\Form\SetPasswordType;
  8. use App\Entity\PasswordUpdate;
  9. use App\Form\ResetPasswordType;
  10. use App\Form\PasswordUpdateType;
  11. use App\EmailNotification\ToUser;
  12. use App\Repository\UserRepository;
  13. use Symfony\Component\Form\FormError;
  14. use Doctrine\Persistence\ObjectManager;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  19. use Symfony\Component\HttpFoundation\JsonResponse;
  20. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  21. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  22. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  23. class AccountController extends AbstractController
  24. {
  25.     private function str_rand(int $length 64)
  26.     {
  27.         $length = ($length 4) ? $length;
  28.         return bin2hex(random_bytes(($length - ($length 2)) / 2));
  29.     }
  30.     
  31.     #[Route(path'/connexion'name'app_login')]
  32.     public function login(AuthenticationUtils $utils): Response
  33.     {
  34.         $user $this->getUser();
  35.         if ($user != null) {
  36.             return $this->redirectToRoute("homepage");
  37.         }
  38.         else 
  39.         {
  40.             $error $utils->getLastAuthenticationError();
  41.             $username $utils->getLastUsername();
  42.             return $this->render('/account/login.html.twig',[
  43.                 'error' => $error,
  44.                 'username' => $username,
  45.             ]);
  46.         }
  47.     }
  48.     #[Route(path'/verification-compte'name'account_check')]
  49.     public function accountCheck(AuthenticationUtils $utilsTokenStorageInterface $tokenStorage)
  50.     {
  51.         $admin $this->getUser();
  52.         if ($admin->getStatus() == 'deleted') {
  53.             $tokenStorage->setToken();
  54.             return $this->redirectToRoute('homepage');
  55.         }
  56.         if ($admin->getHasAlreadyLoggedIn()) {
  57.             
  58.             return $this->redirectToRoute('homepage');
  59.         }
  60.         else {
  61.             return $this->redirectToRoute('set_password');
  62.         }
  63.     }
  64.     
  65.     #[Route(path'/securite/configurer-mot-de-passe'name'set_password')]
  66.     public function setPassword(Request $requestUserPasswordEncoderInterface $encoderObjectManager $manager)
  67.     {
  68.         $admin $this->getUser();
  69.         $password = new SetPassword();
  70.         $form $this->createForm(SetPasswordType::class, $password);
  71.         $form->handleRequest($request);
  72.         if ($form->isSubmitted() && $form->isValid()) {
  73.             $newPass $password->getNewPassword();
  74.             $hash $encoder->encodePassword($admin$newPass);
  75.             $admin->setPassword($hash)
  76.                  ->setHasAlreadyLoggedIn(true)
  77.                  ->setStatus('active');
  78.             $manager->persist($admin);
  79.             $manager->flush();
  80.             $this->addFlash(
  81.                 'success',
  82.                 'Votre mot de passe a bien été sauvegardé'
  83.             );
  84.             
  85.             return $this->redirectToRoute('homepage');
  86.         }
  87.         return $this->render('admin/set-password.html.twig', [
  88.             'form' => $form->createView()
  89.         ]);
  90.     }
  91.     #[Route(path'/mot-de-passe-oublie'name'forgot_password')]
  92.     public function forgotPassword(UserRepository $userRepositoryToUser $toUser)
  93.     {
  94.         if (isset($_POST['forgot_submit'])) {
  95.             $email $_POST['forgot_email'];
  96.             $user $userRepository->findOneBy(['email' => $email]);
  97.             if ($user) {
  98.                 // $toUser->forgotPassword($user);
  99.             }
  100.             return $this->redirectToRoute('forgot_password');
  101.         }
  102.         
  103.         return $this->render('account/forgot-password.html.twig');
  104.     }
  105.     #[Route(path'/reset-password/{token}'name'reset_password')]
  106.     public function resetPassword($tokenUserRepository $userRepositoryUserPasswordEncoderInterface $encoderRequest $requestObjectManager $manager)
  107.     {
  108.         $user $userRepository->findOneBy(['token' => $token]);
  109.         $isAdmin false;
  110.         if ($user instanceof Admin)
  111.         {
  112.             $isAdmin true;
  113.         }
  114.         if ($user) {
  115.             
  116.             $action 'reset';
  117.             $newPassword = new ResetPassword();
  118.             
  119.             $form $this->createForm(ResetPasswordType::class, $newPassword);
  120.     
  121.             $form->handleRequest($request);
  122.     
  123.             if ($form->isSubmitted() && $form->isValid()) {
  124.                 $hashed $encoder->encodePassword($user$newPassword->getPassword());
  125.                 $user->setPassword($hashed)
  126.                     ->setToken($this->str_rand(32));
  127.                 $manager->persist($user);
  128.                 $manager->flush();
  129.                 
  130.                 if($isAdmin)
  131.                 {
  132.                     return $this->redirectToRoute('app_login');
  133.                 }
  134.                 else
  135.                 {
  136.                     return $this->redirectToRoute('app_login');
  137.                 }
  138.             }
  139.             return $this->render('account/reset-password.html.twig', [
  140.                 'action' => $action,
  141.                 'form' => $form->createView()
  142.             ]);
  143.         } 
  144.         else {
  145.             $action 'expired';
  146.             return $this->render('account/reset-password.html.twig', [
  147.                 'action' => $action
  148.             ]);
  149.         }
  150.     }
  151.     #[Route(path'/deconnexion'name'app_logout')]
  152.     public function logout() {}
  153.     #[Route(path'/modifier-mon-profil'name'edit_profile')]
  154.     public function editProfile(Request $requestObjectManager $managerUserPasswordEncoderInterface $encoder)
  155.     {
  156.         $admin $this->getUser();
  157.         $passUpdate = new PasswordUpdate();
  158.         $formPassword $this->createForm(PasswordUpdateType::class, $passUpdate);
  159.         $formProfile $this->createForm(AdminType::class, $admin, ['role' => null'teamInfos' => $this->getUser()->getTeam()->getTeamInfos()]);
  160.         $formPassword->handleRequest($request);
  161.         $formProfile->handleRequest($request);
  162.         if ($formPassword->isSubmitted() && $formPassword->isValid()) {
  163.             if (!password_verify($passUpdate->getOldPassword(), $admin->getPassword())) {
  164.                 $formPassword->get('oldPassword')->addError(new FormError("Ce n'est pas votre de passe actuel !"));
  165.             }
  166.             else {
  167.                 $newPass $passUpdate->getNewPassword();
  168.                 $hash $encoder->encodePassword($admin$newPass);
  169.                 $admin->setPassword($hash);
  170.                 $manager->persist($admin);
  171.                 $manager->flush();
  172.                 $this->addFlash(
  173.                     'success',
  174.                     'Mot de passe mofiié avec succès'
  175.                 );
  176.                 return $this->redirectToRoute('see_profile');
  177.             }
  178.         }
  179.         if ($formProfile->isSubmitted() && $formProfile->isValid()) {
  180.             $manager->persist($admin);
  181.             $manager->flush();
  182.             $this->addFlash(
  183.                 'success',
  184.                 'Votre profil a été modifié'
  185.             );
  186.             return $this->redirectToRoute('see_profile');
  187.         }
  188.         return $this->render('account/edit-profile.html.twig', [
  189.             'formProfile' => $formProfile->createView(),
  190.             'formPassword' => $formPassword->createView()
  191.         ]);
  192.     }
  193.     #[Route(path'/mon-profil'name'see_profile')]
  194.     public function seeProfile()
  195.     {
  196.         $admin $this->getUser();
  197.         return $this->render('account/see-profile.html.twig', [
  198.             'admin' => $admin,
  199.             'menu' => 'profile'
  200.         ]);
  201.     }
  202.     #[Route(path'/login'name'login_check'methods: ['POST'])]
  203.     public function loginCheck()
  204.     {
  205.         //NOT DELETE THIS ONE
  206.     }
  207. }