<?php
namespace App\Controller\Auth;
use App\Entity\User;
use App\Form\AddAgentType;
use App\Security\EmailVerifier;
use App\Form\RegistrationFormType;
use App\Security\LoginAuthenticator;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Utils\AccountVerification;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Message\Auth\Registration\ConfirmAccount;
use App\Repository\Department\DepartmentRepository;
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
class RegistrationController extends AbstractController
{
private $emailVerifier;
protected $entityManager;
protected $departmentRepository;
public function __construct(EmailVerifier $emailVerifier,EntityManagerInterface $entityManager, DepartmentRepository $departmentRepository)
{
$this->emailVerifier = $emailVerifier;
$this->entityManager = $entityManager;
$this->departmentRepository = $departmentRepository;
}
/**
* @Route("/register", name="app.register")
*/
public function register(Request $request, UserPasswordHasherInterface $passwordEncoder, GuardAuthenticatorHandler $guardHandler, LoginAuthenticator $authenticator): Response
{
$user = new User();
if ($request->get("id"))
$form = $this->createForm(AddAgentType::class, $user);
else
$form = $this->createForm(RegistrationFormType::class, $user);
$form->handleRequest($request);
$department = $request->get("id");
$dpt = $this->departmentRepository->findOneBy(["slug" => $department]);
if ($form->isSubmitted() && $form->isValid()) {
$user->setPassword(
$passwordEncoder->hashPassword(
$user,
strtolower($form->get('firstname')->getData())
)
)
->setUsername(uniqid())->setAccountVerification(new AccountVerification());
if ($department) {
$user->setDepartment($dpt)
->setCompany($dpt->getCompany())
->setRoles(["ROLE_USER"])
->setAccountVerification(
(new AccountVerification())
->setAccountVerified(true)
->setEmailVerified(true)
->setEmailVerifiedAt(new \DateTime())
);
$this->addFlash('notice',$user->getNames(). ' a été enregistré avec succès !');
}
//affecter le membre
$user->setParent($this->getUser());
//
$this->entityManager->persist($user);
$this->entityManager->flush();
$this->dispatchMessage(new ConfirmAccount($user->getSlug()));
if ($department)
return $this->redirectToRoute('docs.executive.dpt',['slug' => $department]);
return $guardHandler->authenticateUserAndHandleSuccess(
$user,
$request,
$authenticator,
'main'
);
}
return $this->render('auth/registration/register.html.twig', [
'registrationForm' => $form->createView(),
'id' => $request->get("id"),
]);
}
/**
* @Route("/verify/email", name="app_verify_email")
*/
public function verifyUserEmail(Request $request): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
// validate email confirmation link, sets User::isVerified=true and persists
try {
$this->emailVerifier->handleEmailConfirmation($request, $this->getUser());
} catch (VerifyEmailExceptionInterface $exception) {
$this->addFlash('verify_email_error', $exception->getReason());
return $this->redirectToRoute('app.register');
}
// @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');
}
}