<?php
// src/Controller/SomeController.php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;
class SomeController extends AbstractController
{
/**
* @Route("/", name="app_homepage")
*/
public function index( Request $request )
{
$this -> autologin( "admin@mail.ru", $request );
return $this->render('base.html.twig');
}
public function autologin( $email, $request )
{
$dispatcher = new EventDispatcher();
$user = $this->getDoctrine()->getManager()->getRepository("App\Entity\User")->findOneBy(['email' => $email ]);
$token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
$this->get('security.token_storage')->setToken($token);
$this->get('session')->set('_security_main', serialize($token));
$event = new InteractiveLoginEvent($request, $token);
$dispatcher->dispatch($event);
}
}