src/Controller/Front/AccountController.php line 101

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\Club;
  4. use App\Entity\Admin;
  5. use App\Entity\Coach;
  6. use App\Entity\Operator;
  7. use App\Entity\ResetPassword;
  8. use App\Form\ResetPasswordType;
  9. use App\EmailNotification\ToPro;
  10. use App\EmailNotification\ToUser;
  11. use App\Repository\UserRepository;
  12. use App\EmailNotification\ToTrainee;
  13. use Doctrine\Persistence\ObjectManager;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Component\HttpFoundation\Session\Session;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  20. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  21. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  22. #[Route(path'/{_locale}')]
  23. class AccountController extends AbstractController
  24. {
  25.     private $translator;
  26.     public function __construct(TranslatorInterface $translator)
  27.     {
  28.         $this->translator $translator;
  29.     }
  30.     private function generateString(int $length 64)
  31.     {
  32.         $length = ($length 4) ? $length;
  33.         return bin2hex(random_bytes(($length - ($length 2)) / 2));
  34.     }
  35.     #[Route(path'/login-{type}'name'user_login')]
  36.     public function login(AuthenticationUtils $utils$type 'trainee'): Response
  37.     {
  38.         if ($this->getUser()) {
  39.             return $this->redirectToRoute('homepage');
  40.         }
  41.         $error $utils->getLastAuthenticationError();
  42.         $username $utils->getLastUsername();
  43.         return $this->render('front/account/login.html.twig',[
  44.             'error' => $error,
  45.             'username' => $username,
  46.             'type' => $type
  47.         ]);
  48.         
  49.     }
  50.     #[Route(path'/check-user-account'name'check_user_account')]
  51.     public function checkUserAccount()
  52.     {
  53.         /**
  54.          * @var User $user
  55.          */
  56.         $user $this->getUser();
  57.         if ($user instanceof Admin) {
  58.             return $this->redirectToRoute('user_logout');
  59.         }
  60.         if($user->getIsPro() && !$user->getHasAccess()){
  61.             $session = new Session();
  62.             $session->set('first-login'true);
  63.             if ($user instanceof Coach) {
  64.                 $type 'coach';
  65.             }
  66.             else if ($user instanceof Club) {
  67.                 $type 'club';
  68.             }
  69.             else if ($user instanceof Operator) {
  70.                 $type 'operator';
  71.             }
  72.             return $this->redirectToRoute('membership', [
  73.                 'type' => $type
  74.             ]);
  75.         }
  76.         return $this->redirectToRoute('homepage');
  77.     }
  78.     #[Route(path'/logout'name'user_logout')]
  79.     public function logout() {}
  80.     #[Route(path'/forgot-password'name'forgot_password')]
  81.     public function forgotPassword(UserRepository $userRepositoryRequest $requestToUser $toUser)
  82.     {
  83.         $email null;
  84.         if ($request->request->get('forgot_submit')) {
  85.             $email $request->request->get('forgot_email');
  86.             $user $userRepository->findOneBy(['email' => $email]);
  87.             if ($user) {
  88.                 $toUser->forgotPassword($user);
  89.                 $this->addFlash(
  90.                     'success',
  91.                     $this->translator->trans('flashes.account_controller.forgot_password_email_sent')
  92.                 );
  93.                 $email null;
  94.             }
  95.             else {
  96.                 $this->addFlash(
  97.                     'success',
  98.                     $this->translator->trans('flashes.account_controller.forgot_password_no_user')
  99.                 );                
  100.             }
  101.             return $this->redirectToRoute('forgot_password');
  102.         }
  103.         
  104.         return $this->render('front/account/forgot-password.html.twig', [
  105.             'email' => $email
  106.         ]);
  107.     }
  108.     #[Route(path'/reset-password/{token}'name'reset_password')]
  109.     public function resetPassword($tokenUserRepository $userRepositoryUserPasswordHasherInterface $passwordHasherRequest $requestObjectManager $manager)
  110.     {
  111.         $user $userRepository->findOneBy(['token' => $token]);
  112.         $isAdmin false;
  113.         if ($user instanceof Admin)
  114.         {
  115.             $isAdmin true;
  116.         }
  117.         if ($user) {
  118.             if ($user->getIsPro()) {
  119.                 $type 'pro';
  120.             } else {
  121.                 $type 'trainee';
  122.             }
  123.             $action 'reset';
  124.             $newPassword = new ResetPassword();
  125.             
  126.             $form $this->createForm(ResetPasswordType::class, $newPassword);
  127.     
  128.             $form->handleRequest($request);
  129.     
  130.             if ($form->isSubmitted() && $form->isValid()) {
  131.                 $hashed $passwordHasher->hashPassword($user$newPassword->getPassword());
  132.                 $user->setPassword($hashed)
  133.                     ->setToken($this->generateString(32));
  134.                 $manager->persist($user);
  135.                 $manager->flush();
  136.                 
  137.                 if($isAdmin)
  138.                 {
  139.                     return $this->redirectToRoute('admin_login');
  140.                 }
  141.                 else
  142.                 {
  143.                     $this->addFlash(
  144.                         'success',
  145.                         $this->translator->trans('flashes.account_controller.reset_password')
  146.                     );
  147.                     return $this->redirectToRoute('user_login', ['type' => $type]);
  148.                 }
  149.             }
  150.             return $this->render('front/account/reset-password.html.twig', [
  151.                 'action' => $action,
  152.                 'form' => $form->createView()
  153.             ]);
  154.         }
  155.          
  156.         else {
  157.             $action 'expired';
  158.             return $this->render('front/account/reset-password.html.twig', [
  159.                 'action' => $action
  160.             ]);
  161.         }
  162.     }
  163.     #[Route(path'/confirm-your-email'name'must_confirm_email')]
  164.     public function mustConfirmEmail()
  165.     {
  166.         $user $this->getUser();
  167.         $typeRegister 'trainee';
  168.         if ($user->getIsPro()) {
  169.             $typeRegister 'pro';
  170.         }
  171.         return $this->render('front/account/must-confirm-email.html.twig', [
  172.             'user' => $user,
  173.             'typeRegister' => $typeRegister
  174.         ]);
  175.     }
  176.     #[Route(path'/resend-email-confirmation'name'resend_email_confirmation')]
  177.     public function resendEmailConfirmation(ToTrainee $toTraineeToPro $toPro)
  178.     {
  179.         $user $this->getUser();
  180.         
  181.         if ($user->getIsPro()) {
  182.             $toPro->confirmEmail($user);
  183.         }
  184.         else {
  185.             $toTrainee->confirmEmail($user);
  186.         }
  187.         $this->addFlash(
  188.             'success',
  189.             $this->translator->trans('must_confirm_email.resend_done')
  190.         );
  191.         return $this->redirectToRoute('must_confirm_email');
  192.     }
  193. }