factorisation
This commit is contained in:
@ -14,14 +14,21 @@ use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
|
||||
use Symfony\Component\Routing\Generator\UrlGenerator;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
|
||||
|
||||
class MainController extends AbstractController
|
||||
{
|
||||
/**
|
||||
/**
|
||||
* @var Session
|
||||
*/
|
||||
private $session;
|
||||
|
||||
/**
|
||||
* @var UrlGeneratorInterface
|
||||
*/
|
||||
private $router;
|
||||
|
||||
/**
|
||||
* @var HttpClientInterface
|
||||
@ -29,11 +36,12 @@ class MainController extends AbstractController
|
||||
public $client;
|
||||
private $pdoServices;
|
||||
|
||||
public function __construct(PdoServices $pdoServices, HttpClientInterface $client, SessionInterface $session)
|
||||
public function __construct(PdoServices $pdoServices, HttpClientInterface $client, SessionInterface $session, UrlGeneratorInterface $router)
|
||||
{
|
||||
$this->pdoServices = $pdoServices;
|
||||
$this->session = $session;
|
||||
$this->client = $client;
|
||||
$this->router = $router;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -67,45 +75,46 @@ class MainController extends AbstractController
|
||||
*/
|
||||
public function oauth(Request $request)
|
||||
{
|
||||
if( $request->headers->get('referer') !== $this->router->generate('oauth_login', [], 0) && !in_array($request->headers->get('referer'), $this->getParameter('urlIssuer'))){
|
||||
throw new BadRequestException('Vous devez passer par le issuer pour vous connecter');
|
||||
}
|
||||
|
||||
$user = new User();
|
||||
$loginForm = $this->createForm(UserType::class, $user);
|
||||
$loginForm->handleRequest($request);
|
||||
if($loginForm->isSubmitted() && $loginForm->isValid()){
|
||||
$email = $loginForm->get('email')->getData();
|
||||
$dbh = $this->pdoServices->connection();
|
||||
try {
|
||||
//requête préparée
|
||||
$query = $dbh->prepare($this->getParameter("queryHashPassword")."'".$email ."';");
|
||||
$query->execute();
|
||||
$hashPassword = $query->fetch(PDO::FETCH_ASSOC);
|
||||
// requête préparée
|
||||
$datas = $this->pdoServices->fetchDatas($email);
|
||||
|
||||
if(!$hashPassword){
|
||||
if(!$datas){
|
||||
// Si le hash du password n'est pas trouvé, c'est que l'email n'existe pas, on retourne la page de login avec une erreur
|
||||
return $this->render('login.html.twig', [
|
||||
'form'=>$loginForm->createView(),
|
||||
"error"=> "mail non trouvé",
|
||||
"form" => $loginForm->createView(),
|
||||
"error_mail" => "mail non trouvé",
|
||||
|
||||
]);
|
||||
}
|
||||
$hashPassword = array_values($hashPassword)[0];
|
||||
$hashPassword = $datas[$this->getParameter('passwordColumnName')];
|
||||
$password = $loginForm->get('password')->getData();
|
||||
|
||||
if($this->pdoServices->verifyPassword($password, $hashPassword)){
|
||||
$this->session->set('datas', $this->pdoServices->fetchDatas($email));
|
||||
unset($datas[$this->getParameter('passwordColumnName')]);
|
||||
$this->session->set('datas', $datas);
|
||||
$response = $this->client->request('PUT', $this->getParameter('url_login_challenge_accept').$this->session->get('challenge'), [
|
||||
'json' => [
|
||||
'subject' => $email,
|
||||
],
|
||||
]);
|
||||
// On initie l'acceptation du login challenge émis par hydra et on récupère l'url de redirection
|
||||
// dd($response->toArray());
|
||||
$redirect_to = $response->toArray()['redirect_to'];
|
||||
return $this->redirect($redirect_to, 301);
|
||||
|
||||
}else{
|
||||
return $this->render('login.html.twig', [
|
||||
'form'=>$loginForm->createView(),
|
||||
"error"=> "Le mot de passe est incorrect"
|
||||
"error_password"=> "Le mot de passe est incorrect"
|
||||
|
||||
]);
|
||||
}
|
||||
@ -113,14 +122,9 @@ class MainController extends AbstractController
|
||||
}catch (\Exception $e){
|
||||
dd($e);
|
||||
}
|
||||
// $rememberMe = $loginForm->get('rememberMe')->getData();
|
||||
|
||||
}
|
||||
|
||||
return $this->render('login.html.twig', [
|
||||
'form'=>$loginForm->createView(),
|
||||
"error_mail"=>false,
|
||||
"error_password"=>false
|
||||
]);
|
||||
}
|
||||
|
||||
@ -162,4 +166,14 @@ class MainController extends AbstractController
|
||||
return $this->redirect($redirect_to, 301);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/oauth/logout", name="app_logout")
|
||||
*/
|
||||
public function logout()
|
||||
{
|
||||
$this->session->clear();
|
||||
|
||||
return $this->redirect($this->getParameter('urlLogoutSuccess'));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user