<?php
namespace App\Controller;
use App\Entity\ActorInformation;
use App\Entity\User;
use App\Form\CreateUserType;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class SecurityController extends AbstractController
{
/**
* @Route("/register", name="register", methods={"GET", "POST" })
*/
public function register(UserPasswordHasherInterface $passwordHasher, Request $request, EntityManagerInterface $entityManager): Response
{
$user = new User();
$form = $this->createForm(CreateUserType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid() && !empty($request->request->get("g-recaptcha-response"))) {
// hash the password (based on the security.yaml config for the $user class)
$hashedPassword = $passwordHasher->hashPassword(
$user,
$user->getPassword()
);
$user->setRoles(['ROLE_USER', 'ROLE_ACTOR']);
$user->setPassword($hashedPassword);
$entityManager->persist($user);
$entityManager->flush();
$actorInformation = new ActorInformation();
$actorInformation->setUser($user);
$entityManager->persist($actorInformation);
$entityManager->flush();
return $this->redirectToRoute('user', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('security/register.html.twig', [
'user' => $user,
'form' => $form,
]);
}
/**
* @Route("/fakeregister", name="fakeregister", methods={"GET"})
*/
public function fakeregister(UserPasswordHasherInterface $passwordHasher, ManagerRegistry $doctrine): Response
{
$entityManager = $doctrine->getManager();
$user = new User();
$user->setEmail('julien@gentleman-codeur.fr');
$plaintextPassword = "codeur";
// hash the password (based on the security.yaml config for the $user class)
$hashedPassword = $passwordHasher->hashPassword(
$user,
$plaintextPassword
);
$user->setPassword($hashedPassword);
$user->setRoles(['ROLE_USER', 'ROLE_ADMIN']);
$entityManager->persist($user);
$entityManager->flush();
return $this->json([
'message' => 'success',
]);
}
/**
* @Route("/login", name="login")
*/
public function index(AuthenticationUtils $authenticationUtils): Response
{
// 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', [
'last_username' => $lastUsername,
'error' => $error,
]);
}
/**
* @Route("/logout", name="logout", methods={"GET"})
*/
public function logout(): void
{
// controller can be blank: it will never be called!
throw new \Exception('Don\'t forget to activate logout in security.yaml');
}
/**
* @Route("/forgotold", name="forgotold", methods={"GET", "POST"})
*/
public function forgotold(): void
{
// controller can be blank: it will never be called!
throw new \Exception('Don\'t forget to activate forgot in security.yaml');
}
}