src/Controller/SecurityController.php line 64

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Repository\UserRepository;
  4. use App\Service\EmailService;
  5. use DateTime;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  11. use Symfony\Component\Security\Http\LoginLink\LoginLinkHandlerInterface;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. class SecurityController extends AbstractController
  14. {
  15.     #[Route('/login'name'app_login')]
  16.     public function login(AuthenticationUtils $authenticationUtils): Response
  17.     {
  18.         // if ($this->getUser()) {
  19.         //     return $this->redirectToRoute('target_path');
  20.         // }
  21.         // get the login error if there is one
  22.         $error $authenticationUtils->getLastAuthenticationError();
  23.         // last username entered by the user
  24.         $lastUsername $authenticationUtils->getLastUsername();
  25.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  26.     }
  27.     #[Route('/logout'name'app_logout')]
  28.     public function logout(): void
  29.     {
  30.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  31.     }
  32.     #[Route('/login_check'name'login_check')]
  33.     public function check(Request $requestTranslatorInterface $translator): Response
  34.     {
  35.         $renewPw         false;
  36.         $expires         $request->query->get('expires');
  37.         $username        $request->query->get('user');
  38.         $hash            $request->query->get('hash');
  39.         $isRenewPassword $request->query->get('renewPw');
  40.         $linkLabel       $translator->trans('lang.toDashboard');
  41.         if ($isRenewPassword == 1) {
  42.             $linkLabel $translator->trans('txtChangeMyPasswd');
  43.             $renewPw   true;
  44.         }
  45.         return $this->render('security/process_login_link.html.twig', [
  46.             'expires'    => $expires,
  47.             'user'       => $username,
  48.             'hash'       => $hash,
  49.             'link_label' => $linkLabel,
  50.             'renew_pw'   => $renewPw,
  51.         ]);
  52.     }
  53.     #[Route('/login-link'name'login_link')]
  54.     public function requestLoginLink(
  55.         LoginLinkHandlerInterface $loginLinkHandler,
  56.         UserRepository $userRepository,
  57.         Request $request,
  58.         TranslatorInterface $translator,
  59.         EmailService $emailService
  60.     ) {
  61.         $loginLink       '';
  62.         $headline        $translator->trans('lang.loginViaEmail');
  63.         $isRenewPassword false;
  64.         if (isset($_REQUEST['renew-password'])) {
  65.             $headline        $translator->trans('lang.renewPassword');
  66.             $isRenewPassword true;
  67.         }
  68.         if ($request->isMethod('POST')) {
  69.             $email $request->request->get('email');
  70.             $user  $userRepository->findOneBy(['email' => $email]);
  71.             if (!$user) {
  72.                 $this->addFlash('danger'$translator->trans('unknownUser'));
  73.                 return $this->redirectToRoute('app_login');
  74.             }
  75.             $now             = new DateTime();
  76.             $nowTimestamp    intval($now->format("U"));
  77.             $expireTimestamp $nowTimestamp 600;
  78.             $expireObject    = new DateTime(date("Y-m-d H:i:s"$expireTimestamp));
  79.             $loginLinkDetails $loginLinkHandler->createLoginLink($user);
  80.             $loginLink        $loginLinkDetails->getUrl();
  81.             $emailSubject $translator->trans('lang.yourLoginLink');
  82.             $headline     $translator->trans('lang.yourLoginLink');
  83.             $topic $this->renderView('security/email/login_sent.html.twig', [
  84.                 'login_link'      => $loginLink,
  85.                 'expire_datetime' => $expireObject,
  86.             ]);
  87.             $clientArray = [
  88.                 $user->getSalutation(),
  89.                 $user->getFirstName(),
  90.                 $user->getLastName(),
  91.             ];
  92.             $clientData implode(" "$clientArray);
  93.             if ($request->request->get('renew_password')) {
  94.                 $emailSubject $translator->trans('lang.renewPassword');
  95.                 $headline     $translator->trans('lang.renewPassword');
  96.                 $topic $this->renderView('security/email/new_password.html.twig', [
  97.                     'login_link'      => $loginLink,
  98.                     'expire_datetime' => $expireObject,
  99.                 ]);
  100.             }
  101.             $emailUuid uuid_create();
  102.             $sendTo    $user->getEmail();
  103.             $emailText $this->renderView('client/email/master.html.twig', [
  104.                 'headline'   => $headline,
  105.                 'company'    => $user->getCompanyName(),
  106.                 'clientData' => $clientData,
  107.                 'subject'    => $emailSubject,
  108.                 'topic'      => $topic,
  109.             ]);
  110.             $sentEmail $emailService->sendEmail($emailSubject$emailText$sendTo$emailUuid);
  111.             return $this->render('security/login_link_completed.html.twig', [
  112.                 'headline'          => $headline,
  113.                 'is_renew_password' => $isRenewPassword,
  114.             ]);
  115.         }
  116.         if ($isRenewPassword) {
  117.             return $this->render('security/renew_password.html.twig', [
  118.                 'login_link'        => $loginLink,
  119.                 'headline'          => $headline,
  120.                 'is_renew_password' => 1,
  121.             ]);
  122.         }
  123.         return $this->render('security/login_link.html.twig', [
  124.             'login_link'        => $loginLink,
  125.             'headline'          => $headline,
  126.             'is_renew_password' => 0,
  127.         ]);
  128.     }
  129.     #[Route('/login-redirect'name'login_redirect')]
  130.     public function redirections()
  131.     {
  132.         if ($this->isGranted('ROLE_SALES')) {
  133.             return $this->redirectToRoute('app_sales_dashboard');
  134.         }
  135.         if ($this->isGranted('ROLE_CLIENT')) {
  136.             return $this->redirectToRoute('app_client_dashboard');
  137.         }
  138.         if ($this->isGranted('ROLE_ADMIN')) {
  139.             return $this->redirectToRoute('app_dashboard');
  140.         }
  141.     }
  142.     #[Route('/login-redirect-renew-pw'name'login_redirect_renew_pw')]
  143.     public function redirectionsRenewPw()
  144.     {
  145.         if ($this->isGranted('ROLE_SALES')) {
  146.             return $this->redirectToRoute('app_sales_dashboard_change_password');
  147.         }
  148.         if ($this->isGranted('ROLE_CLIENT')) {
  149.             return $this->redirectToRoute('app_client_dashboard_change_password');
  150.         }
  151.         if ($this->isGranted('ROLE_ADMIN')) {
  152.             return $this->redirectToRoute('app_dashboard_change_password');
  153.         }
  154.     }
  155. }