<?php
namespace App\Controller;
use App\Entity\AuthLog;
use App\Entity\Guardian;
use App\Repository\ChildRepository;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Core\Security as SecurityCore;
class SecurityController extends AbstractController
{
/**
* @Route("/login", name="app_login")
*/
public function login(SecurityCore $security, AuthenticationUtils $authenticationUtils): Response
{
if ($security->isGranted('ROLE_USER')) {
return $this->redirectToRoute('dashboard');
}
return $this->render('security/login.html.twig', [
// last username entered by the user (if any)
'last_username' => $authenticationUtils->getLastUsername(),
// last authentication error (if any)
'error' => $authenticationUtils->getLastAuthenticationError(),
]);
}
/**
* @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("/auth_log", name="app_auth_log_index")
* @Security("is_granted('ROLE_ADMIN') and is_granted('IS_AUTHENTICATED_FULLY')")
*/
public function authLog()
{
return $this->render('security/authLog.html.twig', [
]);
}
/**
* @Route("/tokenlogin", name="app_token_login")
*/
public function tokenLogin(Request $request, ChildRepository $childRepository, RequestStack $requestStack, EntityManagerInterface $entityManager): Response
{
$user = $this->getUser();
if ($user) {
return $this->redirectToRoute('dashboard');
}
$token = $request->request->get('token');
if($token)
{
$child = $childRepository->findOneBy(['loginToken' => $token]);
if($child) {
if($child->getGuardian() && $child->getGuardian()->getStatus() == Guardian::STATUS_LEFT['text'])
{
return $this->render('security/token.html.twig', []);
}
$token = new UsernamePasswordToken($child, 'main', $child->getRoles());
$this->get('security.token_storage')->setToken($token);
$requestStack->getSession()->set('_security_main', serialize($token));
$requestStack->getSession()->set('tokenlogin', ['token' => $request->request->get('token'), 'date' => (new \DateTime())->format('Y-m-d H:i:s')]);
$authLog = (new AuthLog())
->setStatus('success token')
->setEmail($child->getEmail());
$authLog->setUserAgent(
json_encode([
'child_id' => $child->getId(),
'HTTP_USER_AGENT' => isset($_SERVER['HTTP_USER_AGENT'])?$_SERVER['HTTP_USER_AGENT']:'',
'HTTP_SEC_CH_UA_PLATFORM' => isset($_SERVER['HTTP_SEC_CH_UA_PLATFORM'])?$_SERVER['HTTP_SEC_CH_UA_PLATFORM']:'',
'HTTP_SEC_CH_UA_MOBILE' => isset($_SERVER['HTTP_SEC_CH_UA_MOBILE'])?$_SERVER['HTTP_SEC_CH_UA_MOBILE']:'',
'HTTP_SEC_CH_UA' => isset($_SERVER['HTTP_SEC_CH_UA'])?$_SERVER['HTTP_SEC_CH_UA']:'',
'REMOTE_ADDR' => isset($_SERVER['REMOTE_ADDR'])?$_SERVER['REMOTE_ADDR']:'',
])
);
$entityManager->persist($authLog);
$entityManager->flush();
return $this->redirectToRoute('me_child');
}
}
return $this->render('security/token.html.twig', [
]);
}
}