src/Controller/FrontendBundle/PasswortForgetController.php line 50

Open in your IDE?
  1. <?php
  2. namespace App\Controller\FrontendBundle;
  3. use Psr\Log\LoggerInterface;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Session\Session;
  8. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  9. use Symfony\Component\Form\Extension\Core\Type\TextType;
  10. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  11. use App\BackendBundle\Mailer\Target\PasswortForgetConfirmation;
  12. use App\BackendBundle\Helper\CodeGeneratorHelper;
  13. use App\BackendBundle\Helper\MailerHelper;
  14. use App\BackendBundle\Helper\SiteTitleHelper;
  15. use App\BackendBundle\Helper\UserHelper;
  16. use App\Entity\User;
  17. use App\Entity\UserPasswordChange;
  18. /**
  19.  * Class PasswortForgetController
  20.  * @package FrontendBundle\Controller
  21.  */
  22. class PasswortForgetController extends AbstractController {
  23.     private $mailerHelper;
  24.     private $codeGenerator;
  25.     private $siteTitleHelper;
  26.     private $userHelper;
  27.     public function __construct(MailerHelper $mailerhelperSiteTitleHelper $siteTitleHelper,
  28.             CodeGeneratorHelper $codegeneratorhelperUserHelper $userHelper) {
  29.         $this->mailerHelper $mailerhelper;
  30.         $this->siteTitleHelper $siteTitleHelper;
  31.         $this->codeGenerator $codegeneratorhelper;
  32.         $this->userHelper=$userHelper;
  33.     }
  34.     /**
  35.      * @return \Symfony\Component\HttpFoundation\Response
  36.      * @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."}) 
  37.      */
  38.     public function passwortForgetAction(Request $requestLoggerInterface $logger) {
  39.         $this->siteTitleHelper->setTitleDescription($request);
  40.         /* @var $session Session */
  41.         $session $this->get('session');
  42.         if ($session->get('passwortForgetSendReady') == 'ok') {
  43.             return $this->redirectToRoute('forget_passwort_success');
  44.         }
  45.         $name 'Passwort vergessen';
  46.         $errorText '';
  47.         $form $this->createFormBuilder()
  48.                 ->add('username'TextType::class, array(
  49.                     'label' => 'E-Mail-Adresse eingeben',
  50.                     'required' => true))
  51.                 ->add('save'SubmitType::class, array('label' => 'Senden'))
  52.                 ->getForm();
  53.         $form->handleRequest($request);
  54.         if ($form->isSubmitted() && $form->isValid()) {
  55.             $userEmail $form->get('username')->getData();
  56.             $em $this->getDoctrine()->getManager();
  57.             $user $em->getRepository(User::class)->findOneBy(array('email' => $userEmail));
  58.             if ($user != null) {
  59.                 $logger->info("Password forget user found: " $user->getId());
  60.                 $session->set('passwortForgetSendReady''ok');
  61.                 $userPasswordChange = new UserPasswordChange();
  62.                 $userPasswordChange->setCodeUsed(false);
  63.                 $userPasswordChange->setUser($user);
  64.                 $code $this->codeGenerator->generateNewMailConfirmCode();
  65.                 $userPasswordChange->setCode($code);
  66.                 $userPasswordChange->setCreatedAt(new \DateTime());
  67.                 $em->persist($userPasswordChange);
  68.                 $em->flush();
  69.                 $this->sentUserPasswortForgetMail($user$userPasswordChange);
  70.                 $session->set('passwortForgetSendReady''ok');
  71.                 return $this->redirectToRoute('forget_passwort_success');
  72.             } else {
  73.                 $errorText 'Zur eingegebenen E-Mail Adresse wurde kein Benutzer gefunden';
  74.             }
  75.         }
  76.         return $this->render('@frontend/login/passwort_forget.html.twig', array(
  77.                     'errorText' => $errorText,
  78.                     'name' => $name,
  79.                     'form' => $form->createView()
  80.         ));
  81.     }
  82.     /**
  83.      * @return \Symfony\Component\HttpFoundation\Response
  84.      * @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."}) 
  85.      */
  86.     public function passwortForgetSuccessAction(Request $request) {
  87.         $this->siteTitleHelper->setTitleDescription($request);
  88.         $session $this->get('session');
  89.         $session->remove('passwortForgetSendReady');
  90.         $name 'Sie erhalten in Kürze eine E-Mail, wenn Sie bereits auf <b>berufsreise.at</b> registriert sind.';
  91.         //$name = 'Sie bekommen in Kürze eine E-Mail vom <b>berufsreise.at</b>-Team!';
  92.         $title 'Passwort vergessen';
  93.         $errorText '';
  94.         return $this->render('@frontend/login/passwort_forget_success.html.twig', array(
  95.                     'errorText' => $errorText,
  96.                     'name' => $name,
  97.                     'title' => $title
  98.         ));
  99.     }
  100.     /**
  101.      * @return \Symfony\Component\HttpFoundation\Response
  102.      * @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."}) 
  103.      */
  104.     public function passwortForgetNewAction(Request $requestLoggerInterface $logger) {
  105.         $this->siteTitleHelper->setTitleDescription($request);
  106.         $session $this->get('session');
  107.         if ($session->get('passwortForgetNewReady') == 'ok') {
  108.             return $this->redirectToRoute('forget_passwort_new_success');
  109.         }
  110.         if (empty($request->query->get('cp'))) {
  111.             $logger->info('Request cp not found');
  112.             return $this->redirectToRoute('forget_passwort_error');
  113.         }
  114.         $code $request->query->get('cp');
  115.         $logger->info("Code: $code");
  116.         $em $this->getDoctrine()->getManager();
  117.         $userPasswordChange $em->getRepository(UserPasswordChange::class)->findOneBy(array('code' => $code));
  118.         if ($userPasswordChange == null) {
  119.             $logger->info('Password change object not found');
  120.             return $this->redirectToRoute('forget_passwort_error');
  121.         }
  122.         $name 'Neues Passwort erstellen';
  123.         $errorText '';
  124.         $form $this->createFormBuilder()
  125.                 ->add('password_f'PasswordType::class, array('label' => 'Neues Passwort'))
  126.                 ->add('password_s'PasswordType::class, array('label' => 'Neues Passwort wiederholen'))
  127.                 ->add('save'SubmitType::class, array('label' => 'Speichern'))
  128.                 ->getForm();
  129.         $form->handleRequest($request);
  130.         if ($form->isSubmitted() && $form->isValid()) {
  131.             $errorFieldsCheck $this->checkPasswortForgetNewDataInput($form);
  132.             if (isset($errorFieldsCheck) && empty($errorFieldsCheck)) {
  133.                 $plainPassword $form->get('password_f')->getData();
  134.                 $user $userPasswordChange->getUser();
  135.                 $encPassword $this->userHelper->getEncodedPassword($user$plainPassword);
  136.                 $user->setPassword($encPassword);
  137.                 // TODO check if Keep Until Date is set
  138.                 $em->persist($user);
  139.                 $em->flush();
  140.                 $session->set('passwortForgetNewReady''ok');
  141.                 return $this->redirectToRoute('forget_passwort_new_success');
  142.             } else {
  143.                 $errorText $errorFieldsCheck;
  144.             }
  145.         }
  146.         return $this->render('@frontend/login/passwort_forget_new.html.twig', array(
  147.                     'errorText' => $errorText,
  148.                     'name' => $name,
  149.                     'form' => $form->createView()
  150.         ));
  151.     }
  152.     /**
  153.      * @return \Symfony\Component\HttpFoundation\Response
  154.      * @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."}) 
  155.      */
  156.     public function passwortForgetErrorAction(Request $request) {
  157.         $this->siteTitleHelper->setTitleDescription($request);
  158.         return $this->render('@frontend/login/passwort_forget_success.html.twig', array(
  159.                     'errorText' => '',
  160.                     'name' => 'Es ist ein Fehler aufgetreten',
  161.                     'title' => 'Fehler'
  162.         ));
  163.     }
  164.     /**
  165.      * @return \Symfony\Component\HttpFoundation\Response
  166.      * @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!"}) 
  167.      */
  168.     public function passwortForgetNewSuccessAction(Request $request) {
  169.         $this->siteTitleHelper->setTitleDescription($request);
  170.         $session $this->get('session');
  171.         $session->remove('passwortForgetNewReady');
  172.         return $this->render('@frontend/login/passwort_forget_success.html.twig', array(
  173.                     'errorText' => '',
  174.                     'name' => 'Ihr Passwort wurde erfolgreich geändert.',
  175.                     'title' => 'Passwort erfolgreich geändert'
  176.         ));
  177.     }
  178.     /* @var $user User */
  179.     /* @var $userPasswordChange UserPasswordChange */
  180.     private function sentUserPasswortForgetMail($user$userPasswordChange) {
  181.         $code $userPasswordChange->getCode();
  182.         $mailer $this->mailerHelper->getMailerRegisterEmail();
  183.         $options = array();
  184.         $options['user'] = $user;
  185.         $curPwConformationUrl $this->generateUrl('forget_passwort_new', array('cp' => $code));
  186.         $options['confirmationUrl'] = $curPwConformationUrl;
  187.         $target = new PasswortForgetConfirmation($options);
  188.         $this->mailerHelper->sendMailTemplate($mailer$target);
  189.     }
  190.     // *************************************************************************
  191.     // Functions - Passwort Forget Check Data Input
  192.     // *************************************************************************
  193.     private function checkPasswortForgetNewDataInput($form) {
  194.         $errorText '';
  195.         if ($form->get('password_f')->getData() == '') {
  196.             $errorText .= 'Bitte geben Sie ein Passwort ein.<br />';
  197.         }
  198.         if ($form->get('password_s')->getData() == '') {
  199.             $errorText .= 'Bitte wiederholen Sie Ihr Passwort.<br />';
  200.         }
  201.         if ($form->get('password_f')->getData() != '' && $form->get('password_s')->getData() != '') {
  202.             if ($form->get('password_f')->getData() != $form->get('password_s')->getData()) {
  203.                 $errorText .= 'Die 2 eingegebenen Passwörter stimmen nicht überein.<br />';
  204.             }
  205.         }
  206.         return $errorText;
  207.     }
  208.     // *************************************************************************
  209. }