vendor/uvdesk/core-framework/Controller/Authentication.php line 66

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\CoreFrameworkBundle\Controller;
  3. use Symfony\Component\Form\FormError;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Webkul\UVDesk\CoreFrameworkBundle\Entity\User;
  7. use Symfony\Component\EventDispatcher\GenericEvent;
  8. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Webkul\UVDesk\CoreFrameworkBundle\Utils\TokenGenerator;
  11. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  12. use Webkul\UVDesk\CoreFrameworkBundle\Workflow\Events as CoreWorkflowEvents;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. use Webkul\UVDesk\CoreFrameworkBundle\Services\UserService;
  15. use Webkul\UVDesk\CoreFrameworkBundle\Services\ReCaptchaService;
  16. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. use Symfony\Component\HttpKernel\KernelInterface;
  19. class Authentication extends AbstractController
  20. {
  21.     private $userService;
  22.     private $recaptchaService;
  23.     private $authenticationUtils;
  24.     private $eventDispatcher;
  25.     private $translator;
  26.     private $kernel;
  27.     public function __construct(UserService $userServiceAuthenticationUtils $authenticationUtilsEventDispatcherInterface $eventDispatcherTranslatorInterface $translatorReCaptchaService $recaptchaServiceKernelInterface $kernel)
  28.     {
  29.         $this->userService $userService;
  30.         $this->recaptchaService $recaptchaService;
  31.         $this->authenticationUtils $authenticationUtils;
  32.         $this->eventDispatcher $eventDispatcher;
  33.         $this->translator $translator;
  34.         $this->kernel $kernel;
  35.     }
  36.     public function clearProjectCache(Request $request)
  37.     {
  38.         if (true === $request->isXmlHttpRequest()) {
  39.             $output = array();
  40.             $projectDir $this->kernel->getProjectDir();
  41.             $output shell_exec('php '.$projectDir.'/bin/console cache:clear');
  42.             $processId = (int) $output[0];
  43.             $responseContent = [
  44.                 'alertClass' => 'success',
  45.                 'alertMessage' => $this->translator->trans('Success ! Project cache cleared successfully.')
  46.             ];
  47.             return new Response(json_encode($responseContent), 200, ['Content-Type' => 'application/json']);
  48.         }
  49.         $responseContent = [
  50.             'alertClass' => 'warning',
  51.             'alertMessage' => $this->translator->trans('Error! Something went wrong.')
  52.         ];
  53.         return new Response(json_encode($responseContent), 404, ['Content-Type' => 'application/json']);
  54.     }
  55.     public function login(Request $request)
  56.     {
  57.         if (null == $this->userService->getSessionUser()) {
  58.             return $this->render('@UVDeskCoreFramework//login.html.twig', [
  59.                 'last_username' => $this->authenticationUtils->getLastUsername(),
  60.                 'error' => $this->authenticationUtils->getLastAuthenticationError(),
  61.             ]);
  62.         }
  63.         
  64.         return $this->redirect($this->generateUrl('helpdesk_member_dashboard'));
  65.     }
  66.     public function logout(Request $request)
  67.     {
  68.         return;
  69.     }
  70.     public function forgotPassword(Request $request)
  71.     {   
  72.         $entityManager $this->getDoctrine()->getManager();
  73.         $recaptchaDetails $this->recaptchaService->getRecaptchaDetails();
  74.         if ($request->getMethod() == 'POST') {
  75.             if ($recaptchaDetails && $recaptchaDetails->getIsActive() == true  && $this->recaptchaService->getReCaptchaResponse($request->request->get('g-recaptcha-response'))) {
  76.                 $this->addFlash('warning'$this->translator->trans("Warning ! Please select correct CAPTCHA !"));
  77.             } else {
  78.                 $user = new User();
  79.                 $form $this->createFormBuilder($user,['csrf_protection' => false])
  80.                     ->add('email',EmailType::class)
  81.                     ->getForm()
  82.                 ;
  83.                 $form->submit(['email' => $request->request->get('forgot_password_form')['email']]);
  84.                 $form->handleRequest($request);
  85.                 
  86.                 if ($form->isValid()) {
  87.                     $repository $this->getDoctrine()->getRepository(User::class);
  88.                     $user $entityManager->getRepository(User::class)->findOneByEmail($form->getData()->getEmail());
  89.                     if (!empty($user)) {
  90.                         // Trigger agent forgot password event
  91.                         $event = new CoreWorkflowEvents\User\ForgotPassword();
  92.                         $event
  93.                             ->setUser($user)
  94.                         ;
  95.                             
  96.                         $this->eventDispatcher->dispatch($event'uvdesk.automation.workflow.execute');
  97.                         $this->addFlash('success'$this->translator->trans('Please check your mail for password update'));
  98.                         return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  99.                     } else {
  100.                         $this->addFlash('warning'$this->translator->trans('This email address is not registered with us'));
  101.                     }
  102.                 }
  103.             }
  104.         }
  105.             
  106.         return $this->render("@UVDeskCoreFramework//forgotPassword.html.twig");
  107.     }
  108.     public function updateCredentials($email$verificationCodeRequest $requestUserPasswordEncoderInterface $encoder)
  109.     {
  110.         $entityManager $this->getDoctrine()->getManager();
  111.         $user $entityManager->getRepository(User::class)->findOneByEmail($email);
  112.         $lastupdatedInstance $entityManager->getRepository(User::class)->LastupdatedRole($user);
  113.         
  114.         if (empty($user) || $user->getVerificationCode() != $verificationCode) {
  115.             $this->addFlash('success'$this->translator->trans('You have already update password using this link if you wish to change password again click on forget password link here from login page'));
  116.             return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  117.         }
  118.         if ($request->getMethod() == 'POST') {
  119.             $updatedCredentials $request->request->all();
  120.             if ($updatedCredentials['password'] === $updatedCredentials['confirmPassword']) {
  121.                 $user->setPassword($encoder->encodePassword($user$updatedCredentials['password']));
  122.                 $user->setVerificationCode(TokenGenerator::generateToken());
  123.                 $entityManager->persist($user);
  124.                 $entityManager->flush();
  125.                 $this->addFlash('success'$this->translator->trans('Your password has been successfully updated. Login using updated password'));
  126.               
  127.                 if($lastupdatedInstance[0]->getSupportRole()->getId() != 4){
  128.                     return $this->redirect($this->generateUrl('helpdesk_member_handle_login'));
  129.                 }else{
  130.                     return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  131.                 }
  132.             } else {
  133.                 $this->addFlash('success'$this->translator->trans('Please try again, The passwords do not match'));
  134.             }
  135.         }
  136.         return $this->render("@UVDeskCoreFramework//resetPassword.html.twig");
  137.     }
  138. }