mirror of
https://github.com/maelgangloff/domain-watchdog.git
synced 2025-12-29 16:15:04 +00:00
refactor: move registration logic to Processor
This commit is contained in:
@@ -2,11 +2,15 @@
|
|||||||
|
|
||||||
namespace App\Controller;
|
namespace App\Controller;
|
||||||
|
|
||||||
|
use App\Repository\UserRepository;
|
||||||
|
use App\Security\EmailVerifier;
|
||||||
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
|
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Bundle\SecurityBundle\Security;
|
use Symfony\Bundle\SecurityBundle\Security;
|
||||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
||||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||||
use Symfony\Component\Routing\Attribute\Route;
|
use Symfony\Component\Routing\Attribute\Route;
|
||||||
@@ -17,6 +21,8 @@ class HomeController extends AbstractController
|
|||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly RouterInterface $router,
|
private readonly RouterInterface $router,
|
||||||
private readonly ParameterBagInterface $parameterBag,
|
private readonly ParameterBagInterface $parameterBag,
|
||||||
|
private readonly EmailVerifier $emailVerifier,
|
||||||
|
private readonly LoggerInterface $logger,
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,4 +52,28 @@ class HomeController extends AbstractController
|
|||||||
|
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[Route('/verify/email', name: 'app_verify_email')]
|
||||||
|
public function verifyUserEmail(Request $request, UserRepository $userRepository): Response
|
||||||
|
{
|
||||||
|
$id = $request->query->get('id');
|
||||||
|
|
||||||
|
if (null === $id) {
|
||||||
|
return $this->redirectToRoute('index');
|
||||||
|
}
|
||||||
|
|
||||||
|
$user = $userRepository->find($id);
|
||||||
|
|
||||||
|
if (null === $user) {
|
||||||
|
return $this->redirectToRoute('index');
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->emailVerifier->handleEmailConfirmation($request, $user);
|
||||||
|
|
||||||
|
$this->logger->info('User has validated his email address', [
|
||||||
|
'username' => $user->getUserIdentifier(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
return $this->redirectToRoute('index');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ use ApiPlatform\Metadata\ApiResource;
|
|||||||
use ApiPlatform\Metadata\Get;
|
use ApiPlatform\Metadata\Get;
|
||||||
use ApiPlatform\Metadata\Post;
|
use ApiPlatform\Metadata\Post;
|
||||||
use App\Controller\MeController;
|
use App\Controller\MeController;
|
||||||
use App\Controller\RegistrationController;
|
|
||||||
use App\Repository\UserRepository;
|
use App\Repository\UserRepository;
|
||||||
|
use App\State\RegisterUserProcessor;
|
||||||
use Doctrine\Common\Collections\ArrayCollection;
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
use Doctrine\Common\Collections\Collection;
|
use Doctrine\Common\Collections\Collection;
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
@@ -37,13 +37,13 @@ use Symfony\Component\Validator\Constraints as Assert;
|
|||||||
new Post(
|
new Post(
|
||||||
uriTemplate: '/register',
|
uriTemplate: '/register',
|
||||||
routeName: 'user_register',
|
routeName: 'user_register',
|
||||||
controller: RegistrationController::class,
|
|
||||||
openapiContext: [
|
openapiContext: [
|
||||||
'summary' => 'Register a new user',
|
'summary' => 'Register a new user',
|
||||||
'description' => 'If the server configuration allows it, this endpoint enables the registration of a new user. Depending on the server configuration, a confirmation email may be sent to the provided email address so that the user can verify their email address.',
|
'description' => 'If the server configuration allows it, this endpoint enables the registration of a new user. Depending on the server configuration, a confirmation email may be sent to the provided email address so that the user can verify their email address.',
|
||||||
],
|
],
|
||||||
denormalizationContext: ['groups' => ['user:register']],
|
denormalizationContext: ['groups' => ['user:register']],
|
||||||
read: false,
|
read: false,
|
||||||
|
processor: RegisterUserProcessor::class,
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
)]
|
)]
|
||||||
|
|||||||
@@ -1,58 +1,49 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Controller;
|
namespace App\State;
|
||||||
|
|
||||||
|
use ApiPlatform\Metadata\Operation;
|
||||||
|
use ApiPlatform\State\ProcessorInterface;
|
||||||
use App\Entity\User;
|
use App\Entity\User;
|
||||||
use App\Repository\UserRepository;
|
|
||||||
use App\Security\EmailVerifier;
|
use App\Security\EmailVerifier;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\RequestStack;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||||
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
|
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
|
||||||
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
||||||
use Symfony\Component\HttpKernel\KernelInterface;
|
use Symfony\Component\HttpKernel\KernelInterface;
|
||||||
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
|
|
||||||
use Symfony\Component\Mime\Address;
|
use Symfony\Component\Mime\Address;
|
||||||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
||||||
use Symfony\Component\RateLimiter\RateLimiterFactory;
|
use Symfony\Component\RateLimiter\RateLimiterFactory;
|
||||||
use Symfony\Component\Routing\Attribute\Route;
|
|
||||||
use Symfony\Component\Serializer\SerializerInterface;
|
use Symfony\Component\Serializer\SerializerInterface;
|
||||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||||
|
|
||||||
class RegistrationController extends AbstractController
|
readonly class RegisterUserProcessor implements ProcessorInterface
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly EmailVerifier $emailVerifier,
|
private KernelInterface $kernel,
|
||||||
private readonly string $mailerSenderEmail,
|
private LoggerInterface $logger,
|
||||||
private readonly string $mailerSenderName,
|
private EntityManagerInterface $em,
|
||||||
private readonly RateLimiterFactory $userRegisterLimiter,
|
private EmailVerifier $emailVerifier,
|
||||||
private readonly EntityManagerInterface $em,
|
private string $mailerSenderEmail,
|
||||||
private readonly SerializerInterface $serializer,
|
private string $mailerSenderName,
|
||||||
private readonly LoggerInterface $logger,
|
private RateLimiterFactory $userRegisterLimiter,
|
||||||
private readonly KernelInterface $kernel,
|
private SerializerInterface $serializer,
|
||||||
private readonly ValidatorInterface $validator,
|
private ValidatorInterface $validator,
|
||||||
|
private UserPasswordHasherInterface $userPasswordHasher,
|
||||||
|
private RequestStack $requestStack,
|
||||||
|
private ParameterBagInterface $parameterBag,
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
|
||||||
* @throws TransportExceptionInterface
|
|
||||||
*/
|
|
||||||
#[Route(
|
|
||||||
path: '/api/register',
|
|
||||||
name: 'user_register',
|
|
||||||
defaults: [
|
|
||||||
'_api_resource_class' => User::class,
|
|
||||||
'_api_operation_name' => 'user_register',
|
|
||||||
],
|
|
||||||
methods: ['POST']
|
|
||||||
)]
|
|
||||||
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher): Response
|
|
||||||
{
|
{
|
||||||
if (false === $this->getParameter('registration_enabled')) {
|
$request = $this->requestStack->getCurrentRequest();
|
||||||
|
if (false === $this->parameterBag->get('registration_enabled')) {
|
||||||
throw new UnauthorizedHttpException('', 'Registration is disabled on this instance');
|
throw new UnauthorizedHttpException('', 'Registration is disabled on this instance');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,13 +64,13 @@ class RegistrationController extends AbstractController
|
|||||||
}
|
}
|
||||||
|
|
||||||
$user->setPassword(
|
$user->setPassword(
|
||||||
$userPasswordHasher->hashPassword(
|
$this->userPasswordHasher->hashPassword(
|
||||||
$user,
|
$user,
|
||||||
$user->getPlainPassword()
|
$user->getPlainPassword()
|
||||||
)
|
)
|
||||||
)->setCreatedAt(new \DateTimeImmutable());
|
)->setCreatedAt(new \DateTimeImmutable());
|
||||||
|
|
||||||
if (false === (bool) $this->getParameter('registration_verify_email')) {
|
if (false === (bool) $this->parameterBag->get('registration_verify_email')) {
|
||||||
$user->setVerifiedAt($user->getCreatedAt());
|
$user->setVerifiedAt($user->getCreatedAt());
|
||||||
} else {
|
} else {
|
||||||
$email = $this->emailVerifier->sendEmailConfirmation('app_verify_email', $user,
|
$email = $this->emailVerifier->sendEmailConfirmation('app_verify_email', $user,
|
||||||
@@ -107,28 +98,4 @@ class RegistrationController extends AbstractController
|
|||||||
|
|
||||||
return new Response(null, 201);
|
return new Response(null, 201);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[Route('/verify/email', name: 'app_verify_email')]
|
|
||||||
public function verifyUserEmail(Request $request, UserRepository $userRepository): Response
|
|
||||||
{
|
|
||||||
$id = $request->query->get('id');
|
|
||||||
|
|
||||||
if (null === $id) {
|
|
||||||
return $this->redirectToRoute('index');
|
|
||||||
}
|
|
||||||
|
|
||||||
$user = $userRepository->find($id);
|
|
||||||
|
|
||||||
if (null === $user) {
|
|
||||||
return $this->redirectToRoute('index');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->emailVerifier->handleEmailConfirmation($request, $user);
|
|
||||||
|
|
||||||
$this->logger->info('User has validated his email address', [
|
|
||||||
'username' => $user->getUserIdentifier(),
|
|
||||||
]);
|
|
||||||
|
|
||||||
return $this->redirectToRoute('index');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user