您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

UserController.php 2.5KB

3 年前
3 年前
3 年前
3 年前
3 年前
3 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 UserController 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. // @TODO : créer UserManager
  48. $newPasswordEncoded = $passwordEncoder->encodePassword($user, $plainPassword);
  49. $user->setPassword($newPasswordEncoded);
  50. $this->em->update($user);
  51. $this->em->flush();
  52. $this->addFlash('success', new TranslatableMessage('form.account_password.message.success', [], 'admin'));
  53. }
  54. return $this->render(
  55. '@LcSov/user/change_password.html.twig',
  56. [
  57. 'entity_class' => User::class,
  58. 'form' => $form->createView()
  59. ]
  60. );
  61. }
  62. }