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.

80 lines
2.4KB

  1. <?php
  2. namespace Lc\SovBundle\Controller\User;
  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\User\ChangePasswordFormType;
  7. use Lc\SovBundle\Form\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. use Symfony\Component\Translation\TranslatableMessage;
  14. class AccountAdminController extends AbstractController
  15. {
  16. protected $em;
  17. public function __construct(EntityManager $em)
  18. {
  19. $this->em = $em;
  20. }
  21. public function profile(Request $request): Response
  22. {
  23. $user = $this->getUser();
  24. $form = $this->createForm(ProfileFormType::class, $user);
  25. $form->handleRequest($request);
  26. if ($form->isSubmitted() && $form->isValid()) {
  27. $user = $form->getData();
  28. $this->em->update($user);
  29. $this->em->flush();
  30. $this->addFlash('success', new TranslatableMessage('form.account_profile.message.success', [], 'admin'));
  31. }
  32. return $this->render(
  33. '@LcSov/user/profile.html.twig',
  34. [
  35. 'form' => $form->createView(),
  36. ]
  37. );
  38. }
  39. public function changePassword(Request $request, UserPasswordEncoderInterface $passwordEncoder): Response
  40. {
  41. $user = $this->getUser();
  42. $form = $this->createForm(ChangePasswordFormType::class, $user);
  43. $form->handleRequest($request);
  44. if ($form->isSubmitted() && $form->isValid()) {
  45. $user = $form->getData();
  46. $plainPassword = $form->get('plain_password')->getData();
  47. $user->setPassword($passwordEncoder->encodePassword($user, $plainPassword));
  48. $this->em->update($user);
  49. $this->em->flush();
  50. $this->addFlash('success', new TranslatableMessage('form.account_password.message.success', [], 'admin'));
  51. }
  52. return $this->render(
  53. '@LcSov/user/change_password.html.twig',
  54. [
  55. 'entity_class' => User::class,
  56. 'form' => $form->createView()
  57. ]
  58. );
  59. }
  60. }