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.

79 lines
2.3KB

  1. <?php
  2. namespace Lc\SovBundle\Controller\User;
  3. use Lc\SovBundle\Doctrine\EntityManager;
  4. use Lc\SovBundle\Form\User\ChangePasswordFormType;
  5. use Lc\SovBundle\Form\User\ProfileFormType;
  6. use Lc\SovBundle\Model\User\User;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  11. use Symfony\Component\Translation\TranslatableMessage;
  12. class AccountAdminController 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. $this->addFlash('success', new TranslatableMessage('form.account_profile.message.success', [], 'admin'));
  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. $user->setPassword($passwordEncoder->encodePassword($user, $plainPassword));
  46. $this->em->update($user);
  47. $this->em->flush();
  48. $this->addFlash('success', new TranslatableMessage('form.account_password.message.success', [], 'admin'));
  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. }