Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

77 lines
2.1KB

  1. <?php
  2. namespace Lc\SovBundle\Controller\Admin;
  3. use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
  4. use Lc\SovBundle\Doctrine\EntityManager;
  5. use Lc\SovBundle\Form\Type\User\ChangePasswordFormType;
  6. use Lc\SovBundle\Form\Type\User\ProfileFormType;
  7. use Lc\SovBundle\Model\User\User;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  12. class UserController extends AbstractController
  13. {
  14. protected $em;
  15. public function __construct(EntityManager $em)
  16. {
  17. $this->em = $em;
  18. }
  19. public function profile(Request $request): Response
  20. {
  21. $user = $this->getUser();
  22. $form = $this->createForm(ProfileFormType::class, $user);
  23. $form->handleRequest($request);
  24. if ($form->isSubmitted() && $form->isValid()) {
  25. $user = $form->getData();
  26. $this->em->update($user);
  27. $this->em->flush();
  28. }
  29. return $this->render(
  30. '@LcSov/user/profile.html.twig',
  31. [
  32. 'form' => $form->createView(),
  33. ]
  34. );
  35. }
  36. public function changePassword(Request $request, UserPasswordEncoderInterface $passwordEncoder): Response
  37. {
  38. $user = $this->getUser();
  39. $form = $this->createForm(ChangePasswordFormType::class, $user);
  40. $form->handleRequest($request);
  41. if ($form->isSubmitted() && $form->isValid()) {
  42. $user = $form->getData();
  43. $plainPassword = $form->get('plainPassword')->getData();
  44. // @TODO : créer UserManager
  45. $newPasswordEncoded = $passwordEncoder->encodePassword($user, $plainPassword);
  46. $user->setPassword($newPasswordEncoded);
  47. $this->em->update($user);
  48. $this->em->flush();
  49. }
  50. return $this->render(
  51. '@LcSov/user/change_password.html.twig',
  52. [
  53. 'entity_class' => User::class,
  54. 'form' => $form->createView()
  55. ]
  56. );
  57. }
  58. }