SHARE
TWEET

ResetPassword

a guest Feb 14th, 2020 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace App\Controller\admin;
  4.  
  5. use App\Entity\User;
  6. use App\Form\ProfileType;
  7. use App\Form\ResetPasswordType;
  8. use App\Repository\ProfileRepository;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Security\Core\User\UserInterface;
  14. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  15. use Symfony\Component\Form\FormError;
  16. use Symfony\Component\Security\Core\Security;
  17.  
  18. /**
  19.  * @Route("/admin/profile")
  20.  */
  21. class ProfileController extends AbstractController
  22. {
  23.  
  24.     private $passwordEncoder;
  25.  
  26.     public function __construct(UserPasswordEncoderInterface $passwordEncoder)
  27.     {
  28.         $this->passwordEncoder = $passwordEncoder;
  29.     }
  30.    
  31.  
  32.      /**
  33.      * @Route("/", name="admin.profile.index", methods={"GET"})
  34.      */
  35.     public function index(ProfileRepository $profileRepository, UserInterface $user): Response
  36.     {
  37.         return $this->render('admin/profile/index.html.twig', [
  38.             'profile' => $profileRepository->findById($user),
  39.  
  40.         ]);
  41.     }
  42.  
  43.     /**
  44.      * @Route("/{slug}/edit", name="admin.profile.edit", methods={"GET","POST"})
  45.      */
  46.    
  47.     // UserInterface $user,
  48.     public function edit(Request $request, User $user): Response
  49.     {
  50.  
  51.         $em = $this->getDoctrine()->getManager();
  52.        
  53.         $form = $this->createForm(ResetPasswordType::class, $user);
  54.         $form->handleRequest($request);
  55.  
  56.  
  57.        
  58.  
  59.        if ($form->isSubmitted() && $form->isValid()) {
  60.                
  61.      
  62.         $passwordEncoder = $this->get('security.password_encoder');
  63.         $password = $request->request->get('user')['password'];
  64.        
  65.  
  66.             // Si l'ancien mot de passe est bon
  67.             if ($passwordEncoder->isPasswordValid($user, $password)) {
  68.                
  69.                 $newEncodedPassword = $passwordEncoder->encodePassword($user, $user->getPlainPassword());
  70.                 $user->setPassword($newEncodedPassword);
  71.                
  72.                 $em->persist($user);
  73.                 $em->flush();
  74.  
  75.                 $this->addFlash('notice', 'Votre mot de passe à bien été changé !');
  76.  
  77.                 return $this->redirectToRoute('admin.profile.index');
  78.             } else {
  79.                 $form->addError(new FormError('Ancien mot de passe incorrect'));
  80.             }
  81.  
  82.        
  83.         }
  84.         return $this->render('admin/profile/edit.html.twig', [
  85.  
  86.             'profile' => $user,
  87.             'form' => $form->createView(),
  88.         ]);
  89.        
  90.     }
  91. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top