src/Controller/Admin/SecurityAdminController.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin;
  3. use App\Repository\UserRepository;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  9. use Symfony\Component\HttpFoundation\Request;
  10. #[Route('/admin'name'hs_training_center_admin_')]
  11. class SecurityAdminController extends AbstractController
  12. {
  13.     #[Route("/login"name:"login")]
  14.     public function login(AuthenticationUtils $authenticationUtils): Response
  15.     {
  16.         if ($this->getUser() && $this->getUser()->isPublished()) {
  17.              return $this->redirectToRoute('hs_training_center_dashboard');
  18.          }
  19.         // get the login error if there is one
  20.         $error $authenticationUtils->getLastAuthenticationError();
  21.         // last username entered by the user
  22.         $lastUsername $authenticationUtils->getLastUsername();
  23.         return $this->render('Admin/Security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  24.     }
  25.     #[Route("/logout"name:"logout")]
  26.     public function logout(): never
  27.     {
  28.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  29.     }
  30.     #[Route('/change-password'name'change_password')]
  31.     public function changePassword(Request $requestUserRepository $userRepositoryUserPasswordHasherInterface $passwordEncoder): Response
  32.     {
  33.         $user $this->getUser();
  34.         $error null;
  35.         if (!$user) {
  36.             return $this->redirectToRoute('hs_training_center_admin_login');
  37.         }
  38.         if ($request->isMethod('POST')) {
  39.             $currentPassword $request->request->get('current_password');
  40.             $newPassword $request->request->get('new_password');
  41.             if ($passwordEncoder->isPasswordValid($user$currentPassword)) {
  42.                 $encodedPassword $passwordEncoder->hashPassword($user$newPassword);
  43.                 $user->setPassword($encodedPassword);
  44.                 $userRepository->save($usertrue);
  45.                 // Redirection vers une page ou un message de succès
  46.                 $this->addFlash('success''Votre mot de passe a bien été mis à jour.');
  47.                 return $this->redirectToRoute('hs_training_center_dashboard');
  48.             } else {
  49.                 $this->addFlash('warning''Le mot de passe actuel est incorrect.');
  50.             }
  51.         }
  52.         return $this->render('Admin/Security/change_password.html.twig');
  53.     }
  54. }