<?php
namespace App\Controller\Security;
use App\Entity\User;
use App\Form\UserRegistrationType;
use App\Security\CustomAuthenticator;
use MercurySeries\FlashyBundle\FlashyNotifier;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Serializer\SerializerInterface;
class SecurityController extends AbstractController
{
public const LAST_EMAIL = 'app_login_form_last_email';
/**
* @var FlashyNotifier
*/
private $flashy;
public function __construct(FlashyNotifier $flashy, ContainerInterface $container)
{
$this->flashy = $flashy;
$this->container = $container;
}
/**
* @Route("/login", name="security_login")
* @param AuthenticationUtils $authenticationUtils
* @return Response
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
/** @var AuthorizationChecker $authorization_checker */
$authorization_checker = $this->get('security.authorization_checker');
if ($authorization_checker->isGranted('IS_AUTHENTICATED_FULLY'))
{
return $this->redirectToRoute('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', [
'title' => 'Connectez vous',
'last_username' => $lastUsername,
'error' => $error
]);
}
/**
* @Route("/register", name="security_register")
* @param Request $request
* @param UserPasswordEncoderInterface $encoder
* @param GuardAuthenticatorHandler $guardHandler
* @param CustomAuthenticator $authenticator
* @return Response
*/
public function registration(
Request $request,
UserPasswordEncoderInterface $encoder,
GuardAuthenticatorHandler $guardHandler,
CustomAuthenticator $authenticator
): Response
{
if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY'))
{
return $this->redirectToRoute('home');
}
$user = new User();
$form = $this->createForm(UserRegistrationType::class, $user);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
$domaine = $form->get('domaines')->getData()->getRole();
$password = $encoder->encodePassword($user, $user->getPassword());
$user->setPassword($password);
$user->setEnabled(true);
$user->setRoles([$domaine]);
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
$this->flashy->success('Votre inscription a été prise en compte.');
//On connecte automatiquement l'utilisateur
return $guardHandler
->authenticateUserAndHandleSuccess($user, $request, $authenticator, 'main');
//return $this->redirectToRoute('security_login');
}
return $this->render('security/registration.html.twig', [
'form' => $form->createView(),
'last' => $user
]);
}
/**
* @Route("/logout", name="security_logout")
*/
public function logout()
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
}