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

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