src/Controller/SecurityController.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Utilisateur;
  4. use App\Repository\UtilisateurRepository;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\Security\Core\Encoder\EncoderAwareInterface;
  10. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  11. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  12. use App\Services\AL\CoreService;
  13. use Symfony\Component\HttpFoundation\Request;
  14. class SecurityController extends AbstractController
  15. {
  16.     #[Route('/al/login'methods: ['GET','POST'])]
  17.     #[Route('/login'methods: ['GET','POST'], name:'app_login')]
  18.     public function login(CoreService $coreServiceAuthenticationUtils $authenticationUtilsRequest $request): Response
  19.     {
  20.         // if ($this->getUser()) {
  21.         //     return $this->redirectToRoute('target_path');
  22.         // }
  23.         
  24.         $twig "security/login.html.twig";
  25.         if(!$coreService->isAlis($request)) {
  26.             $twig "AL/security/login.html.twig";
  27.             
  28.             //@al-info : no login al part
  29.             if(!$coreService->isSitePro($request) && !$coreService->isTest()) {
  30.                 return $coreService->show404Page($request);
  31.             }
  32.         }
  33.         // get the login error if there is one
  34.         $error $authenticationUtils->getLastAuthenticationError();
  35.         // last username entered by the user
  36.         $lastUsername $authenticationUtils->getLastUsername();
  37.         return $this->render($twig, ['last_username' => $lastUsername'error' => $error]);
  38.     }
  39.     #[Route('/admin'name:'app_admin')]
  40.     public function admin(CoreService $coreServiceRequest $request)
  41.     {
  42.         if($coreService->isSitePro($request)) {
  43.             return $this->redirectToRoute('app_al_admin_index');
  44.         }
  45.         return $this->redirect('/');
  46.     }
  47.     #[Route('/logout'name:'app_logout')]
  48.     public function logout()
  49.     {
  50.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  51.     }
  52.     #[Route('/reset_passwords'name:'app_reset_passwords')]
  53.     public function resetPasswords(UtilisateurRepository $utilisateurRepoUserPasswordEncoderInterface $encoderEntityManagerInterface $em)
  54.     {
  55.         $usernames = [];
  56.         $out '';
  57.         foreach ($usernames as $username) {
  58.             $user $utilisateurRepo->findOneByUsername($username);
  59.             if($user instanceof Utilisateur) {
  60.                 $plainPassword $this->getRandomPassword();
  61.                 $encodedPassword $encoder->encodePassword($user$plainPassword);
  62.                 $user->setPassword($encodedPassword);
  63.                 $em->flush();
  64.                 $out .= "$username;$plainPassword;$encodedPassword<br>";
  65.             }
  66.         }
  67.         return new Response($out);
  68.     }
  69.     private function getRandomPassword($length=10) : string
  70.     {
  71.         $chars =  'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  72.         $str '';
  73.         $max strlen($chars) - 1;
  74.         for ($i=0$i $length$i++)
  75.             $str .= $chars[random_int(0$max)];
  76.         return $str;
  77.     }
  78. }