src/Controller/ProfileController.php line 69

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use App\Repository\UserRepository;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use App\Form\RegistrationFormType;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. //use Symfony\Component\Security\Core\Security; ?????
  12. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  13. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  14. class ProfileController extends AbstractController
  15. {
  16.     #[Route('/profile'name'app_profile')]
  17.     public function index(): Response
  18.     {
  19.        if (!$this->getUser()) {return $this->redirectToRoute('app_login');} //check connexion
  20.         
  21.         return $this->render('profile/index.html.twig', [
  22.             'user' => $this->getUser(),
  23.         ]);
  24.     }
  25. /**
  26.      * @Route("/profile/edit-profile", name="app_edit_profile")
  27.      */
  28.     public function editProfile(Request $requestEntityManagerInterface $entityManager)
  29.     {
  30.         if (!$this->getUser()) {return $this->redirectToRoute('app_login');} //check connexion
  31.         //?????if (!$this->getUser()) {return $this->redirectToRoute('app_login');} //check connexion
  32. $user=$this->getUser();
  33.        
  34.         $form=$this->createForm(RegistrationFormType::class,$user);
  35.         $form->remove('plainPassword');
  36.         //$form->remove('rolesList');
  37.         $form->remove('isActive');
  38.        // $form->remove('employee_bareme');
  39.         $form->handleRequest($request);
  40.         if($form->isSubmitted() && $form->isValid())
  41.         {
  42.             $entityManager->persist($user);
  43.             $entityManager->flush();
  44.             $this->addFlash('success','Your profile has been modified with success');
  45.             return $this->redirectToRoute('app_profile');
  46.         }
  47.         return $this->render('profile/edit_profile.html.twig', [
  48.             'form' => $form->createView(),
  49.             'user' => $user,
  50.         ]);
  51.     }
  52. /**
  53.      * @Route("/profile/change-password", name="app_change_password")
  54.      */
  55.     public function changePassword(Request $requestEntityManagerInterface $entityManager
  56.         UserPasswordHasherInterface $userPasswordHasher)
  57.     {
  58.         //check connected
  59.         if (!$this->getUser()){
  60.             return $this->redirectToRoute('app_login');
  61.         }
  62.         
  63.         $user=$this->getUser();
  64.         $form $this->createFormBuilder()
  65.             ->add('oldPassword'PasswordType::class,['label'=>'Old password'])
  66.             ->add('newPassword'PasswordType::class,['label'=>'New password'])
  67.             ->getForm();
  68.         $form->handleRequest($request);
  69.         if($form->isSubmitted() && $form->isValid())
  70.         {
  71.             $oldPassword $form["oldPassword"]->getData();
  72.             $newPassword $form["newPassword"]->getData();
  73.             $newPasswordEncrypted=$userPasswordHasher->hashPassword($user,$newPassword);
  74.             if (!$userPasswordHasher->isPasswordValid($user,$oldPassword))
  75.             {
  76.                  $this->addFlash('error','Wrong password');
  77.                  return $this->redirectToRoute('app_login',[]);
  78.             } else {
  79.                 $user->setPassword($newPasswordEncrypted);
  80.                 $entityManager->flush();
  81.                 $this->addFlash('success','Your password has been modified with success');
  82.                 return $this->redirectToRoute('app_profile');
  83.             }
  84.         }      
  85.             //dd($newPasswordEncrypted);
  86.             return $this->render('profile/change_password.html.twig', [
  87.                 'form' => $form->createView()
  88.         ]);
  89.     }
  90. }