<?php
namespace App\Controller;
use App\Entity\Users\Child;
use App\Entity\Users\User;
use App\Repository\Users\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Http\Attribute\CurrentUser;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface;
class SecurityController extends AbstractController
{
//ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
/**
* @Route("/connexion", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('dashboard');
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/connexion.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
/**
* @Route("/verify", name="registration_confirmation_route")
*/
public function verifyUserEmail(Request $request,VerifyEmailHelperInterface $helper,UserRepository $userRepository): Response
{
$id = $request->get('id');
if (null === $id) {
return $this->redirectToRoute('front');
}
$user = $userRepository->find($id);
// Ensure the user exists in persistence
if (null === $user) {
return $this->redirectToRoute('front');
}
// Do not get the User's Id or Email Address from the Request object
try {
$helper->validateEmailConfirmation($request->getUri(), $user->getId(), $user->getEmail());
$user->setIsVerified(true);
$this->getDoctrine()->getManager()->flush();
} catch (VerifyEmailExceptionInterface $e) {
$this->addFlash('verify_email_error', $e->getReason());
return $this->redirectToRoute('front');
}
// Mark your user as verified. e.g. switch a User::verified property to true
$this->addFlash('notice', 'Super ! Tu peux maintenant te connecter à ton espace de révisions.');
return $this->redirectToRoute('app_login');
}
/**
* @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("/api/login", name="api_login_v2",methods={"post"})
*/
public function index(?User $user)
{
if ($this->getUser() === $user) {
return $this->json([
'message' => 'missing credentials',
], Response::HTTP_UNAUTHORIZED);
}
return $this->json([
'user' => $this->getUser()->getUserIdentifier(),
'path'=> $_SERVER['HTTP_REFERER']
]);
}
/**
* @Route("/impersonate/user/switch")
*/
public function impersonateUser(Request $request,EntityManagerInterface $entityManager){
$email = $request->get('email');
$email = urldecode($email);
$email = str_replace(' ','+',$email);
if(filter_var($email,FILTER_VALIDATE_EMAIL)){
$current = $entityManager->getRepository(User::class)->findOneByEmail($email);
if($current){
// $cookies = $request->cookies;
// if (!is_null($cookies->get('admin_switch_user'))) {
// $cookieTraining = $cookies->get('admin_switch_user');
// $value = json_decode($cookieTraining,true);
//$current = $entityManager->getRepository(User::class)->findOneByEmail($value['current']);
// login user
//if($current){
$token = new UsernamePasswordToken($current, $current->getPassword(), "main", $current->getRoles());
$this->get("security.token_storage")->setToken($token);
$this->get('session')->set('_security_main', serialize($token));
$event = new InteractiveLoginEvent($request, $token);
$dispatcher = new EventDispatcher();
$dispatcher->dispatch($event, "security.interactive_login");
$url = $this->generateUrl('dashboard').'?_switch_user='.urlencode($email);
return $this->redirect($url);
//}
//}
}
}
return $this->redirectToRoute("dashboard");
}
}