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.

AccountAdminController.php 2.5KB

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