src/Controller/SecurityController.php line 46

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Controller\Traits\SecurityTrait;
  4. use App\Exception\reCaptcha3Exception;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use App\Services\{ParameterServicereCaptcha3ValidatorServiceTenantFlowServiceTenantServiceTranslatorService};
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\Security\Core\Security;
  11. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  12. class SecurityController extends AbstractController
  13. {
  14.     use SecurityTrait;
  15.     private $tenant;
  16.     private $tenantFlowService;
  17.     private $translatorService;
  18.     private $reCaptcha3ValidatorService;
  19.     public function __construct(
  20.         TenantService $tenantService,
  21.         TenantFlowService $tenantFlowService,
  22.         TranslatorService $translatorService,
  23.         ParameterService $parameterService,
  24.         reCaptcha3ValidatorService $reCaptcha3ValidatorService
  25.     )
  26.     {
  27.         $this->tenant $tenantService->defineTenant();
  28.         $this->tenantFlowService $tenantFlowService;
  29.         $this->translatorService $translatorService;
  30.         $this->reCaptcha3ValidatorService $reCaptcha3ValidatorService;
  31.         $this->reCaptcha3ValidatorService->setAccess(
  32.             $parameterService->getParameter($this->tenant->getSettingsArrayAssoc()['config'] ?? 'non-existent''reCaptcha3.secretKey'),
  33.             $parameterService->getParameter($this->tenant->getSettingsArrayAssoc()['config'] ?? 'non-existent''reCaptcha3.allowableScore')
  34.         );
  35.     }
  36.     /**
  37.      * @Route("/log-in", name="user_sign_in")
  38.      */
  39.     public function controlSignIn(AuthenticationUtils $authenticationUtilsSecurity $security): Response
  40.     {
  41.         if (!is_null($security->getUser())) return $this->redirectToRoute('user_profile');
  42.         return $this->render("{$this->tenant->getRootPath()}/pages/sign-in.html.twig", [
  43.             'rootPath' => $this->tenant->getRootPath(),
  44.             'last_username' => $authenticationUtils->getLastUsername(),
  45.             'message' => $authenticationUtils->getLastAuthenticationError()
  46.         ]
  47.             + $this->tenantFlowService->prepareTemplateArguments('signUp'$this->tenant)
  48.             + $this->tenantFlowService->prepareTemplateArguments('menu'$this->tenant)
  49.         );
  50.     }
  51.     /**
  52.      * @Route("/ask-access", name="ask_access")
  53.      */
  54.     public function controlAskingOneTimeAuthorization(Request $request): Response
  55.     {
  56.         if ( $this->isPost() ) {
  57.             try {
  58.                 if ( ! $this->isCsrfTokenExistsAndValid('AskAccessForm') ) {
  59.                     throw new \Exception('Invalid CSRF token.');
  60.                 }
  61.                 $this->reCaptcha3ValidatorService->setToken($request->request->get('reCAPTCHA_token'));
  62.                 if ($this->reCaptcha3ValidatorService->validate()) {
  63.                     throw new reCaptcha3Exception('reCaptcha3 validation failed.');
  64.                 }
  65.                 
  66.                 return $this->tenantFlowService->controlRequest($this->tenant$request);
  67.                 
  68.             } catch (\Exception $exception) {
  69.                 $message $exception->getMessage();
  70.             }
  71.         }
  72.         return $this->render("{$this->tenant->getRootPath()}/pages/ask-access.html.twig", [
  73.             'rootPath' => $this->tenant->getRootPath(),
  74.             'message' => $message ?? ''
  75.         ]
  76.             + $this->tenantFlowService->prepareTemplateArguments('signUp'$this->tenant)
  77.             + $this->tenantFlowService->prepareTemplateArguments('menu'$this->tenant)
  78.         );
  79.     }
  80.     /**
  81.      * @Route("/use-access-code", name="use_access_code", methods={"GET", "HEAD"})
  82.      */
  83.     public function controlUseAccessCode(Request $request): Response
  84.     {
  85.         try {
  86.             if (is_null($request->query->get('code'))) return $this->redirectToRoute('user_sign_in');
  87.             return $this->tenantFlowService->controlRequest($this->tenant$request);
  88.         } catch (\Exception $exception) {
  89.             $message $exception->getMessage();
  90.         }
  91.         return $this->redirectToRoute('user_sign_in');
  92.     }
  93.     /**
  94.      * @Route("/sign-out", name="user_sign_out")
  95.      * @throws \RuntimeException
  96.      */
  97.     public function controlSignOut()
  98.     {
  99.         throw new \RuntimeException('Intercepted by the logout key on your firewall.');
  100.     }
  101. }