<?php
namespace App\Controller;
use App\Entity\User;
use App\Service\UserService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
/**
* @Route("/", name="app_login")
* @param AuthenticationUtils $authenticationUtils
* @return Response
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
$securityContext = $this->container->get('security.authorization_checker');
if ($securityContext->isGranted('IS_AUTHENTICATED_FULLY')) {
return $this->redirectToRoute('app_homepage');
}
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
/**
* @Route("/logout", name="app_logout", methods={"GET"})
*/
public function logout()
{
// controller can be blank: it will never be executed!
throw new \Exception('Don\'t forget to activate logout in security.yaml');
}
/**
* @Route("/forgottenPassword", name="forgotten_pass")
* @param AuthenticationUtils $authenticationUtils
* @return Response
*/
public function forgottenPassword(AuthenticationUtils $authenticationUtils): Response
{
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/password.html.twig', ['last_username' => $lastUsername]);
}
/**
* @Route("/newPassword", name="new_pass")
* @param UserServiceController $userService
* @param AuthenticationUtils $authenticationUtils
* @return Response
*/
public function makeNewPassword(UserService $userService, AuthenticationUtils $authenticationUtils){
$lastUsername = $authenticationUtils->getLastUsername();
// On check si l'utilisateur existe en base, sinon on retourne un flash message
$pseudo = $_POST['pseudo'];
$pass = $_POST['password'];
$pass = password_hash($pass, PASSWORD_DEFAULT);
$existsUser = $this->getDoctrine()->getRepository(User::class)->getUserByPseudo($pseudo);
if(count($existsUser) === 0){
$this->addFlash('error', 'Pseudonyme inconnu, veuillez ré-essayer !');
}else{
// Génération du nouveau mot de passe
$userService->updatePasswordByPseudo($pseudo, $pass);
$this->addFlash('success', 'Votre mot de passe a bien été modifié !');
}
return $this->render('security/password.html.twig', ['last_username' => $lastUsername]);
}
/**
* @Route("/changePasswordFirstConnexion", name="change_pass_first_connexion")
* @param UserServiceController $userService
* @return RedirectResponse
*/
public function changePasswordFirstConnexion(UserService $userService){
$pass = $_POST['password'];
$pass = password_hash($pass, PASSWORD_DEFAULT);
$userId = $this->getUser()->getId();
$userService->updatePasswordActiveById($userId,$pass);
$this->addFlash('success', 'Vos informations ont été mises à jour, veuillez vous ré-identifier !');
return $this->redirectToRoute('app_login');
}
/**
* @Route("/makeNewPassAjax", name="make_new_pass_ajax")
*/
public function makeNewPassAjax(){
$userId = $_POST['userId'];
$password = $_POST['password'];
$user = $this->getDoctrine()->getRepository(User::class)->find($userId);
$user->setPassword(password_hash($password, PASSWORD_BCRYPT));
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
return $this->redirect('user_gestion');
}
/**
* @Route("/setPassDefault", name="set_pass_default")
*/
public function setPassDefault(){
// Fonction de mise à jour des mots de passe à "ABIOLAB" si jamais connectés
$aUsers = $this->getDoctrine()->getRepository(User::class)->findBy(['active' => 0]);
foreach ($aUsers as $iValue) {
$user = $iValue;
$em = $this->getDoctrine()->getManager();
$user->setPassword(password_hash('abiolab', PASSWORD_BCRYPT));
$em->persist($user);
$em->flush();
}
return $this->redirectToRoute('user_gestion');
}
}