<?php
namespace App\Controller;
use App\Entity\User;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use App\Form\RegistrationFormType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
/**
* @Route("/login", name="login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
if ($this->getUser() && $this->getUser()->isActive()) {
return $this->redirectToRoute('home');
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$email = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', ['email' => $email, 'error' => $error]);
}
/**
* @Route("/register", name="register")
* @IsGranted("ROLE_REGISTER")
*/
public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder): Response
{
$user = new User();
$form = $this->createForm(RegistrationFormType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// encode the plain password
$user->setPassword(
$passwordEncoder->encodePassword(
$user,
$form->get('plainPassword')->getData()
)
);
if($this->isGranted("ROLE_ADMIN") || $this->isGranted("ROLE_SUPER_ADMIN")){
$user->setEmailValidate(TRUE);
$user->setActive(TRUE);
}
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($user);
$entityManager->flush();
// do anything else you need here, like send an email
return $this->redirectToRoute('home');
}
return $this->render('security/register.html.twig', [
'registrationForm' => $form->createView(),
]);
}
/**
* @Route("/logout", name="logout")
*/
public function logout()
{
throw new \Exception('This method can be blank - it will be intercepted by the logout key on your firewall');
}
/**
* @Route("/maintenance", name="maintenance")
*/
public function maintenance(): Response
{
return $this->render('security/maintenance.html.twig');
}
}