<?php
declare(strict_types=1);
/*
* ImmoBay - BAUR Immobilien
*
* @copyright Copyright (c) 2008-2022, 47GradNord - Agentur für Internetlösungen
* @author 47GradNord - Agentur für Internetlösungen <info@47gradnord.de>
*/
namespace App\Controller;
use App\Service\UserManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Notifier\NotifierInterface;
use Symfony\Component\Notifier\Recipient\Recipient;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\LoginLink\LoginLinkHandlerInterface;
use Symfony\Component\Security\Http\LoginLink\LoginLinkNotification;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
class DefaultController extends AbstractAppController
{
/**
* @Route("/", name="index", methods={"GET"} )
*/
public function index(): Response
{
if(null !== $this->getAppUser())
{
return $this->redirectToRoute('app_index');
}
return $this->render('default/index.html.twig', [
'user' => $this->getAppUser(),
]);
}
/**
* @Route("/create-login-link", name="create-login-link", methods={"GET", "POST"} )
*/
public function createLoginLink(Request $request, LoginLinkHandlerInterface $loginLinkHandler, UserManager $manager, NotifierInterface $notifier): Response
{
// check if login form is submitted
if ($request->isMethod('POST')) {
// load the user in some way (e.g. using the form input)
$user = $manager->findUserByEmail($request->request->get('email'));
if (null === $user) {
$this->addFlash('danger', 'Die Aktion konnte nicht durchgeführt werden. Bitte wenden Sie sich an einen Administrator.');
return $this->redirectToRoute('create-login-link');
}
// create a login link for $user this returns an instance
// of LoginLinkDetails
$loginLinkDetails = $loginLinkHandler->createLoginLink($user);
$loginLink = $loginLinkDetails->getUrl();
// create a notification based on the login link details
$notification = new LoginLinkNotification(
$loginLinkDetails,
'Ihr Anmelde-Link' // email subject
);
// create a recipient for this user
$recipient = new Recipient($user->getEmail());
// send the notification to the user
$notifier->send($notification, $recipient);
/**try {
$manager->proceedSendLoginLink($user, $loginLink);
} catch (TransportExceptionInterface | LoaderError | RuntimeError | SyntaxError $e) {
}**/
$this->addFlash('success', 'Bitte prüfen Sie ihren Posteingang. Wir haben den Anmelde-Link verschickt.');
return $this->redirectToRoute('create-login-link');
}
return $this->render('default/createLoginLink.html.twig', [
]);
}
}