src\Controller\SecurityController.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\PasswordResetType;
  4. use App\Model\User\Authtype\Authtype;
  5. use App\Model\User\UserFacade;
  6. use App\Repository\ResetPasswordHolderRepository;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  14. use Symfony\Component\Validator\Constraints\Length;
  15. use Symfony\Component\Validator\Constraints\NotBlank;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. class SecurityController extends AbstractController
  18. {
  19.     public function __construct(private readonly EntityManagerInterface $entityManager)
  20.     {
  21.     }
  22.     #[Route(path'/login/{authtype}'name'app_login')]
  23.     public function login(AuthenticationUtils $authenticationUtils, ?string $authtype null): Response
  24.     {
  25.         if ($this->getUser()) {
  26.             return $this->redirectToRoute('dashboard');
  27.         }
  28.         // get the login error if there is one
  29.         $error $authenticationUtils->getLastAuthenticationError();
  30.         // last username entered by the user
  31.         $lastUsername $authenticationUtils->getLastUsername();
  32.         $authtypes $this->entityManager->getRepository(Authtype::class)->findBy( [], ['isPrimary'=>'DESC''id'=>'DESC']);
  33.         $selectedAuthtype null;
  34.         if ($authtype) {
  35.             $selectedAuthtype $this->entityManager->getRepository(Authtype::class)->findOneBy([
  36.                 'name' => $authtype
  37.             ]);
  38.         }
  39.         return $this->render('security/login.html.twig', [
  40.             'last_username' => $lastUsername
  41.             'error' => $error,
  42.             'authtypes' => $authtypes,
  43.             'selectedAuthtype' => $selectedAuthtype,
  44.             ]
  45.         );
  46.     }
  47.     #[Route(path'/password-reset'name'password_reset')]
  48.     public function passwordReset(Request $requestResetPasswordHolderRepository $rep,
  49.         TranslatorInterface $translatorEntityManagerInterface $emUserFacade $userFacade)
  50.     {
  51.         $userId $request->get("userId");
  52.         $hex$request->get("hex");
  53.         $resetPassword $rep->get($userId$hex);
  54.         if($resetPassword === null)
  55.         {
  56.             $this->addFlash("error"$translator->trans("The URL is not correct, please try resetting your password again"));
  57.             return $this->redirectToRoute("app_login");
  58.         }
  59.         $form $this->createForm(PasswordResetType::class);
  60.         $form->handleRequest($request);
  61.         if($form->isSubmitted() && $form->isValid())
  62.         {
  63.             $resetPassword->setActivatedAt(new \DateTime());
  64.             $em->flush();
  65.             $userFacade->setNewPassword($resetPassword->getUser(), $form->get("password")->getData());
  66.             $em->flush();
  67.             $this->addFlash("info"$translator->trans("Password reset, please log in"));
  68.             return $this->redirectToRoute("app_login");
  69.         }
  70.         return $this->render(
  71.             "security/resetPassword.html.twig", [
  72.                 'form' => $form->createView()
  73.             ]   
  74.         );
  75.        
  76.     }
  77.     #[Route(path'/logout'name'app_logout')]
  78.     public function logout(): never
  79.     {
  80.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  81.     }
  82. }