<?php
namespace App\Controller;
use ContainerOt7jKaL\getConsole_Command_FormDebugService;
use Firebase\JWT\JWT;
use LogicException;
use Swift_Mailer;
use Swift_Message;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Contracts\Translation\TranslatorInterface;
use function strlen;
class SecurityController extends AbstractController
{
#[Route(path: '/login', name: 'app_login')]
public function login(Request $request, AuthenticationUtils $authenticationUtils, SessionInterface $session): Response
{
if ($this->getUser()) {
$url_target_path = $request->getSession()->get('_security.main.target_path');
if (!empty($url_target_path)) {
$path = $url_target_path;
} else {
$path = $this->generateUrl("home");
}
$response = new RedirectResponse($path);
$response->sendHeaders();
return $response;
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
$lastKadh = $session->get('_security.last_k_adherent');
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'last_kadherent' => $lastKadh, 'error' => $error]);
}
#[Route(path: '/logout', name: 'app_logout')]
public
function logout(): never
{
throw new LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
#[Route(path: '/resetting/init-{init}', name: 'app_first_password')]
#[Route(path: '/resetting/init', name: 'app_forget_password')]
public
function forgetPassword($init = false): Response
{
return $this->render('security/forget_password.html.twig', [
'init' => $init
]);
}
#[Route(path: '/resetting/check/{init}', name: 'reseting_check')]
public
function resetCheckAction(Request $request, TranslatorInterface $translator, Swift_Mailer $mailer, $init = null): Response
{
if ($request->isMethod('post') && !empty($request->request->get('_submit'))) {
$new_datas = [
'K_ADHERENT' => $request->request->get('username'),
'EMAIL' => $request->request->get('email')
];
$ch = curl_init($this->getParameter("auth_url")."/reset-password-token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($new_datas));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
$response = curl_exec($ch);
$response = json_decode($response, true);
if ($response['status'] == "200" && !empty($response['user']['TOKEN'])) {
if ($response['user']['PAYS'] == "BE" && $response['user']['LANGUE'] == "NL") {
$translator->setLocale("nl");
}
$url_confirmed = $request->getHttpHost() . $this->generateUrl("resetting_token_check", ['token' => $response['user']['TOKEN']]);
$title = (!is_null($init)) ? $translator->trans("Initialisation de votre mot de passe") : $translator->trans("Réinitialisation de votre mot de passe");
$message = (new Swift_Message('[Groupe All] - ' . $title))
->setFrom('contact@groupeall.fr')
->setTo($response['user']['EMAIL'])
->setBody(
$this->renderView(
(!is_null($init)) ? 'security/email/email_first_password.html.twig' : 'security/email/email_forget_password.html.twig',
['username' => $response['user']['K_ADHERENT'], "confirmationUrl" => $url_confirmed]
),
'text/html'
);
$mailer->send($message);
$this->addFlash('success', $translator->trans('Un e-mail a été envoyé sur votre adresse'));
return $this->render('security/forget_password.html.twig', ['init' => $init]);
} else {
$this->addFlash('danger', 'Identifiants invalides');
return $this->render('security/forget_password.html.twig', ['init' => $init]);
}
}
return $this->render('security/forget_password.html.twig');
}
/**
* @param $token
*/
#[Route(path: '/resetting/password-{token}', name: 'resetting_token_check')]
public function resettingTokenCheck($token): Response
{
$new_datas = [];
if (isset($token) && !empty($token)) {
$new_datas['TOKEN'] = $token;
$ch = curl_init($this->getParameter("auth_url")."/reset-password-check-token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($new_datas));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
$response = curl_exec($ch);
$response = json_decode($response, true);
if ($response['status'] == 200) {
return $this->render('security/password_resetting.html.twig', ['token' => $token]);
} else {
return $this->render('security/password_resetting.html.twig', ['token' => 'Invalid_token']);
}
}
}
/**
* @param $token
*/
#[Route(path: '/resetting/resetting-password-{token}', name: 'resetting_password')]
public
function resettingPasswordAction(Request $request, $token): Response
{
if (!empty($token) && $request->isMethod('post') && !empty($request->request->get('_submit'))) {
/**
* Vérifie que le mot de passe choisi réponde aux exigences.
*/
$errors = [];
if (!preg_match('#[A-Z]+#', $request->request->get('password_reset'))) {
$errors[] = 'Veuillez choisir un mot de passe contenant au moins une majuscule.';
}
if (!preg_match('#[0-9]+#', $request->request->get('password_reset'))) {
$errors[] = 'Veuillez choisir un mot de passe contenant au moins un chiffre.';
}
if (!preg_match('#[()*.!$%@&§]+#', $request->request->get('password_reset'))) {
$errors[] = 'Veuillez choisir un mot de passe contenant au moins un caractère spécial parmi ()*.!$%@&§';
}
if (strlen($request->request->get('password_reset')) < 10) {
$errors[] = 'Veuillez choisir un mot de passe d`\'au moins 10 caractères.';
}
if (!preg_match('#^[()*.!$%@&§A-Za-z0-9]*$#', $request->request->get('password_reset'))) {
$errors[] = 'Le mot de passe que vous avez choisi contient des caractères non autorisés.';
}
if ($request->request->get('password_reset') != $request->request->get('password_reset_repeat')) {
$errors[] = 'Le nouveau mot de passe et celui de validation sont différents.';
}
/**
* Retourne les erreurs si il y en a.
*/
if (!empty($errors)) {
return $this->render('security/password_resetting.html.twig', [
'token' => $token,
'errors' => $errors
]);
}
$new_datas = [
'TOKEN' => $token,
'PWD' => $request->request->get('password_reset')
];
$ch = curl_init($this->getParameter("auth_url")."/reset-password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($new_datas));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
$response = curl_exec($ch);
$response = json_decode($response, true);
if ($response['status'] == 200) {
return $this->render('security/password_resetting.html.twig', ['token' => $token, 'success' => true]);
} else {
return $this->render('security/password_resetting.html.twig', ['token' => $token, 'errors' => ['Une erreur est survenue']]);
}
}
}
}