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

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.             ) {
  77.                 $this->addFlash('warning'$this->translator->trans("Warning ! Please select correct CAPTCHA !"));
  78.             } else {
  79.                 $user = new User();
  80.                 $form $this->createFormBuilder($user,['csrf_protection' => false])
  81.                         ->add('email',EmailType::class)
  82.                         ->getForm();
  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('UVDeskCoreFrameworkBundle:User');
  88.                     $user $entityManager->getRepository('UVDeskCoreFrameworkBundle:User')->findOneByEmail($form->getData()->getEmail());
  89.                     if (!empty($user)) {
  90.                         // Trigger agent forgot password event
  91.                         $event = new GenericEvent(CoreWorkflowEvents\UserForgotPassword::getId(), [
  92.                             'entity' => $user,
  93.                         ]);
  94.                             
  95.                         $this->eventDispatcher->dispatch($event'uvdesk.automation.workflow.execute');
  96.                         $this->addFlash('success'$this->translator->trans('Please check your mail for password update'));
  97.                         return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  98.                     } else {
  99.                         $this->addFlash('warning'$this->translator->trans('This email address is not registered with us'));
  100.                     }
  101.                 }
  102.             }
  103.         }
  104.             
  105.         return $this->render("@UVDeskCoreFramework//forgotPassword.html.twig");
  106.     }
  107.     public function updateCredentials($email$verificationCodeRequest $requestUserPasswordEncoderInterface $encoder)
  108.     {
  109.         $entityManager $this->getDoctrine()->getManager();
  110.         $user $entityManager->getRepository('UVDeskCoreFrameworkBundle:User')->findOneByEmail($email);
  111.         $lastupdatedInstance $entityManager->getRepository('UVDeskCoreFrameworkBundle:User')->LastupdatedRole($user);
  112.         
  113.         if (empty($user) || $user->getVerificationCode() != $verificationCode) {
  114.             $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'));
  115.             return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  116.         }
  117.         if ($request->getMethod() == 'POST') {
  118.             $updatedCredentials $request->request->all();
  119.             if ($updatedCredentials['password'] === $updatedCredentials['confirmPassword']) {
  120.                 $user->setPassword($encoder->encodePassword($user$updatedCredentials['password']));
  121.                 $user->setVerificationCode(TokenGenerator::generateToken());
  122.                 $entityManager->persist($user);
  123.                 $entityManager->flush();
  124.                 $this->addFlash('success'$this->translator->trans('Your password has been successfully updated. Login using updated password'));
  125.               
  126.                 if($lastupdatedInstance[0]->getSupportRole()->getId() != 4){
  127.                     return $this->redirect($this->generateUrl('helpdesk_member_handle_login'));
  128.                 }else{
  129.                     return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  130.                 }
  131.             } else {
  132.                 $this->addFlash('success'$this->translator->trans('Please try again, The passwords do not match'));
  133.             }
  134.         }
  135.         return $this->render("@UVDeskCoreFramework//resetPassword.html.twig");
  136.     }
  137. }