src/Controller/FrontendBundle/PasswortForgetController.php line 49

Open in your IDE?
  1. <?php
  2. namespace App\Controller\FrontendBundle;
  3. use Psr\Log\LoggerInterface;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Session\Session;
  9. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  10. use Symfony\Component\Form\Extension\Core\Type\TextType;
  11. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  12. use App\BackendBundle\Mailer\Target\PasswortForgetConfirmation;
  13. use App\BackendBundle\Helper\CodeGeneratorHelper;
  14. use App\BackendBundle\Helper\MailerHelper;
  15. use App\BackendBundle\Helper\SiteTitleHelper;
  16. use App\BackendBundle\Helper\UserHelper;
  17. use App\Entity\User;
  18. use App\Entity\UserPasswordChange;
  19. /**
  20.  * Class PasswortForgetController
  21.  * @package FrontendBundle\Controller
  22.  */
  23. class PasswortForgetController extends AbstractController {
  24.     private EntityManagerInterface $em;
  25.     private CodeGeneratorHelper $codeGenerator;
  26.     private LoggerInterface $logger;
  27.     private MailerHelper $mailerHelper;
  28.     private SiteTitleHelper $siteTitleHelper;
  29.     private UserHelper $userHelper;
  30.     public function __construct(EntityManagerInterface $emMailerHelper $mailerhelperSiteTitleHelper $siteTitleHelper,
  31.             CodeGeneratorHelper $codegeneratorhelperUserHelper $userHelperLoggerInterface $logger) {
  32.         $this->em $em;
  33.         $this->codeGenerator $codegeneratorhelper;
  34.         $this->logger $logger;
  35.         $this->mailerHelper $mailerhelper;
  36.         $this->siteTitleHelper $siteTitleHelper;
  37.         $this->userHelper $userHelper;
  38.     }
  39.     /**
  40.      * @return \Symfony\Component\HttpFoundation\Response
  41.      * @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."}) 
  42.      */
  43.     public function passwortForgetAction(Request $requestSession $session) {
  44.         $this->siteTitleHelper->setTitleDescription($request);
  45.         if ($session->get('passwortForgetSendReady') == 'ok') {
  46.             return $this->redirectToRoute('forget_passwort_success');
  47.         }
  48.         $name 'Passwort vergessen';
  49.         $errorText '';
  50.         if ($request->isMethod('POST')) {
  51.             $userEmail $request->request->get('email');
  52.             $user $this->em->getRepository(User::class)->findOneBy(array('email' => $userEmail));
  53.             if ($user == null) {
  54.                 echo "error";
  55.                 $errorText 'Zur eingegebenen E-Mail Adresse wurde kein Benutzer gefunden';
  56.             } else {
  57.                 $this->logger->info("Password forget user found: " $user->getId());
  58.                 $session->set('passwortForgetSendReady''ok');
  59.                 $userPasswordChange = new UserPasswordChange();
  60.                 $userPasswordChange->setCodeUsed(false);
  61.                 $userPasswordChange->setUser($user);
  62.                 $code $this->codeGenerator->generateNewMailConfirmCode();
  63.                 $userPasswordChange->setCode($code);
  64.                 $userPasswordChange->setCreatedAt(new \DateTime());
  65.                 $this->em->persist($userPasswordChange);
  66.                 $this->em->flush();
  67.                 $this->sentUserPasswortForgetMail($user$userPasswordChange);
  68.                 $session->set('passwortForgetSendReady''ok');
  69.                 return $this->redirectToRoute('forget_passwort_success');
  70.             }
  71.         }
  72.         return $this->render('@frontend/login/passwort_forget.html.twig', array(
  73.                     'errorText' => $errorText,
  74.                     'name' => $name
  75.         ));
  76.     }
  77.     /**
  78.      * @return \Symfony\Component\HttpFoundation\Response
  79.      * @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."}) 
  80.      */
  81.     public function passwortForgetSuccessAction(Request $requestSession $session) {
  82.         $this->siteTitleHelper->setTitleDescription($request);
  83.         $session->remove('passwortForgetSendReady');
  84.         $name 'Sie erhalten in Kürze ein E-Mail, durch welches Sie ein neues Passwort hinterlegen können.';
  85.         //$name = 'Sie bekommen in Kürze eine E-Mail vom <b>berufsreise.at</b>-Team!';
  86.         $title 'Passwort vergessen';
  87.         $errorText '';
  88.         return $this->render('@frontend/login/passwort_forget_success.html.twig', array(
  89.                     'errorText' => $errorText,
  90.                     'name' => $name,
  91.                     'title' => $title
  92.         ));
  93.     }
  94.     /**
  95.      * @return \Symfony\Component\HttpFoundation\Response
  96.      * @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."}) 
  97.      */
  98.     public function passwortForgetNewAction(Request $requestSession $session) {
  99.         $this->siteTitleHelper->setTitleDescription($request);
  100.         if ($session->get('passwortForgetNewReady') == 'ok') {
  101.             return $this->redirectToRoute('forget_passwort_new_success');
  102.         }
  103.         if (empty($request->query->get('cp'))) {
  104.             $this->logger->info('Request cp not found');
  105.             return $this->redirectToRoute('forget_passwort_error');
  106.         }
  107.         $code $request->query->get('cp');
  108.         $this->logger->info("Code: $code");
  109.         $userPasswordChange $this->em->getRepository(UserPasswordChange::class)->findOneBy(array('code' => $code));
  110.         if ($userPasswordChange == null) {
  111.             $this->logger->info('Password change object not found');
  112.             return $this->redirectToRoute('forget_passwort_error');
  113.         }
  114.         $name 'Neues Passwort erstellen';
  115.         $errorText '';
  116.         if ($request->isMethod('POST')) {
  117.             $errorText $this->checkPasswords($request);
  118.             if (empty($errorText)) {
  119.                 $plainPassword $request->request->get('password1');
  120.                 $user $userPasswordChange->getUser();
  121.                 $encPassword $this->userHelper->getEncodedPassword($user$plainPassword);
  122.                 $user->setPassword($encPassword);
  123.                 $this->em->persist($user);
  124.                 $this->em->flush();
  125.                 $session->set('passwortForgetNewReady''ok');
  126.                 return $this->redirectToRoute('forget_passwort_new_success');
  127.             }
  128.         }
  129.         return $this->render('@frontend/login/passwort_forget_new.html.twig', array(
  130.                     'errorText' => $errorText,
  131.                     'name' => $name
  132.         ));
  133.     }
  134.     private function checkPasswords(Request $request) {
  135.         $password1 $request->request->get('password1');
  136.         $password2 $request->request->get('password2');
  137.         if (strlen($password1) < 6) {
  138.             return "Das Passwort muss mindestens 6 Zeichen lang sein";
  139.         }
  140.         if ($password1 != $password2) {
  141.             return "Die Passwörter stimmen nicht überein";
  142.         }
  143.         return "";
  144.     }
  145.     /**
  146.      * @return \Symfony\Component\HttpFoundation\Response
  147.      * @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."}) 
  148.      */
  149.     public function passwortForgetErrorAction(Request $request) {
  150.         $this->siteTitleHelper->setTitleDescription($request);
  151.         return $this->render('@frontend/login/passwort_forget_success.html.twig', array(
  152.                     'errorText' => '',
  153.                     'name' => 'Es ist ein Fehler aufgetreten',
  154.                     'title' => 'Fehler'
  155.         ));
  156.     }
  157.     /**
  158.      * @return \Symfony\Component\HttpFoundation\Response
  159.      * @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!"}) 
  160.      */
  161.     public function passwortForgetNewSuccessAction(Request $requestSession $session) {
  162.         $this->siteTitleHelper->setTitleDescription($request);
  163.         $session->remove('passwortForgetNewReady');
  164.         return $this->render('@frontend/login/passwort_forget_success.html.twig', array(
  165.                     'errorText' => '',
  166.                     'name' => 'Ihr Passwort wurde erfolgreich geändert.',
  167.                     'title' => 'Passwort erfolgreich geändert'
  168.         ));
  169.     }
  170.     private function sentUserPasswortForgetMail(User $userUserPasswordChange $userPasswordChange) {
  171.         $code $userPasswordChange->getCode();
  172.         $mailer $this->mailerHelper->getMailerRegisterEmail();
  173.         $options = array();
  174.         $options['user'] = $user;
  175.         $curPwConformationUrl $this->generateUrl('forget_passwort_new', array('cp' => $code));
  176.         $options['confirmationUrl'] = $curPwConformationUrl;
  177.         $target = new PasswortForgetConfirmation($options);
  178.         $this->mailerHelper->sendMailTemplate($mailer$target);
  179.     }
  180. }