initial commit

This commit is contained in:
boris
2025-09-30 09:24:25 +01:00
committed by boris
parent a783a12c97
commit c7770ea03b
4695 changed files with 525784 additions and 0 deletions

View File

@@ -0,0 +1,102 @@
<?= "<?php\n" ?>
namespace <?= $namespace; ?>;
<?= $use_statements; ?>
class <?= $class_name; ?> extends AbstractController
{
<?php if ($will_verify_email): ?>
public function __construct(private <?= $generator->getPropertyType($email_verifier_class_details) ?>$emailVerifier)
{
}
<?php endif; ?>
<?= $generator->generateRouteForControllerMethod($route_path, $route_name) ?>
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher<?= $login_after_registration ? ', Security $security': '' ?>, EntityManagerInterface $entityManager): Response
{
$user = new <?= $user_class_name ?>();
$form = $this->createForm(<?= $form_class_name ?>::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var string $plainPassword */
$plainPassword = $form->get('plainPassword')->getData();
// encode the plain password
$user->set<?= ucfirst($password_field) ?>($userPasswordHasher->hashPassword($user, $plainPassword));
$entityManager->persist($user);
$entityManager->flush();
<?php if ($will_verify_email): ?>
// generate a signed url and email it to the user
$this->emailVerifier->sendEmailConfirmation('app_verify_email', $user,
(new TemplatedEmail())
->from(new Address('<?= $from_email ?>', '<?= $from_email_name ?>'))
->to((string) $user-><?= $email_getter ?>())
->subject('Please Confirm your Email')
->htmlTemplate('registration/confirmation_email.html.twig')
);
<?php endif; ?>
// do anything else you need here, like send an email
<?php if ($login_after_registration): ?>
return $security->login($user, <?= $authenticator ?>, '<?= $firewall ?>');
<?php else: ?>
return $this->redirectToRoute('<?= $redirect_route_name ?>');
<?php endif; ?>
}
return $this->render('registration/register.html.twig', [
'registrationForm' => $form,
]);
}
<?php if ($will_verify_email): ?>
<?= $generator->generateRouteForControllerMethod('/verify/email', 'app_verify_email') ?>
public function verifyUserEmail(Request $request<?php if ($translator_available): ?>, TranslatorInterface $translator<?php endif ?><?= $verify_email_anonymously ? sprintf(', %s %s', $repository_class_name, $repository_var) : null ?>): Response
{
<?php if (!$verify_email_anonymously): ?>
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
<?php else: ?>
$id = $request->query->get('id');
if (null === $id) {
return $this->redirectToRoute('app_register');
}
<?php if ('$manager' === $repository_var): ?>
$repository = $manager->getRepository(<?= $user_class_name ?>::class);
$user = $repository->find($id);
<?php else: ?>
$user = <?= $repository_var; ?>->find($id);
<?php endif; ?>
if (null === $user) {
return $this->redirectToRoute('app_register');
}
<?php endif; ?>
// validate email confirmation link, sets User::isVerified=true and persists
try {
<?php if (!$verify_email_anonymously): ?>
/** @var <?= $user_class_name ?> $user */
$user = $this->getUser();
<?php endif; ?>
$this->emailVerifier->handleEmailConfirmation($request, $user);
} catch (VerifyEmailExceptionInterface $exception) {
$this->addFlash('verify_email_error', <?php if ($translator_available): ?>$translator->trans($exception->getReason(), [], 'VerifyEmailBundle')<?php else: ?>$exception->getReason()<?php endif ?>);
return $this->redirectToRoute('<?= $route_name ?>');
}
// @TODO Change the redirect on success and handle or remove the flash message in your templates
$this->addFlash('success', 'Your email address has been verified.');
return $this->redirectToRoute('app_register');
}
<?php endif; ?>
}

View File

@@ -0,0 +1,75 @@
<?= "<?php\n" ?>
namespace App\Tests;
<?= $use_statements ?>
class RegistrationControllerTest extends WebTestCase
{
private KernelBrowser $client;
private <?= $repository_class_name ?> $userRepository;
protected function setUp(): void
{
$this->client = static::createClient();
// Ensure we have a clean database
$container = static::getContainer();
/** @var EntityManager $em */
$em = $container->get('doctrine')->getManager();
$this->userRepository = $container->get(<?= $repository_class_name ?>::class);
foreach ($this->userRepository->findAll() as $user) {
$em->remove($user);
}
$em->flush();
}
public function testRegister(): void
{
// Register a new user
$this->client->request('GET', '/register');
self::assertResponseIsSuccessful();
self::assertPageTitleContains('Register');
$this->client->submitForm('Register', [
'registration_form[email]' => 'me@example.com',
'registration_form[plainPassword]' => 'password',
'registration_form[agreeTerms]' => true,
]);
// Ensure the response redirects after submitting the form, the user exists, and is not verified
// self::assertResponseRedirects('/'); @TODO: set the appropriate path that the user is redirected to.
self::assertCount(1, $this->userRepository->findAll());
self::assertFalse(($user = $this->userRepository->findAll()[0])->isVerified());
// Ensure the verification email was sent
// Use either assertQueuedEmailCount() || assertEmailCount() depending on your mailer setup
// self::assertQueuedEmailCount(1);
self::assertEmailCount(1);
self::assertCount(1, $messages = $this->getMailerMessages());
self::assertEmailAddressContains($messages[0], 'from', '<?= $from_email ?>');
self::assertEmailAddressContains($messages[0], 'to', 'me@example.com');
self::assertEmailTextBodyContains($messages[0], 'This link will expire in 1 hour.');
// Login the new user
$this->client->followRedirect();
$this->client->loginUser($user);
// Get the verification link from the email
/** @var TemplatedEmail $templatedEmail */
$templatedEmail = $messages[0];
$messageBody = $templatedEmail->getHtmlBody();
self::assertIsString($messageBody);
preg_match('#(http://localhost/verify/email.+)">#', $messageBody, $resetLink);
// "Click" the link and see if the user is verified
$this->client->request('GET', $resetLink[1]);
$this->client->followRedirect();
self::assertTrue(static::getContainer()->get(<?= $repository_class_name ?>::class)->findAll()[0]->isVerified());
}
}

View File

@@ -0,0 +1,46 @@
<?= "<?php\n" ?>
namespace App\Tests;
<?= $use_statements ?>
class RegistrationControllerTest extends WebTestCase
{
private KernelBrowser $client;
private <?= $repository_class_name ?> $userRepository;
protected function setUp(): void
{
$this->client = static::createClient();
// Ensure we have a clean database
$container = static::getContainer();
/** @var EntityManager $em */
$em = $container->get('doctrine')->getManager();
$this->userRepository = $container->get(<?= $repository_class_name ?>::class);
foreach ($this->userRepository->findAll() as $user) {
$em->remove($user);
}
$em->flush();
}
public function testRegister(): void
{
// Register a new user
$this->client->request('GET', '/register');
self::assertResponseIsSuccessful();
self::assertPageTitleContains('Register');
$this->client->submitForm('Register', [
'registration_form[email]' => 'me@example.com',
'registration_form[plainPassword]' => 'password',
'registration_form[agreeTerms]' => true,
]);
// Ensure the response redirects after submitting the form, the user exists, and is not verified
// self::assertResponseRedirects('/'); @TODO: set the appropriate path that the user is redirected to.
self::assertCount(1, $this->userRepository->findAll());
}
}

View File

@@ -0,0 +1,11 @@
<h1>Hi! Please confirm your email!</h1>
<p>
Please confirm your email address by clicking the following link: <br><br>
<a href="{{ signedUrl|raw }}">Confirm my Email</a>.
This link will expire in {{ expiresAtMessageKey|trans(expiresAtMessageData, 'VerifyEmailBundle') }}.
</p>
<p>
Cheers!
</p>

View File

@@ -0,0 +1,23 @@
<?= $helper->getHeadPrintCode('Register'); ?>
{% block body %}
<?php if ($will_verify_email): ?>
{% for flash_error in app.flashes('verify_email_error') %}
<div class="alert alert-danger" role="alert">{{ flash_error }}</div>
{% endfor %}
<?php endif; ?>
<h1>Register</h1>
{{ form_errors(registrationForm) }}
{{ form_start(registrationForm) }}
{{ form_row(registrationForm.<?= $username_field ?>) }}
{{ form_row(registrationForm.plainPassword, {
label: 'Password'
}) }}
{{ form_row(registrationForm.agreeTerms) }}
<button type="submit" class="btn">Register</button>
{{ form_end(registrationForm) }}
{% endblock %}