|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace App\Controller; |
| 13 | + |
| 14 | +use App\Form\Type\ChangePasswordType; |
| 15 | +use App\Form\UserType; |
| 16 | +use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; |
| 17 | +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 18 | +use Symfony\Component\HttpFoundation\Request; |
| 19 | +use Symfony\Component\HttpFoundation\Response; |
| 20 | +use Symfony\Component\Routing\Annotation\Route; |
| 21 | +use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
| 22 | + |
| 23 | +/** |
| 24 | + * Controller used to manage current user. |
| 25 | + * |
| 26 | + * @Route("/profile") |
| 27 | + * @Security("has_role('ROLE_USER')") |
| 28 | + * |
| 29 | + * @author Romain Monteil <monteil.romain@gmail.com> |
| 30 | + */ |
| 31 | +class UserController extends AbstractController |
| 32 | +{ |
| 33 | + /** |
| 34 | + * @Route("/edit", methods={"GET", "POST"}, name="user_edit") |
| 35 | + */ |
| 36 | + public function edit(Request $request): Response |
| 37 | + { |
| 38 | + $user = $this->getUser(); |
| 39 | + |
| 40 | + $form = $this->createForm(UserType::class, $user); |
| 41 | + $form->handleRequest($request); |
| 42 | + |
| 43 | + if ($form->isSubmitted() && $form->isValid()) { |
| 44 | + $this->getDoctrine()->getManager()->flush(); |
| 45 | + |
| 46 | + $this->addFlash('success', 'user.updated_successfully'); |
| 47 | + |
| 48 | + return $this->redirectToRoute('user_edit'); |
| 49 | + } |
| 50 | + |
| 51 | + return $this->render('user/edit.html.twig', [ |
| 52 | + 'user' => $user, |
| 53 | + 'form' => $form->createView(), |
| 54 | + ]); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @Route("/change-password", methods={"GET", "POST"}, name="user_change_password") |
| 59 | + */ |
| 60 | + public function changePassword(Request $request, UserPasswordEncoderInterface $encoder): Response |
| 61 | + { |
| 62 | + $user = $this->getUser(); |
| 63 | + |
| 64 | + $form = $this->createForm(ChangePasswordType::class); |
| 65 | + $form->handleRequest($request); |
| 66 | + |
| 67 | + if ($form->isSubmitted() && $form->isValid()) { |
| 68 | + $user->setPassword($encoder->encodePassword($user, $form->get('newPassword')->getData())); |
| 69 | + |
| 70 | + $this->getDoctrine()->getManager()->flush(); |
| 71 | + |
| 72 | + return $this->redirectToRoute('security_logout'); |
| 73 | + } |
| 74 | + |
| 75 | + return $this->render('user/change_password.html.twig', [ |
| 76 | + 'form' => $form->createView(), |
| 77 | + ]); |
| 78 | + } |
| 79 | +} |
0 commit comments