Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

90 lines
2.7KB

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