<?php
namespace App\Controller;
use App\Entity\Utilisateur;
use App\Repository\UtilisateurRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\EncoderAwareInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use App\Services\AL\CoreService;
use Symfony\Component\HttpFoundation\Request;
class SecurityController extends AbstractController
{
#[Route('/al/login', methods: ['GET','POST'])]
#[Route('/login', methods: ['GET','POST'], name:'app_login')]
public function login(CoreService $coreService, AuthenticationUtils $authenticationUtils, Request $request): Response
{
// if ($this->getUser()) {
// return $this->redirectToRoute('target_path');
// }
$twig = "security/login.html.twig";
if(!$coreService->isAlis($request)) {
$twig = "AL/security/login.html.twig";
//@al-info : no login al part
if(!$coreService->isSitePro($request) && !$coreService->isTest()) {
return $coreService->show404Page($request);
}
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render($twig, ['last_username' => $lastUsername, 'error' => $error]);
}
#[Route('/admin', name:'app_admin')]
public function admin(CoreService $coreService, Request $request)
{
if($coreService->isSitePro($request)) {
return $this->redirectToRoute('app_al_admin_index');
}
return $this->redirect('/');
}
#[Route('/logout', name:'app_logout')]
public function logout()
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
#[Route('/reset_passwords', name:'app_reset_passwords')]
public function resetPasswords(UtilisateurRepository $utilisateurRepo, UserPasswordEncoderInterface $encoder, EntityManagerInterface $em)
{
$usernames = [];
$out = '';
foreach ($usernames as $username) {
$user = $utilisateurRepo->findOneByUsername($username);
if($user instanceof Utilisateur) {
$plainPassword = $this->getRandomPassword();
$encodedPassword = $encoder->encodePassword($user, $plainPassword);
$user->setPassword($encodedPassword);
$em->flush();
$out .= "$username;$plainPassword;$encodedPassword<br>";
}
}
return new Response($out);
}
private function getRandomPassword($length=10) : string
{
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$str = '';
$max = strlen($chars) - 1;
for ($i=0; $i < $length; $i++)
$str .= $chars[random_int(0, $max)];
return $str;
}
}