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.

84 lines
2.5KB

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