119 lines
4.5 KiB
PHP
119 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Form\LoginFormType;
|
|
use App\Form\RegistrationFormType;
|
|
use App\Repository\UserRepository;
|
|
use App\Security\EmailVerifier;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Mime\Address;
|
|
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
|
use Symfony\Component\Routing\Annotation\Route;
|
|
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
|
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
|
|
|
|
class SecurityController extends AbstractController
|
|
{
|
|
|
|
public function __construct(private readonly EmailVerifier $emailVerifier)
|
|
{
|
|
// empty body
|
|
}
|
|
|
|
#[Route(path: '/login', name: 'app_login')]
|
|
public function index(AuthenticationUtils $authenticationUtils): Response
|
|
{
|
|
// get the login error if there is one
|
|
if ($error = $authenticationUtils->getLastAuthenticationError()) {
|
|
$this->addFlash(type: 'error', message: $error->getMessageKey());
|
|
}
|
|
|
|
// last username entered by the user
|
|
$lastUsername = $authenticationUtils->getLastUsername();
|
|
|
|
return $this->render(view: '@default/security/login.html.twig', parameters: [
|
|
'last_username' => $lastUsername,
|
|
'error' => '',
|
|
]);
|
|
}
|
|
|
|
|
|
#[Route(path: '/register', name: 'app_register')]
|
|
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager): Response
|
|
{
|
|
$form = $this->createForm(type: RegistrationFormType::class);
|
|
$form->handleRequest(request: $request);
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
$user = $form->getData();
|
|
// hash the plain password
|
|
$user->setPassword(
|
|
password: $userPasswordHasher->hashPassword(
|
|
user: $user,
|
|
plainPassword: $form->get(name: 'password')->getData()
|
|
)
|
|
);
|
|
|
|
if ($form->get(name: 'agreeTerms')->getData()) {
|
|
$user->agreeTerms();
|
|
} // no else, we already confirmed in the form itself
|
|
$entityManager->persist(entity: $user);
|
|
$entityManager->flush();
|
|
|
|
// generate a signed url and email it to the user
|
|
$this->emailVerifier->sendEmailConfirmation(verifyEmailRouteName: 'app_verify_email', user: $user,
|
|
email: (new TemplatedEmail())
|
|
->from(new Address(address: 'info@24unix.net', name: '24unix.net'))
|
|
->to($user->getEmail())
|
|
->subject(subject: 'Please Confirm your Email')
|
|
->htmlTemplate(template: '@default/security/mail/registration.html.twig')
|
|
->context(context: [
|
|
'username' => $user->getUsername()
|
|
])
|
|
);
|
|
|
|
return $this->render(view: '@default/security/registration_finished.html.twig');
|
|
}
|
|
|
|
return $this->render(view: '@default/security/register.html.twig', parameters: [
|
|
'registrationForm' => $form->createView(),
|
|
]);
|
|
}
|
|
|
|
#[Route(path: '/verify/email', name: 'app_verify_email')]
|
|
public function verifyUserEmail(Request $request, TranslatorInterface $translator, UserRepository $userRepository): Response
|
|
{
|
|
$id = $request->get(key: 'id');
|
|
|
|
if ($id === null) {
|
|
return $this->redirectToRoute(route: 'app_register');
|
|
}
|
|
|
|
$user = $userRepository->find($id);
|
|
|
|
if ($user === null) {
|
|
return $this->redirectToRoute(route: 'app_register');
|
|
}
|
|
|
|
// validate email confirmation link, sets User::isVerified=true and persists
|
|
try {
|
|
$this->emailVerifier->handleEmailConfirmation(request: $request, user: $user);
|
|
} catch (VerifyEmailExceptionInterface $exception) {
|
|
$this->addFlash(type: 'verify_email_error', message: $translator->trans(id: $exception->getReason(), parameters: [], domain: 'VerifyEmailBundle'));
|
|
|
|
return $this->redirectToRoute(route: 'app_main');
|
|
}
|
|
|
|
$this->addFlash(type: 'success', message: 'Your email address has been verified.');
|
|
|
|
return $this->redirectToRoute(route: 'app_main');
|
|
}
|
|
|
|
}
|