Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

87 linhas
2.7KB

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