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

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\Component\Translation\TranslatorInterface;
  18. class Authentication extends AbstractController
  19. {
  20.     private $userService;
  21.     private $recaptchaService;
  22.     private $authenticationUtils;
  23.     private $eventDispatcher;
  24.     private $translator;
  25.     public function __construct(UserService $userServiceAuthenticationUtils $authenticationUtilsEventDispatcherInterface $eventDispatcherTranslatorInterface $translatorReCaptchaService $recaptchaService)
  26.     {
  27.         $this->userService $userService;
  28.         $this->recaptchaService $recaptchaService;
  29.         $this->authenticationUtils $authenticationUtils;
  30.         $this->eventDispatcher $eventDispatcher;
  31.         $this->translator $translator;
  32.     }
  33.     public function login(Request $request)
  34.     {
  35.         if (null == $this->userService->getSessionUser()) {
  36.             return $this->render('@UVDeskCoreFramework//login.html.twig', [
  37.                 'last_username' => $this->authenticationUtils->getLastUsername(),
  38.                 'error' => $this->authenticationUtils->getLastAuthenticationError(),
  39.             ]);
  40.         }
  41.         
  42.         return $this->redirect($this->generateUrl('helpdesk_member_dashboard'));
  43.     }
  44.     public function logout(Request $request)
  45.     {
  46.         return;
  47.     }
  48.     public function forgotPassword(Request $request)
  49.     {   
  50.         $entityManager $this->getDoctrine()->getManager();
  51.         $recaptchaDetails $this->recaptchaService->getRecaptchaDetails();
  52.         if ($request->getMethod() == 'POST') {
  53.             if ($recaptchaDetails && $recaptchaDetails->getIsActive() == true  && $this->recaptchaService->getReCaptchaResponse($request->request->get('g-recaptcha-response'))
  54.             ) {
  55.                 $this->addFlash('warning'$this->translator->trans("Warning ! Please select correct CAPTCHA !"));
  56.             } else {
  57.                 $user = new User();
  58.                 $form $this->createFormBuilder($user,['csrf_protection' => false])
  59.                         ->add('email',EmailType::class)
  60.                         ->getForm();
  61.                 $form->submit(['email' => $request->request->get('forgot_password_form')['email']]);
  62.                 $form->handleRequest($request);
  63.                 
  64.                 if ($form->isValid()) {
  65.                     $repository $this->getDoctrine()->getRepository('UVDeskCoreFrameworkBundle:User');
  66.                     $user $entityManager->getRepository('UVDeskCoreFrameworkBundle:User')->findOneByEmail($form->getData()->getEmail());
  67.                     if (!empty($user)) {
  68.                         // Trigger agent forgot password event
  69.                         $event = new GenericEvent(CoreWorkflowEvents\UserForgotPassword::getId(), [
  70.                             'entity' => $user,
  71.                         ]);
  72.                             
  73.                         $this->eventDispatcher->dispatch('uvdesk.automation.workflow.execute'$event);
  74.                         $this->addFlash('success'$this->translator->trans('Please check your mail for password update'));
  75.                         return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  76.                     } else {
  77.                         $this->addFlash('warning'$this->translator->trans('This email address is not registered with us'));
  78.                     }
  79.                 }
  80.             }
  81.         }
  82.             
  83.         return $this->render("@UVDeskCoreFramework//forgotPassword.html.twig");
  84.     }
  85.     public function updateCredentials($email$verificationCodeRequest $requestUserPasswordEncoderInterface $encoder)
  86.     {
  87.         $entityManager $this->getDoctrine()->getManager();
  88.         $user $entityManager->getRepository('UVDeskCoreFrameworkBundle:User')->findOneByEmail($email);
  89.         if (empty($user) || $user->getVerificationCode() != $verificationCode) {
  90.             $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'));
  91.             return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  92.         }
  93.         
  94.         if ($request->getMethod() == 'POST') {
  95.             $updatedCredentials $request->request->all();
  96.             if ($updatedCredentials['password'] === $updatedCredentials['confirmPassword']) {
  97.                 $user->setPassword($encoder->encodePassword($user$updatedCredentials['password']));
  98.                 $user->setVerificationCode(TokenGenerator::generateToken());
  99.                 $entityManager->persist($user);
  100.                 $entityManager->flush();
  101.                 $this->addFlash('success'$this->translator->trans('Your password has been successfully updated. Login using updated password'));
  102.                 return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  103.             } else {
  104.                 $this->addFlash('success'$this->translator->trans('Please try again, The passwords do not match'));
  105.             }
  106.         }
  107.         return $this->render("@UVDeskCoreFramework//resetPassword.html.twig");
  108.     }
  109. }