You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.2KB

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