Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

UserController.php 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace Lc\SovBundle\Controller\Admin;
  3. use Lc\SovBundle\Doctrine\EntityManager;
  4. use Lc\SovBundle\Form\Type\User\ChangePasswordFormType;
  5. use Lc\SovBundle\Form\Type\User\ProfileFormType;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  10. class UserController extends AbstractController
  11. {
  12. protected $em;
  13. public function __construct(EntityManager $em)
  14. {
  15. $this->em = $em;
  16. }
  17. public function profile(Request $request): Response
  18. {
  19. $user = $this->getUser();
  20. $form = $this->createForm(ProfileFormType::class, $user);
  21. $form->handleRequest($request);
  22. if ($form->isSubmitted() && $form->isValid()) {
  23. $user = $form->getData();
  24. $this->em->update($user);
  25. $this->em->flush();
  26. }
  27. return $this->render(
  28. '@LcSov/user/profile.html.twig',
  29. [
  30. 'form' => $form->createView()
  31. ]
  32. );
  33. }
  34. public function changePassword(Request $request, UserPasswordEncoderInterface $passwordEncoder): Response
  35. {
  36. $user = $this->getUser();
  37. $form = $this->createForm(ChangePasswordFormType::class, $user);
  38. $form->handleRequest($request);
  39. if ($form->isSubmitted() && $form->isValid()) {
  40. $user = $form->getData();
  41. $plainPassword = $form->get('plainPassword')->getData();
  42. // @TODO : créer UserManager
  43. $newPasswordEncoded = $passwordEncoder->encodePassword($user, $plainPassword);
  44. $user->setPassword($newPasswordEncoded);
  45. $this->em->update($user);
  46. $this->em->flush();
  47. }
  48. return $this->render(
  49. '@LcSov/user/change_password.html.twig',
  50. [
  51. 'form' => $form->createView()
  52. ]
  53. );
  54. }
  55. }