src/Controller/DefaultController.php line 32

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * ImmoBay - BAUR Immobilien
  5.  *
  6.  * @copyright  Copyright (c) 2008-2022, 47GradNord - Agentur für Internetlösungen
  7.  * @author     47GradNord - Agentur für Internetlösungen <info@47gradnord.de>
  8.  */
  9. namespace App\Controller;
  10. use App\Service\UserManager;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  14. use Symfony\Component\Notifier\NotifierInterface;
  15. use Symfony\Component\Notifier\Recipient\Recipient;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Component\Security\Http\LoginLink\LoginLinkHandlerInterface;
  18. use Symfony\Component\Security\Http\LoginLink\LoginLinkNotification;
  19. use Twig\Error\LoaderError;
  20. use Twig\Error\RuntimeError;
  21. use Twig\Error\SyntaxError;
  22. class DefaultController extends AbstractAppController
  23. {
  24.     /**
  25.      * @Route("/", name="index", methods={"GET"} )
  26.      */
  27.     public function index(): Response
  28.     {
  29.         if(null !== $this->getAppUser())
  30.         {
  31.             return $this->redirectToRoute('app_index');
  32.         }
  33.         return $this->render('default/index.html.twig', [
  34.             'user' => $this->getAppUser(),
  35.         ]);
  36.     }
  37.     /**
  38.      * @Route("/create-login-link", name="create-login-link", methods={"GET", "POST"} )
  39.      */
  40.     public function createLoginLink(Request $requestLoginLinkHandlerInterface $loginLinkHandlerUserManager $managerNotifierInterface $notifier): Response
  41.     {
  42.         // check if login form is submitted
  43.         if ($request->isMethod('POST')) {
  44.             // load the user in some way (e.g. using the form input)
  45.             $user $manager->findUserByEmail($request->request->get('email'));
  46.             if (null === $user) {
  47.                 $this->addFlash('danger''Die Aktion konnte nicht durchgeführt werden. Bitte wenden Sie sich an einen Administrator.');
  48.                 return $this->redirectToRoute('create-login-link');
  49.             }
  50.             // create a login link for $user this returns an instance
  51.             // of LoginLinkDetails
  52.             $loginLinkDetails $loginLinkHandler->createLoginLink($user);
  53.             $loginLink $loginLinkDetails->getUrl();
  54.             // create a notification based on the login link details
  55.             $notification = new LoginLinkNotification(
  56.                 $loginLinkDetails,
  57.                 'Ihr Anmelde-Link' // email subject
  58.             );
  59.             // create a recipient for this user
  60.             $recipient = new Recipient($user->getEmail());
  61.             // send the notification to the user
  62.             $notifier->send($notification$recipient);
  63.             /**try {
  64.                 $manager->proceedSendLoginLink($user, $loginLink);
  65.             } catch (TransportExceptionInterface | LoaderError | RuntimeError | SyntaxError $e) {
  66.             }**/
  67.             $this->addFlash('success''Bitte prüfen Sie ihren Posteingang. Wir haben den Anmelde-Link verschickt.');
  68.             return $this->redirectToRoute('create-login-link');
  69.         }
  70.         return $this->render('default/createLoginLink.html.twig', [
  71.         ]);
  72.     }
  73. }