src/Controller/SecurityController.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\PasswordUpdateType;
  4. use App\Model\UserManager;
  5. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  11. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  12. class SecurityController extends AbstractController
  13. {
  14.     /**
  15.      * @Route("/login", name="app_login")
  16.      *
  17.      * @param AuthenticationUtils $authenticationUtils
  18.      *
  19.      * @return Response
  20.      */
  21.     public function login(AuthenticationUtils $authenticationUtils): Response
  22.     {
  23.         if ($this->getUser()) {
  24.             return $this->redirectToRoute('home');
  25.         }
  26.         $error $authenticationUtils->getLastAuthenticationError();
  27.         $lastUsername $authenticationUtils->getLastUsername();
  28.         return $this->render('security/login.html.twig', [
  29.             'error' => $error,
  30.             'last_username' => $lastUsername,
  31.         ]);
  32.     }
  33.     /**
  34.      * @Route("/user/change-password", name="user_change_password")
  35.      *
  36.      * @IsGranted("ROLE_CHANGE_PASSWORD")
  37.      *
  38.      * @param Request $request
  39.      * @param UserPasswordEncoderInterface $encoder
  40.      * @param UserManager $manager
  41.      *
  42.      * @return Response
  43.      */
  44.     public function changePassword(Request $requestUserPasswordEncoderInterface $encoderUserManager $manager)
  45.     {
  46.         $user $this->getUser();
  47.         $form $this->createForm(PasswordUpdateType::class);
  48.         $form->handleRequest($request);
  49.         if ($form->isSubmitted() && $form->isValid()) {
  50.             $oldPassword $form->get('oldPassword')->getData();
  51.             $newPassword $form->get('plainPassword')->getData();
  52.             if ($encoder->isPasswordValid($user$oldPassword) === false) {
  53.                 $this->addFlash('error''Your old password is not correct.');
  54.                 return $this->redirectToRoute('user_change_password');
  55.             }
  56.             if ($oldPassword === $newPassword) {
  57.                 $this->addFlash('error''Your new password cannot be the same as your old password.');
  58.                 return $this->redirectToRoute('user_change_password');
  59.             }
  60.             $manager->updatePassword($user$encoder->encodePassword($user$newPassword));
  61.             $this->addFlash('success''Password updated successfully');
  62.             return $this->redirectToRoute('user_change_password');
  63.         }
  64.         return $this->render('security/change_password.html.twig', [
  65.             'form' => $form->createView(),
  66.             'policies' => $this->getParameter('app.password.policies')
  67.         ]);
  68.     }
  69.     /**
  70.      * @Route("/logout", name="app_logout")
  71.      */
  72.     public function logout()
  73.     {
  74.         return $this->redirectToRoute('app_login');
  75.     }
  76. }