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 年之前
3 年之前
3 年之前
3 年之前
3 年之前
3 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace Lc\SovBundle\Controller\User;
  3. use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
  4. use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
  5. use Lc\SovBundle\Doctrine\EntityManager;
  6. use Lc\SovBundle\Form\User\ChangePasswordFormType;
  7. use Lc\SovBundle\Form\User\ProfileFormType;
  8. use Lc\SovBundle\Form\User\UserMerchantFormType;
  9. use Lc\SovBundle\Model\User\User;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  14. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  15. use Symfony\Component\Translation\TranslatableMessage;
  16. class AccountAdminController extends AbstractController
  17. {
  18. protected $em;
  19. public function __construct(EntityManager $em)
  20. {
  21. $this->em = $em;
  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/user/profile.html.twig',
  36. [
  37. 'form' => $form->createView(),
  38. ]
  39. );
  40. }
  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->em->update($user);
  51. $this->em->flush();
  52. $this->addFlash('success', new TranslatableMessage('form.account_password.message.success', [], 'admin'));
  53. }
  54. return $this->render(
  55. '@LcSov/user/change_password.html.twig',
  56. [
  57. 'entity_class' => User::class,
  58. 'form' => $form->createView()
  59. ]
  60. );
  61. }
  62. }