<?php
namespace App\Controller\FrontendBundle;
use Psr\Log\LoggerInterface;
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 $mailerHelper;
private $codeGenerator;
private $siteTitleHelper;
private $userHelper;
public function __construct(MailerHelper $mailerhelper, SiteTitleHelper $siteTitleHelper,
CodeGeneratorHelper $codegeneratorhelper, UserHelper $userHelper) {
$this->mailerHelper = $mailerhelper;
$this->siteTitleHelper = $siteTitleHelper;
$this->codeGenerator = $codegeneratorhelper;
$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, LoggerInterface $logger) {
$this->siteTitleHelper->setTitleDescription($request);
/* @var $session Session */
$session = $this->get('session');
if ($session->get('passwortForgetSendReady') == 'ok') {
return $this->redirectToRoute('forget_passwort_success');
}
$name = 'Passwort vergessen';
$errorText = '';
$form = $this->createFormBuilder()
->add('username', TextType::class, array(
'label' => 'E-Mail-Adresse eingeben',
'required' => true))
->add('save', SubmitType::class, array('label' => 'Senden'))
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$userEmail = $form->get('username')->getData();
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository(User::class)->findOneBy(array('email' => $userEmail));
if ($user != null) {
$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());
$em->persist($userPasswordChange);
$em->flush();
$this->sentUserPasswortForgetMail($user, $userPasswordChange);
$session->set('passwortForgetSendReady', 'ok');
return $this->redirectToRoute('forget_passwort_success');
} else {
$errorText = 'Zur eingegebenen E-Mail Adresse wurde kein Benutzer gefunden';
}
}
return $this->render('@frontend/login/passwort_forget.html.twig', array(
'errorText' => $errorText,
'name' => $name,
'form' => $form->createView()
));
}
/**
* @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) {
$this->siteTitleHelper->setTitleDescription($request);
$session = $this->get('session');
$session->remove('passwortForgetSendReady');
$name = 'Sie erhalten in Kürze eine E-Mail, wenn Sie bereits auf <b>berufsreise.at</b> registriert sind.';
//$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, LoggerInterface $logger) {
$this->siteTitleHelper->setTitleDescription($request);
$session = $this->get('session');
if ($session->get('passwortForgetNewReady') == 'ok') {
return $this->redirectToRoute('forget_passwort_new_success');
}
if (empty($request->query->get('cp'))) {
$logger->info('Request cp not found');
return $this->redirectToRoute('forget_passwort_error');
}
$code = $request->query->get('cp');
$logger->info("Code: $code");
$em = $this->getDoctrine()->getManager();
$userPasswordChange = $em->getRepository(UserPasswordChange::class)->findOneBy(array('code' => $code));
if ($userPasswordChange == null) {
$logger->info('Password change object not found');
return $this->redirectToRoute('forget_passwort_error');
}
$name = 'Neues Passwort erstellen';
$errorText = '';
$form = $this->createFormBuilder()
->add('password_f', PasswordType::class, array('label' => 'Neues Passwort'))
->add('password_s', PasswordType::class, array('label' => 'Neues Passwort wiederholen'))
->add('save', SubmitType::class, array('label' => 'Speichern'))
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$errorFieldsCheck = $this->checkPasswortForgetNewDataInput($form);
if (isset($errorFieldsCheck) && empty($errorFieldsCheck)) {
$plainPassword = $form->get('password_f')->getData();
$user = $userPasswordChange->getUser();
$encPassword = $this->userHelper->getEncodedPassword($user, $plainPassword);
$user->setPassword($encPassword);
// TODO check if Keep Until Date is set
$em->persist($user);
$em->flush();
$session->set('passwortForgetNewReady', 'ok');
return $this->redirectToRoute('forget_passwort_new_success');
} else {
$errorText = $errorFieldsCheck;
}
}
return $this->render('@frontend/login/passwort_forget_new.html.twig', array(
'errorText' => $errorText,
'name' => $name,
'form' => $form->createView()
));
}
/**
* @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) {
$this->siteTitleHelper->setTitleDescription($request);
$session = $this->get('session');
$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'
));
}
/* @var $user User */
/* @var $userPasswordChange UserPasswordChange */
private function sentUserPasswortForgetMail($user, $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);
}
// *************************************************************************
// Functions - Passwort Forget Check Data Input
// *************************************************************************
private function checkPasswortForgetNewDataInput($form) {
$errorText = '';
if ($form->get('password_f')->getData() == '') {
$errorText .= 'Bitte geben Sie ein Passwort ein.<br />';
}
if ($form->get('password_s')->getData() == '') {
$errorText .= 'Bitte wiederholen Sie Ihr Passwort.<br />';
}
if ($form->get('password_f')->getData() != '' && $form->get('password_s')->getData() != '') {
if ($form->get('password_f')->getData() != $form->get('password_s')->getData()) {
$errorText .= 'Die 2 eingegebenen Passwörter stimmen nicht überein.<br />';
}
}
return $errorText;
}
// *************************************************************************
}