src/Controller/Front/LuminousController.php line 76

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Knp\Component\Pager\PaginatorInterface;
  7. use App\Service\AppServices;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Contracts\Translation\TranslatorInterface;
  11. // Used for Login 
  12. use Symfony\Component\HttpFoundation\Session\Session;
  13. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  14. use Symfony\Component\Security\Core\Security;
  15. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  16. // Used for Sign Up 
  17. use FOS\UserBundle\Event\FilterUserResponseEvent;
  18. use FOS\UserBundle\Event\FormEvent;
  19. use FOS\UserBundle\Event\GetResponseUserEvent;
  20. use FOS\UserBundle\Form\Factory\FactoryInterface;
  21. use FOS\UserBundle\FOSUserEvents;
  22. use FOS\UserBundle\Model\UserInterface;
  23. use FOS\UserBundle\Model\UserManagerInterface;
  24. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  25. use Symfony\Component\HttpFoundation\RedirectResponse;
  26. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  27. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  28. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  29. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  30. use Doctrine\DBAL\Connection;
  31. use Doctrine\DBAL\Query\QueryBuilder;
  32. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  33. use App\Form\RegistrationType;
  34. use FOS\UserBundle\Util\TokenGeneratorInterface;
  35. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  36. use Twig\Environment;
  37. class LuminousController extends Controller {
  38.     // For Login
  39.     private $tokenManager;
  40.     // For Sign Up
  41.     private $eventDispatcher;
  42.     private $formFactory;
  43.     private $userManager;
  44.     private $tokenStorage;
  45.     private $services;
  46.     private $translator;
  47.     private $connection;
  48.     private $tokenGenerator;
  49.     protected $twig;
  50.     public function __construct(CsrfTokenManagerInterface $tokenManager nullFactoryInterface $formFactory,EventDispatcherInterface $eventDispatcher,  UserManagerInterface $userManagerTokenStorageInterface $tokenStorageAppServices $services,TranslatorInterface $translatorConnection $connection,TokenGeneratorInterface $tokenGenerator,Environment $twig) {
  51.         // For Login 
  52.         $this->tokenManager $tokenManager;
  53.         // For Sign Up 
  54.         $this->eventDispatcher $eventDispatcher;
  55.         $this->formFactory $formFactory;
  56.         $this->userManager $userManager;
  57.         $this->tokenStorage $tokenStorage;
  58.         $this->services $services;
  59.         $this->translator $translator;
  60.         $this->connection $connection;
  61.         $this->tokenGenerator $tokenGenerator;
  62.         $this->twig $twig;
  63.     }
  64.     /**
  65.      * @Route("/", name="ll_home")
  66.     */
  67.     
  68.     public function ll_home(Request $requestPaginatorInterface $paginatorTranslatorInterface $translator,AppServices $services) {
  69.         
  70.         $csrfToken $this->tokenManager $this->tokenManager->getToken('authenticate')->getValue() : null;
  71.         
  72.         return $this->render('Front/Luminous/home.html.twig',array(
  73.             'csrf_token' => $csrfToken,
  74.         ));
  75.         
  76.     }
  77.     /**
  78.      * @Route("/luminous/ll_signin", name="ll_signin")
  79.     */
  80.     public function ll_signin(Request $requestPaginatorInterface $paginatorAppServices $servicesTranslatorInterface $translator) {
  81.           
  82.             if ($this->isGranted("IS_AUTHENTICATED_REMEMBERED")) {
  83.                 return $this->redirectToRoute("dashboard_index");
  84.             }
  85.             /** @var $session Session */
  86.             $session $request->getSession();
  87.             $authErrorKey Security::AUTHENTICATION_ERROR;
  88.             $lastUsernameKey Security::LAST_USERNAME;
  89.             // get the error if any (works with forward and redirect -- see below)
  90.             if ($request->attributes->has($authErrorKey)) {
  91.                 $error $request->attributes->get($authErrorKey);
  92.             } elseif (null !== $session && $session->has($authErrorKey)) {
  93.                 $error $session->get($authErrorKey);
  94.                 $session->remove($authErrorKey);
  95.             } else {
  96.                 $error null;
  97.             }
  98.             if (!$error instanceof AuthenticationException) {
  99.                 $error null// The value does not come from the security component.
  100.             }
  101.             // last username entered by the user
  102.             $lastUsername = (null === $session) ? '' $session->get($lastUsernameKey);
  103.             $csrfToken $this->tokenManager $this->tokenManager->getToken('authenticate')->getValue() : null;
  104.             $data = array(
  105.                 'last_username' => $lastUsername,
  106.                 'error' => $error,
  107.                 'csrf_token' => $csrfToken
  108.             );
  109.         return $this->render('Front/Luminous/signin.html.twig',$data);
  110.     }
  111.     /**
  112.      * @Route("/luminous/signup/attendee", name="ll_signup_attendee")
  113.     */
  114.     public function ll_signup_attendee(Request $request,AppServices $servicesTranslatorInterface $translator ,Connection $connection, \Swift_Mailer $mailer) {
  115.         $user $this->userManager->createUser();
  116.         $user->setEnabled(true);
  117.         $form $this->createForm(RegistrationType::class, $user);
  118.         if ($this->isGranted("IS_AUTHENTICATED_REMEMBERED")) {
  119.             return $this->redirectToRoute("dashboard_index");
  120.         }
  121.     
  122.         $event = new GetResponseUserEvent($user$request);
  123.         $this->eventDispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE$event);
  124.         if (null !== $event->getResponse()) {
  125.             return $event->getResponse();
  126.         }
  127.         $form->remove("organizer");
  128.         if ($this->services->getSetting("google_recaptcha_enabled") == "no") {
  129.             $form->remove("recaptcha");
  130.         }
  131.         $form->setData($user);
  132.         $form->handleRequest($request);
  133.         try {
  134.             if ($form->isSubmitted()) {
  135.                 // If Email Exist Mail Send Process by ll
  136.                 $emailTo $request->request->get('fos_user_registration_form')['email'] ;
  137.                 $userTo $request->request->get('fos_user_registration_form')['username'] ;
  138.                 $checkEmailExist $this->checkEmailExist($connection,$emailTo$userTo);
  139.                 if($checkEmailExist != null){
  140.                     $result $this->sendMailToExistsUser($request,$emailTo,$userTo,$services,$mailer);
  141.                     if ($result == 'no') {
  142.                         $this->addFlash('danger'$translator->trans("The email could not be sent"));
  143.                     } else {
  144.                         return new RedirectResponse($this->generateUrl('ll_signup_attendee', array('username' => $result )));
  145.                     }
  146.                 }
  147.                 if ($form->isValid()) {
  148.                     $event = new FormEvent($form$request);
  149.                     $this->eventDispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS$event);
  150.                     $user->addRole('ROLE_ATTENDEE');
  151.                     $this->userManager->updateUser($user);
  152.                     if (null === $response $event->getResponse()) {
  153.                         $url $this->generateUrl('fos_user_registration_confirmed');
  154.                         $response = new RedirectResponse($url);
  155.                     }
  156.                     $this->eventDispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user$request$response));
  157.                     return $response;
  158.                 }
  159.                 $event = new FormEvent($form$request);
  160.                 $this->eventDispatcher->dispatch(FOSUserEvents::REGISTRATION_FAILURE$event);
  161.                 if (null !== $response $event->getResponse()) {
  162.                     return $response;
  163.                 }
  164.             }
  165.         } catch (\Execption $ex) {
  166.             dd($ex->getMessage());
  167.         }
  168.         // $csrfToken = $this->tokenManager ? $this->tokenManager->getToken('authenticate')->getValue() : null;
  169.         $data = [
  170.             'form' => $form->createView(),
  171.         ];
  172.         return $this->render('Front/Luminous/attendee_signup.html.twig'$data);
  173.     }
  174.     /**
  175.      * @Route("/luminous/signup/organizer", name="ll_signup_organizer")
  176.     */
  177.     public function ll_signup_organizer(Request $requestPaginatorInterface $paginatorAppServices $servicesTranslatorInterface $translator ,Connection $connection, \Swift_Mailer $mailer) {
  178.         if ($this->isGranted("IS_AUTHENTICATED_REMEMBERED")) {
  179.             return $this->redirectToRoute("dashboard_index");
  180.         }
  181.         $user $this->userManager->createUser();
  182.         $user->setEnabled(true);
  183.         $event = new GetResponseUserEvent($user$request);
  184.         $this->eventDispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE$event);
  185.         if (null !== $event->getResponse()) {
  186.             return $event->getResponse();
  187.         }
  188.         $form $this->formFactory->createForm();
  189.         if ($this->services->getSetting("google_recaptcha_enabled") == "no") {
  190.             $form->remove("recaptcha");
  191.         }
  192.         
  193.         $form->setData($user);
  194.         $form->handleRequest($request);
  195.         if ($form->isSubmitted()) {
  196.             // If Email Exist Mail Send Process by ll
  197.             $emailTo $request->request->get('fos_user_registration_form')['email'] ;
  198.             $userTo $request->request->get('fos_user_registration_form')['username'] ;
  199.             $checkEmailExist $this->checkEmailExist($connection,$emailTo$userTo);
  200.             if($checkEmailExist != null){
  201.                 $result $this->sendMailToExistsUser($request,$emailTo,$userTo,$services,$mailer);
  202.                 if ($result == 'no') {
  203.                     $this->addFlash('danger'$translator->trans("The email could not be sent"));
  204.                 } else {
  205.                     return new RedirectResponse($this->generateUrl('fos_user_resetting_check_email', array('username' => $result )));
  206.                 }
  207.             }
  208.             if ($form->isValid()) {
  209.                 $event = new FormEvent($form$request);
  210.                 $this->eventDispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS$event);
  211.                 $user->addRole('ROLE_ORGANIZER');
  212.                 $user->getOrganizer()->setUser($user);
  213.                 $this->userManager->updateUser($user);
  214.                 if (null === $response $event->getResponse()) {
  215.                     $url $this->generateUrl('fos_user_registration_confirmed');
  216.                     $response = new RedirectResponse($url);
  217.                 }
  218.                 $this->eventDispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user$request$response));
  219.                 return $response;
  220.             }
  221.             $event = new FormEvent($form$request);
  222.             $this->eventDispatcher->dispatch(FOSUserEvents::REGISTRATION_FAILURE$event);
  223.             if (null !== $response $event->getResponse()) {
  224.                 return $response;
  225.             }
  226.         }
  227.         
  228.         return $this->render('Front/Luminous/organizer_signup.html.twig', array(
  229.                     'form' => $form->createView(),
  230.         ));
  231.     }
  232.     public function sendMailToExistsUser($request,$emailTo,$userTo,$services,$mailer){
  233.             $user $this->userManager->findUserByUsernameOrEmail($emailTo);
  234.             $email_subject_title "Reset Password";
  235.             if($user == null){
  236.                 $user $this->userManager->findUserByUsernameOrEmail($userTo);
  237.                 if($user == null){
  238.                     $this->addFlash('error''Your account is disabled. please contact the administrator');
  239.                     return $this->redirectToRoute('ll_signup_attendee');
  240.                 }
  241.             }
  242.             $event = new GetResponseUserEvent($user$request);
  243.             if (null !== $event->getResponse()) {
  244.                 return $event->getResponse();
  245.             }
  246.             if (null == $user->getConfirmationToken()) {
  247.                 $token  $this->tokenGenerator->generateToken() ; 
  248.                 $user->setConfirmationToken($token);
  249.             }else{
  250.                 $token $user->getConfirmationToken();
  251.                 $user->setConfirmationToken($token);
  252.             }
  253.             $confirmationUrl $this->generateUrl('fos_user_resetting_reset', ['token' => $token ], UrlGeneratorInterface::ABSOLUTE_URL);
  254.             $userName $user->getUserName() ;
  255.             $user->setPasswordRequestedAt(new \DateTime());
  256.             $this->userManager->updateUser($user);
  257.             $context = [
  258.                 'user' => $user,
  259.                 'confirmationUrl' => $confirmationUrl,
  260.             ];
  261.             
  262.             $templatePath "bundles/FOSUserBundle/Resetting/email.html.twig";
  263.             $template $this->twig->load($templatePath);
  264.             
  265.             // Render the subject
  266.             $subject $template->renderBlock('subject'$context);
  267.             // Render the text body
  268.             $textBody $template->renderBlock('body_text'$context);
  269.             // Initialize the HTML body
  270.             $htmlBody '';
  271.             // Check if the template has an HTML block
  272.             if ($template->hasBlock('body_html'$context)) {
  273.                 $htmlBody $template->renderBlock('body_html'$context);
  274.             }
  275.             
  276.             $email = (new \Swift_Message($email_subject_title))
  277.                 ->setFrom($services->getSetting('no_reply_email'))
  278.                 ->setSubject($subject)
  279.                 ->setTo($emailTo);
  280.             
  281.             // Set the email body
  282.             if (!empty($htmlBody)) {
  283.                 $email->setBody($htmlBody'text/html')
  284.                       ->addPart($textBody'text/plain');
  285.             } else {
  286.                 $email->setBody($textBody'text/plain');
  287.             }
  288.             $result $mailer->send($email);
  289.             if($result == ){
  290.                 return "no" ;
  291.             }else{
  292.                 return $userName ;
  293.             }
  294.     }
  295.     // Email Check Method By LL 
  296.     public function checkEmailExist($connection,$email,$username){
  297.         
  298.         $queryBuilder = new QueryBuilder($connection);
  299.          
  300.         $queryBuilder
  301.                 ->select('eventic_user.id')
  302.                 ->from('eventic_user')
  303.                 ->where('email = :email')
  304.                 ->orWhere('username = :username')
  305.                 ->setParameters(['email' => $email'username' => $username]);
  306.         $statement $queryBuilder->execute();
  307.         $resultElement $statement->fetchColumn();
  308.         if($resultElement != null){
  309.             $this->addFlash('error''Your username or email already exists');
  310.             return $resultElement ;
  311.         }else return null ;
  312.     }
  313.     /**
  314.      * @Route("/luminous/forget-password", name="ll_forget_password")
  315.     */
  316.     public function ll_forget_password(Request $requestPaginatorInterface $paginatorAppServices $servicesTranslatorInterface $translator) {
  317.         return $this->render('Front/Luminous/forget_password.html.twig');
  318.     }
  319.      public function checkEmailAction(Request $request) {
  320.         $email $request->getSession()->get('fos_user_send_confirmation_email/email');
  321.         if (empty($email)) {
  322.             return new RedirectResponse($this->generateUrl('ll_home'));
  323.         }
  324.         $request->getSession()->remove('fos_user_send_confirmation_email/email');
  325.         $user $this->userManager->findUserByEmail($email);
  326.         if (null === $user) {
  327.             return new RedirectResponse($this->container->get('router')->generate('ll_signin'));
  328.         }
  329.         return $this->render('Front/Luminous/check_email.html.twig', array(
  330.             'user' => $user,
  331.         ));
  332.     }
  333.     /**
  334.      * @Route("/luminous/set_password", name="set_password")
  335.     */
  336.     public function setPassword(Request $request) {
  337.         $ud $_REQUEST['ud'] ?? null;
  338.         $password $_REQUEST['_password'] ?? null;
  339.         $confirm_password $_REQUEST['_confirm-password'] ?? null;
  340.         if($ud != null){
  341.     
  342.           $sql3 "SELECT * FROM eventic_user WHERE slug = :slug";
  343.             $params3 = ['slug' => $ud];
  344.             $statement3 $this->connection->prepare($sql3);
  345.             $statement3->execute($params3);
  346.             $user $statement3->fetch();
  347.     
  348.             if ($user) {
  349.                 $user $this->userManager->findUserByEmail($user['email']);
  350.                 $user->setEnabled(true);
  351.                 if ($password !== null && $confirm_password !== null && $password === $confirm_password) {
  352.                     $user->setPassword($password);
  353.                     $user->setPlainPassword($password);
  354.                     $this->userManager->updateUser($user);
  355.                     $token = new UsernamePasswordToken($usernull'main'$user->getRoles());
  356.                     $this->container->get('security.token_storage')->setToken($token);
  357.                     $this->addFlash('success'$this->translator->trans('Password Setup successfully!'));
  358.                     return $this->redirectToRoute('dashboard_index');
  359.                 } else {
  360.                     $this->addFlash('error'$this->translator->trans('Passwords do not match!'));
  361.                     return $this->redirect($request->headers->get('referer'));
  362.                 }
  363.             } else {
  364.                 $this->addFlash('error'$this->translator->trans('Undefined User!!!'));
  365.                 return $this->redirect($request->headers->get('referer'));
  366.             }  
  367.         }
  368.         return $this->redirectToRoute('dashboard_index');
  369.     }
  370. }