neofapometer/src/Controller/RegistrationController.php

73 lines
2.3 KiB
PHP

<?php
namespace App\Controller;
use App\Entity\RegistrationToken;
use App\Entity\User;
use App\Form\RegistrationFormType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
class RegistrationController extends CustomAbstractController
{
function __construct(
private EntityManagerInterface $em,
) {}
#[Route('/register/{id}', name: 'app_register')]
public function register(
RegistrationToken $token,
Request $request,
UserPasswordHasherInterface $userPasswordHasher,
Security $security,
): Response {
if ($token->getUser() !== null) throw $this->createNotFoundException();
$user = new User();
$form = $this->createForm(RegistrationFormType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$token->setUser($user);
/** @var string $plainPassword */
$plainPassword = $form->get('plainPassword')->getData();
// encode the plain password
$user->setPassword($userPasswordHasher->hashPassword($user, $plainPassword));
$this->em->persist($user);
$this->em->flush();
// do anything else you need here, like send an email
return $security->login($user, 'form_login', 'main');
}
return $this->render('registration/register.html.twig', [
'registrationForm' => $form,
]);
}
#[IsGranted('ROLE_CREATE_REGISTRATION_TOKEN')]
#[Route('/register/token/new', 'app_register_new_token', methods: 'POST')]
public function new_registration_token(): Response
{
$token = (new RegistrationToken)
->setCreatedBy($this->getUser());
$this->em->persist($token);
$this->em->flush();
return $this->renderBlock(
MainController::tmpl('index'),
'fap_statistics_container',
[],
);
}
}