src/Controller/TourOperator/LoginController.php line 25

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by Elements.at New Media Solutions GmbH
  4.  *
  5.  */
  6. namespace App\Controller\TourOperator;
  7. use App\Model\DataObject\TourOperatorUser;
  8. use App\Service\B2B\ProfileService;
  9. use App\Service\CDMUserManagerService;
  10. use Carbon\Carbon;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. /**
  17.  * @Route("/{_locale}", name="tour-operator_")
  18.  *
  19.  * @package App\Controller\TourOperator
  20.  */
  21. class LoginController extends AbstractTourOperatorController
  22. {
  23.     public function defaultAction(): Response
  24.     {
  25.         return $this->redirectToRoute('tour-operator_login');
  26.     }
  27.     /**
  28.      * @Route("/login", name="login")
  29.      *
  30.      * @param AuthenticationUtils $authenticationUtils
  31.      * @param TranslatorInterface $translator
  32.      *
  33.      * @return Response
  34.      *
  35.      * @package App\Controller\TourOperator
  36.      */
  37.     public function loginAction(AuthenticationUtils $authenticationUtilsTranslatorInterface $translator): Response
  38.     {
  39.         if ($this->isGranted('ROLE_USER')) {
  40.             return $this->redirectToRoute('tour-operator_ticket_overview');
  41.         }
  42.         if ($this->isGranted('ROLE_USER_INACTIVE')) {
  43.             $this->addFlash('warning'$translator->trans('tourOperator.error.account-not-active'));
  44.         }
  45.         // get the login error if there is one
  46.         $error $authenticationUtils->getLastAuthenticationError();
  47.         // last username entered by the user
  48.         if ($lastUsername $authenticationUtils->getLastUsername()) {
  49.             if ($user TourOperatorUser::getByEmail($lastUsername1)) {
  50.                 if ($user->getNeedsToSetPassword()) {
  51.                     $needsPasswordReset true;
  52.                     $this->addFlash('warning'$translator->trans('tourOperator.login.password-reset-needed'));
  53.                 }
  54.             }
  55.         }
  56.         return $this->render('tour-operator/login/login.html.twig', [
  57.             'error' => $error,
  58.             'needsPasswordReset' => $needsPasswordReset ?? false,
  59.         ]);
  60.     }
  61.     /**
  62.      * @Route("/logout", name="logout")
  63.      */
  64.     public function logoutAction(): Response
  65.     {
  66.         return $this->redirectToRoute('tourOperator_login');
  67.     }
  68.     /**
  69.      * @Route("/password-recovery", name="password_recovery")
  70.      */
  71.     public function passwordRecoveryAction(Request $requestProfileService $profileServiceTranslatorInterface $translator): Response
  72.     {
  73.         $returnData = [];
  74.         if ($request->isMethod('POST') && $request->get('phone') == '') {
  75.             $params['email'] = $request->get('email');
  76.             if (filter_var($params['email'], FILTER_VALIDATE_EMAIL) !== false) {
  77.                 if ($user $profileService->userExists($params['email'])) {
  78.                     if (!$user->getActive()) {
  79.                         $this->addFlash('error'$translator->trans('tourOperator.error.account-not-active'));
  80.                     } else {
  81.                         $emailDoc $this->document->getProperty('tourOperatorRecoveryEmail');
  82.                         if ($profileService->sendRecoveryMail($params['email'], $emailDoc)) {
  83.                             $this->addFlash('success'$translator->trans('tourOperator.success.recovery-email'));
  84.                             return $this->redirectToRoute('tourOperator_login');
  85.                         } else {
  86.                             $this->addFlash('error'$translator->trans('tourOperator.error.recovery-email'));
  87.                         }
  88.                     }
  89.                 } else {
  90.                     $this->addFlash('error'$translator->trans('tourOperator.error.user-not-found'));
  91.                 }
  92.             } else {
  93.                 $this->addFlash('error'$translator->trans('tourOperator.error.email-not-valid'));
  94.             }
  95.         }
  96.         return $this->render('tour-operator/login/send-recovery.html.twig'$returnData);
  97.     }
  98.     /**
  99.      * @Route("/password-change-recovery", name="password_change_recovery")
  100.      */
  101.     public function changePasswordAction(
  102.         Request $request,
  103.         ProfileService $profileService,
  104.         TranslatorInterface $translator,
  105.         CDMUserManagerService $userManagerService
  106.     ): Response {
  107.         $token $request->get('token');
  108.         if ($request->isMethod('POST') && $request->get('mobile') == '') {
  109.             $user $profileService->getChangePasswordTourOperatorUser($token);
  110.             $validToken false;
  111.             if ($user instanceof TourOperatorUser) {
  112.                 $hashbase $user->getId() . $user->getEmail() . $user->getRecoveryDate()->getTimestamp();
  113.                 $hash md5($hashbase);
  114.                 if ($token == $hash && $token == $user->getRecoverToken() && $user->getRecoveryDate()->greaterThan(Carbon::now()->subMinutes(30))) {
  115.                     $validToken true;
  116.                 }
  117.                 if ($validToken) {
  118.                     $returnData $userManagerService->handleChangePasswordPreRequest($request);
  119.                     if (empty($returnData['errors'])) {
  120.                         $user->setPassword($request->get('pwnew'));
  121.                         $user->setPasswordChangeDate(Carbon::now());
  122.                         $user->setRecoverToken(null);
  123.                         if ($user->getNeedsToSetPassword()) {
  124.                             $user->setNeedsToSetPassword(false);
  125.                         }
  126.                         try {
  127.                             $user->save();
  128.                             $this->addFlash('success'$translator->trans('tourOperator.success.change-password'));
  129.                             return $this->redirectToRoute('tourOperator_login');
  130.                         } catch (\Exception $e) {
  131.                             $this->addFlash('error'$translator->trans('tourOperator.error.saving-user'));
  132.                         }
  133.                     } else {
  134.                         $this->addFlash('error'$translator->trans('tourOperator.error.passwords-do-not-match'));
  135.                     }
  136.                 } else {
  137.                     $this->addFlash('error'$translator->trans('tourOperator.error.token-not-valid'));
  138.                 }
  139.             }
  140.         }
  141.         return $this->render('tourOperator/login/change-password.html.twig');
  142.     }
  143. }