<?php
namespace App\Controller;
use App\Repository\UserRepository;
use App\Service\EmailService;
use DateTime;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Http\LoginLink\LoginLinkHandlerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class SecurityController extends AbstractController
{
#[Route('/login', name: 'app_login')]
public function login(AuthenticationUtils $authenticationUtils): Response
{
// if ($this->getUser()) {
// return $this->redirectToRoute('target_path');
// }
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
#[Route('/logout', name: 'app_logout')]
public function logout(): void
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
#[Route('/login_check', name: 'login_check')]
public function check(Request $request, TranslatorInterface $translator): Response
{
$renewPw = false;
$expires = $request->query->get('expires');
$username = $request->query->get('user');
$hash = $request->query->get('hash');
$isRenewPassword = $request->query->get('renewPw');
$linkLabel = $translator->trans('lang.toDashboard');
if ($isRenewPassword == 1) {
$linkLabel = $translator->trans('txtChangeMyPasswd');
$renewPw = true;
}
return $this->render('security/process_login_link.html.twig', [
'expires' => $expires,
'user' => $username,
'hash' => $hash,
'link_label' => $linkLabel,
'renew_pw' => $renewPw,
]);
}
#[Route('/login-link', name: 'login_link')]
public function requestLoginLink(
LoginLinkHandlerInterface $loginLinkHandler,
UserRepository $userRepository,
Request $request,
TranslatorInterface $translator,
EmailService $emailService
) {
$loginLink = '';
$headline = $translator->trans('lang.loginViaEmail');
$isRenewPassword = false;
if (isset($_REQUEST['renew-password'])) {
$headline = $translator->trans('lang.renewPassword');
$isRenewPassword = true;
}
if ($request->isMethod('POST')) {
$email = $request->request->get('email');
$user = $userRepository->findOneBy(['email' => $email]);
if (!$user) {
$this->addFlash('danger', $translator->trans('unknownUser'));
return $this->redirectToRoute('app_login');
}
$now = new DateTime();
$nowTimestamp = intval($now->format("U"));
$expireTimestamp = $nowTimestamp + 600;
$expireObject = new DateTime(date("Y-m-d H:i:s", $expireTimestamp));
$loginLinkDetails = $loginLinkHandler->createLoginLink($user);
$loginLink = $loginLinkDetails->getUrl();
$emailSubject = $translator->trans('lang.yourLoginLink');
$headline = $translator->trans('lang.yourLoginLink');
$topic = $this->renderView('security/email/login_sent.html.twig', [
'login_link' => $loginLink,
'expire_datetime' => $expireObject,
]);
$clientArray = [
$user->getSalutation(),
$user->getFirstName(),
$user->getLastName(),
];
$clientData = implode(" ", $clientArray);
if ($request->request->get('renew_password')) {
$emailSubject = $translator->trans('lang.renewPassword');
$headline = $translator->trans('lang.renewPassword');
$topic = $this->renderView('security/email/new_password.html.twig', [
'login_link' => $loginLink,
'expire_datetime' => $expireObject,
]);
}
$emailUuid = uuid_create();
$sendTo = $user->getEmail();
$emailText = $this->renderView('client/email/master.html.twig', [
'headline' => $headline,
'company' => $user->getCompanyName(),
'clientData' => $clientData,
'subject' => $emailSubject,
'topic' => $topic,
]);
$sentEmail = $emailService->sendEmail($emailSubject, $emailText, $sendTo, $emailUuid);
return $this->render('security/login_link_completed.html.twig', [
'headline' => $headline,
'is_renew_password' => $isRenewPassword,
]);
}
if ($isRenewPassword) {
return $this->render('security/renew_password.html.twig', [
'login_link' => $loginLink,
'headline' => $headline,
'is_renew_password' => 1,
]);
}
return $this->render('security/login_link.html.twig', [
'login_link' => $loginLink,
'headline' => $headline,
'is_renew_password' => 0,
]);
}
#[Route('/login-redirect', name: 'login_redirect')]
public function redirections()
{
if ($this->isGranted('ROLE_SALES')) {
return $this->redirectToRoute('app_sales_dashboard');
}
if ($this->isGranted('ROLE_CLIENT')) {
return $this->redirectToRoute('app_client_dashboard');
}
if ($this->isGranted('ROLE_ADMIN')) {
return $this->redirectToRoute('app_dashboard');
}
}
#[Route('/login-redirect-renew-pw', name: 'login_redirect_renew_pw')]
public function redirectionsRenewPw()
{
if ($this->isGranted('ROLE_SALES')) {
return $this->redirectToRoute('app_sales_dashboard_change_password');
}
if ($this->isGranted('ROLE_CLIENT')) {
return $this->redirectToRoute('app_client_dashboard_change_password');
}
if ($this->isGranted('ROLE_ADMIN')) {
return $this->redirectToRoute('app_dashboard_change_password');
}
}
}