<?php
/**
* Created by Elements.at New Media Solutions GmbH
*
*/
namespace App\Controller\TourOperator;
use App\Model\DataObject\TourOperatorUser;
use App\Service\B2B\ProfileService;
use App\Service\CDMUserManagerService;
use Carbon\Carbon;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* @Route("/{_locale}", name="tour-operator_")
*
* @package App\Controller\TourOperator
*/
class LoginController extends AbstractTourOperatorController
{
public function defaultAction(): Response
{
return $this->redirectToRoute('tour-operator_login');
}
/**
* @Route("/login", name="login")
*
* @param AuthenticationUtils $authenticationUtils
* @param TranslatorInterface $translator
*
* @return Response
*
* @package App\Controller\TourOperator
*/
public function loginAction(AuthenticationUtils $authenticationUtils, TranslatorInterface $translator): Response
{
if ($this->isGranted('ROLE_USER')) {
return $this->redirectToRoute('tour-operator_ticket_overview');
}
if ($this->isGranted('ROLE_USER_INACTIVE')) {
$this->addFlash('warning', $translator->trans('tourOperator.error.account-not-active'));
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
if ($lastUsername = $authenticationUtils->getLastUsername()) {
if ($user = TourOperatorUser::getByEmail($lastUsername, 1)) {
if ($user->getNeedsToSetPassword()) {
$needsPasswordReset = true;
$this->addFlash('warning', $translator->trans('tourOperator.login.password-reset-needed'));
}
}
}
return $this->render('tour-operator/login/login.html.twig', [
'error' => $error,
'needsPasswordReset' => $needsPasswordReset ?? false,
]);
}
/**
* @Route("/logout", name="logout")
*/
public function logoutAction(): Response
{
return $this->redirectToRoute('tourOperator_login');
}
/**
* @Route("/password-recovery", name="password_recovery")
*/
public function passwordRecoveryAction(Request $request, ProfileService $profileService, TranslatorInterface $translator): Response
{
$returnData = [];
if ($request->isMethod('POST') && $request->get('phone') == '') {
$params['email'] = $request->get('email');
if (filter_var($params['email'], FILTER_VALIDATE_EMAIL) !== false) {
if ($user = $profileService->userExists($params['email'])) {
if (!$user->getActive()) {
$this->addFlash('error', $translator->trans('tourOperator.error.account-not-active'));
} else {
$emailDoc = $this->document->getProperty('tourOperatorRecoveryEmail');
if ($profileService->sendRecoveryMail($params['email'], $emailDoc)) {
$this->addFlash('success', $translator->trans('tourOperator.success.recovery-email'));
return $this->redirectToRoute('tourOperator_login');
} else {
$this->addFlash('error', $translator->trans('tourOperator.error.recovery-email'));
}
}
} else {
$this->addFlash('error', $translator->trans('tourOperator.error.user-not-found'));
}
} else {
$this->addFlash('error', $translator->trans('tourOperator.error.email-not-valid'));
}
}
return $this->render('tour-operator/login/send-recovery.html.twig', $returnData);
}
/**
* @Route("/password-change-recovery", name="password_change_recovery")
*/
public function changePasswordAction(
Request $request,
ProfileService $profileService,
TranslatorInterface $translator,
CDMUserManagerService $userManagerService
): Response {
$token = $request->get('token');
if ($request->isMethod('POST') && $request->get('mobile') == '') {
$user = $profileService->getChangePasswordTourOperatorUser($token);
$validToken = false;
if ($user instanceof TourOperatorUser) {
$hashbase = $user->getId() . $user->getEmail() . $user->getRecoveryDate()->getTimestamp();
$hash = md5($hashbase);
if ($token == $hash && $token == $user->getRecoverToken() && $user->getRecoveryDate()->greaterThan(Carbon::now()->subMinutes(30))) {
$validToken = true;
}
if ($validToken) {
$returnData = $userManagerService->handleChangePasswordPreRequest($request);
if (empty($returnData['errors'])) {
$user->setPassword($request->get('pwnew'));
$user->setPasswordChangeDate(Carbon::now());
$user->setRecoverToken(null);
if ($user->getNeedsToSetPassword()) {
$user->setNeedsToSetPassword(false);
}
try {
$user->save();
$this->addFlash('success', $translator->trans('tourOperator.success.change-password'));
return $this->redirectToRoute('tourOperator_login');
} catch (\Exception $e) {
$this->addFlash('error', $translator->trans('tourOperator.error.saving-user'));
}
} else {
$this->addFlash('error', $translator->trans('tourOperator.error.passwords-do-not-match'));
}
} else {
$this->addFlash('error', $translator->trans('tourOperator.error.token-not-valid'));
}
}
}
return $this->render('tourOperator/login/change-password.html.twig');
}
}