<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Http\LoginLink\LoginLinkHandlerInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use App\Entity\User;
use App\Utilities\Utilities;
class SecurityController extends AbstractController {
public function login(AuthenticationUtils $authenticationUtils): Response {
if ($this->getUser()) {
return $this->redirectToRoute('app_home');
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
public function logout(): void {
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
public function requestLoginLink(LoginLinkHandlerInterface $loginLinkHandler, UserPasswordHasherInterface $userPasswordHasher, Request $request) {
// obtener el parametro del cookie para luego consultarlo al API foreneo
$userstudent = $request->get('username');
$util = new Utilities();
$url = $this->getParameter('api_getusersession_url');
$username = $this->getParameter('api_query_user');
$password = $this->getParameter('api_query_pass');
$data = ["token" => $userstudent];
$apiresponse = $util->curlResultTest($url, $data, false, $username, $password);
$response = json_decode($apiresponse['data'], true);
if(!isset($response['id']) || empty($response['id'])) {
return $this->redirect('https://praxisonline.co');
}
$em = $this->getDoctrine()->getManager();
$student = $response;
// load the user in some way (e.g. using the form input)
$util->setEm($em);
$util->setUserPasswordHasher($userPasswordHasher);
$user = $util->praxisNewUser($student['id'], $student['id'], 'student', true);
/*
* crear el estudiante
*/
$params = [
'stdCourse' => 1,
'stdcourseLevel' => isset($student['level']) && !empty($student['level']) ? $student['level'] : 0,
'stdname' => $student['name'],
'stdlastname' => $student['last_name'],
'stdIdentification' => preg_replace('/[^\da-z]/i', '', $student['id']),
'stdCampus' => $student['headquarter'],
'stdEmail' => $student['mail'],
];
$util->createStudent($params, $user);
// create a login link for $user this returns an instance
// of LoginLinkDetails
$loginLinkDetails = $loginLinkHandler->createLoginLink($user);
$loginLink = $loginLinkDetails->getUrl();
return $this->redirect($loginLink);
}
}