<?php
namespace App\Controller\Front;
use App\Entity\Club;
use App\Entity\Admin;
use App\Entity\Coach;
use App\Entity\Operator;
use App\Entity\ResetPassword;
use App\Form\ResetPasswordType;
use App\EmailNotification\ToPro;
use App\EmailNotification\ToUser;
use App\Repository\UserRepository;
use App\EmailNotification\ToTrainee;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
#[Route(path: '/{_locale}')]
class AccountController extends AbstractController
{
private $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
private function generateString(int $length = 64)
{
$length = ($length < 4) ? 4 : $length;
return bin2hex(random_bytes(($length - ($length % 2)) / 2));
}
#[Route(path: '/login-{type}', name: 'user_login')]
public function login(AuthenticationUtils $utils, $type = 'trainee'): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('homepage');
}
$error = $utils->getLastAuthenticationError();
$username = $utils->getLastUsername();
return $this->render('front/account/login.html.twig',[
'error' => $error,
'username' => $username,
'type' => $type
]);
}
#[Route(path: '/check-user-account', name: 'check_user_account')]
public function checkUserAccount()
{
/**
* @var User $user
*/
$user = $this->getUser();
if ($user instanceof Admin) {
return $this->redirectToRoute('user_logout');
}
if($user->getIsPro() && !$user->getHasAccess()){
$session = new Session();
$session->set('first-login', true);
if ($user instanceof Coach) {
$type = 'coach';
}
else if ($user instanceof Club) {
$type = 'club';
}
else if ($user instanceof Operator) {
$type = 'operator';
}
return $this->redirectToRoute('membership', [
'type' => $type
]);
}
return $this->redirectToRoute('homepage');
}
#[Route(path: '/logout', name: 'user_logout')]
public function logout() {}
#[Route(path: '/forgot-password', name: 'forgot_password')]
public function forgotPassword(UserRepository $userRepository, Request $request, ToUser $toUser)
{
$email = null;
if ($request->request->get('forgot_submit')) {
$email = $request->request->get('forgot_email');
$user = $userRepository->findOneBy(['email' => $email]);
if ($user) {
$toUser->forgotPassword($user);
$this->addFlash(
'success',
$this->translator->trans('flashes.account_controller.forgot_password_email_sent')
);
$email = null;
}
else {
$this->addFlash(
'success',
$this->translator->trans('flashes.account_controller.forgot_password_no_user')
);
}
return $this->redirectToRoute('forgot_password');
}
return $this->render('front/account/forgot-password.html.twig', [
'email' => $email
]);
}
#[Route(path: '/reset-password/{token}', name: 'reset_password')]
public function resetPassword($token, UserRepository $userRepository, UserPasswordHasherInterface $passwordHasher, Request $request, ObjectManager $manager)
{
$user = $userRepository->findOneBy(['token' => $token]);
$isAdmin = false;
if ($user instanceof Admin)
{
$isAdmin = true;
}
if ($user) {
if ($user->getIsPro()) {
$type = 'pro';
} else {
$type = 'trainee';
}
$action = 'reset';
$newPassword = new ResetPassword();
$form = $this->createForm(ResetPasswordType::class, $newPassword);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$hashed = $passwordHasher->hashPassword($user, $newPassword->getPassword());
$user->setPassword($hashed)
->setToken($this->generateString(32));
$manager->persist($user);
$manager->flush();
if($isAdmin)
{
return $this->redirectToRoute('admin_login');
}
else
{
$this->addFlash(
'success',
$this->translator->trans('flashes.account_controller.reset_password')
);
return $this->redirectToRoute('user_login', ['type' => $type]);
}
}
return $this->render('front/account/reset-password.html.twig', [
'action' => $action,
'form' => $form->createView()
]);
}
else {
$action = 'expired';
return $this->render('front/account/reset-password.html.twig', [
'action' => $action
]);
}
}
#[Route(path: '/confirm-your-email', name: 'must_confirm_email')]
public function mustConfirmEmail()
{
$user = $this->getUser();
$typeRegister = 'trainee';
if ($user->getIsPro()) {
$typeRegister = 'pro';
}
return $this->render('front/account/must-confirm-email.html.twig', [
'user' => $user,
'typeRegister' => $typeRegister
]);
}
#[Route(path: '/resend-email-confirmation', name: 'resend_email_confirmation')]
public function resendEmailConfirmation(ToTrainee $toTrainee, ToPro $toPro)
{
$user = $this->getUser();
if ($user->getIsPro()) {
$toPro->confirmEmail($user);
}
else {
$toTrainee->confirmEmail($user);
}
$this->addFlash(
'success',
$this->translator->trans('must_confirm_email.resend_done')
);
return $this->redirectToRoute('must_confirm_email');
}
}