<?php
namespace App\Controller\FrontendBundle;
use Psr\Log\LoggerInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use App\BackendBundle\Mailer\Target\PasswortForgetConfirmation;
use App\BackendBundle\Helper\CodeGeneratorHelper;
use App\BackendBundle\Helper\MailerHelper;
use App\BackendBundle\Helper\SiteTitleHelper;
use App\BackendBundle\Helper\UserHelper;
use App\Entity\User;
use App\Entity\UserPasswordChange;
/**
* Class PasswortForgetController
* @package FrontendBundle\Controller
*/
class PasswortForgetController extends AbstractController {
private EntityManagerInterface $em;
private CodeGeneratorHelper $codeGenerator;
private LoggerInterface $logger;
private MailerHelper $mailerHelper;
private SiteTitleHelper $siteTitleHelper;
private UserHelper $userHelper;
public function __construct(EntityManagerInterface $em, MailerHelper $mailerhelper, SiteTitleHelper $siteTitleHelper,
CodeGeneratorHelper $codegeneratorhelper, UserHelper $userHelper, LoggerInterface $logger) {
$this->em = $em;
$this->codeGenerator = $codegeneratorhelper;
$this->logger = $logger;
$this->mailerHelper = $mailerhelper;
$this->siteTitleHelper = $siteTitleHelper;
$this->userHelper = $userHelper;
}
/**
* @return \Symfony\Component\HttpFoundation\Response
* @Route("/passwort_vergessen", name="forget_passwort", defaults={"title": "Passwort vergessen", "description": "Sie haben Ihr Passwort vergessen? Ändern Sie einfach und unkompliziert Ihre Zugangsdaten für berufsreise.at."})
*/
public function passwortForgetAction(Request $request, Session $session) {
$this->siteTitleHelper->setTitleDescription($request);
if ($session->get('passwortForgetSendReady') == 'ok') {
return $this->redirectToRoute('forget_passwort_success');
}
$name = 'Passwort vergessen';
$errorText = '';
if ($request->isMethod('POST')) {
$userEmail = $request->request->get('email');
$user = $this->em->getRepository(User::class)->findOneBy(array('email' => $userEmail));
if ($user == null) {
echo "error";
$errorText = 'Zur eingegebenen E-Mail Adresse wurde kein Benutzer gefunden';
} else {
$this->logger->info("Password forget user found: " . $user->getId());
$session->set('passwortForgetSendReady', 'ok');
$userPasswordChange = new UserPasswordChange();
$userPasswordChange->setCodeUsed(false);
$userPasswordChange->setUser($user);
$code = $this->codeGenerator->generateNewMailConfirmCode();
$userPasswordChange->setCode($code);
$userPasswordChange->setCreatedAt(new \DateTime());
$this->em->persist($userPasswordChange);
$this->em->flush();
$this->sentUserPasswortForgetMail($user, $userPasswordChange);
$session->set('passwortForgetSendReady', 'ok');
return $this->redirectToRoute('forget_passwort_success');
}
}
return $this->render('@frontend/login/passwort_forget.html.twig', array(
'errorText' => $errorText,
'name' => $name
));
}
/**
* @return \Symfony\Component\HttpFoundation\Response
* @Route("/passwort_vergessen_erfolg", name="forget_passwort_success", defaults={"title": "Passwort vergessen", "description": "Sie haben Ihr Passwort vergessen? Ändern Sie einfach und unkompliziert Ihre Zugangsdaten für berufsreise.at."})
*/
public function passwortForgetSuccessAction(Request $request, Session $session) {
$this->siteTitleHelper->setTitleDescription($request);
$session->remove('passwortForgetSendReady');
$name = 'Sie erhalten in Kürze ein E-Mail, durch welches Sie ein neues Passwort hinterlegen können.';
//$name = 'Sie bekommen in Kürze eine E-Mail vom <b>berufsreise.at</b>-Team!';
$title = 'Passwort vergessen';
$errorText = '';
return $this->render('@frontend/login/passwort_forget_success.html.twig', array(
'errorText' => $errorText,
'name' => $name,
'title' => $title
));
}
/**
* @return \Symfony\Component\HttpFoundation\Response
* @Route("/passwort_neu", name="forget_passwort_new", defaults={"title": "Passwort vergessen", "description": "Sie haben Ihr Passwort vergessen? Ändern Sie einfach und unkompliziert Ihre Zugangsdaten für berufsreise.at."})
*/
public function passwortForgetNewAction(Request $request, Session $session) {
$this->siteTitleHelper->setTitleDescription($request);
if ($session->get('passwortForgetNewReady') == 'ok') {
return $this->redirectToRoute('forget_passwort_new_success');
}
if (empty($request->query->get('cp'))) {
$this->logger->info('Request cp not found');
return $this->redirectToRoute('forget_passwort_error');
}
$code = $request->query->get('cp');
$this->logger->info("Code: $code");
$userPasswordChange = $this->em->getRepository(UserPasswordChange::class)->findOneBy(array('code' => $code));
if ($userPasswordChange == null) {
$this->logger->info('Password change object not found');
return $this->redirectToRoute('forget_passwort_error');
}
$name = 'Neues Passwort erstellen';
$errorText = '';
if ($request->isMethod('POST')) {
$errorText = $this->checkPasswords($request);
if (empty($errorText)) {
$plainPassword = $request->request->get('password1');
$user = $userPasswordChange->getUser();
$encPassword = $this->userHelper->getEncodedPassword($user, $plainPassword);
$user->setPassword($encPassword);
$this->em->persist($user);
$this->em->flush();
$session->set('passwortForgetNewReady', 'ok');
return $this->redirectToRoute('forget_passwort_new_success');
}
}
return $this->render('@frontend/login/passwort_forget_new.html.twig', array(
'errorText' => $errorText,
'name' => $name
));
}
private function checkPasswords(Request $request) {
$password1 = $request->request->get('password1');
$password2 = $request->request->get('password2');
if (strlen($password1) < 6) {
return "Das Passwort muss mindestens 6 Zeichen lang sein";
}
if ($password1 != $password2) {
return "Die Passwörter stimmen nicht überein";
}
return "";
}
/**
* @return \Symfony\Component\HttpFoundation\Response
* @Route("/passwort_vergessen_fehler", name="forget_passwort_error", defaults={"title": "Passwort vergessen", "description": "Sie haben Ihr Passwort vergessen? Ändern Sie einfach und unkompliziert Ihre Zugangsdaten für berufsreise.at."})
*/
public function passwortForgetErrorAction(Request $request) {
$this->siteTitleHelper->setTitleDescription($request);
return $this->render('@frontend/login/passwort_forget_success.html.twig', array(
'errorText' => '',
'name' => 'Es ist ein Fehler aufgetreten',
'title' => 'Fehler'
));
}
/**
* @return \Symfony\Component\HttpFoundation\Response
* @Route("/passwort_neu_erfolg", name="forget_passwort_new_success", defaults={"title": "Passwort erfolgreich geändert", "description": "berufsreise.at ist die größte Online-Plattform zum Thema Berufsorientierung in Tirol. Entdecke spannende Berufe, lerne Tiroler Unternehmen, BO-Institutionen und weiterführende Schulen kennen oder begleite Tiroler Lehrlinge und Schüler*innen in deren Ausbildungen!"})
*/
public function passwortForgetNewSuccessAction(Request $request, Session $session) {
$this->siteTitleHelper->setTitleDescription($request);
$session->remove('passwortForgetNewReady');
return $this->render('@frontend/login/passwort_forget_success.html.twig', array(
'errorText' => '',
'name' => 'Ihr Passwort wurde erfolgreich geändert.',
'title' => 'Passwort erfolgreich geändert'
));
}
private function sentUserPasswortForgetMail(User $user, UserPasswordChange $userPasswordChange) {
$code = $userPasswordChange->getCode();
$mailer = $this->mailerHelper->getMailerRegisterEmail();
$options = array();
$options['user'] = $user;
$curPwConformationUrl = $this->generateUrl('forget_passwort_new', array('cp' => $code));
$options['confirmationUrl'] = $curPwConformationUrl;
$target = new PasswortForgetConfirmation($options);
$this->mailerHelper->sendMailTemplate($mailer, $target);
}
}