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

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 $userService, AuthenticationUtils $authenticationUtils, EventDispatcherInterface $eventDispatcher, TranslatorInterface $translator, ReCaptchaService $recaptchaService, KernelInterface $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. return $this->redirect($this->generateUrl('helpdesk_member_dashboard'));
  64. }
  65. public function logout(Request $request)
  66. {
  67. return;
  68. }
  69. public function forgotPassword(Request $request)
  70. {
  71. $entityManager = $this->getDoctrine()->getManager();
  72. $recaptchaDetails = $this->recaptchaService->getRecaptchaDetails();
  73. if ($request->getMethod() == 'POST') {
  74. if ($recaptchaDetails && $recaptchaDetails->getIsActive() == true && $this->recaptchaService->getReCaptchaResponse($request->request->get('g-recaptcha-response'))) {
  75. $this->addFlash('warning', $this->translator->trans("Warning ! Please select correct CAPTCHA !"));
  76. } else {
  77. $user = new User();
  78. $form = $this->createFormBuilder($user,['csrf_protection' => false])
  79. ->add('email',EmailType::class)
  80. ->getForm()
  81. ;
  82. $form->submit(['email' => $request->request->get('forgot_password_form')['email']]);
  83. $form->handleRequest($request);
  84. if ($form->isValid()) {
  85. $repository = $this->getDoctrine()->getRepository(User::class);
  86. $user = $entityManager->getRepository(User::class)->findOneByEmail($form->getData()->getEmail());
  87. if (!empty($user)) {
  88. // Trigger agent forgot password event
  89. $event = new CoreWorkflowEvents\User\ForgotPassword();
  90. $event
  91. ->setUser($user)
  92. ;
  93. $this->eventDispatcher->dispatch($event, 'uvdesk.automation.workflow.execute');
  94. $this->addFlash('success', $this->translator->trans('Please check your mail for password update'));
  95. return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  96. } else {
  97. $this->addFlash('warning', $this->translator->trans('This email address is not registered with us'));
  98. }
  99. }
  100. }
  101. }
  102. return $this->render("@UVDeskCoreFramework//forgotPassword.html.twig");
  103. }
  104. public function updateCredentials($email, $verificationCode, Request $request, UserPasswordEncoderInterface $encoder)
  105. {
  106. $entityManager = $this->getDoctrine()->getManager();
  107. $user = $entityManager->getRepository(User::class)->findOneByEmail($email);
  108. $lastupdatedInstance = $entityManager->getRepository(User::class)->LastupdatedRole($user);
  109. if (empty($user) || $user->getVerificationCode() != $verificationCode) {
  110. $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'));
  111. return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  112. }
  113. if ($request->getMethod() == 'POST') {
  114. $updatedCredentials = $request->request->all();
  115. if ($updatedCredentials['password'] === $updatedCredentials['confirmPassword']) {
  116. $user->setPassword($encoder->encodePassword($user, $updatedCredentials['password']));
  117. $user->setVerificationCode(TokenGenerator::generateToken());
  118. $entityManager->persist($user);
  119. $entityManager->flush();
  120. $this->addFlash('success', $this->translator->trans('Your password has been successfully updated. Login using updated password'));
  121. if($lastupdatedInstance[0]->getSupportRole()->getId() != 4){
  122. return $this->redirect($this->generateUrl('helpdesk_member_handle_login'));
  123. }else{
  124. return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  125. }
  126. } else {
  127. $this->addFlash('success', $this->translator->trans('Please try again, The passwords do not match'));
  128. }
  129. }
  130. return $this->render("@UVDeskCoreFramework//resetPassword.html.twig");
  131. }
  132. }