<?php
namespace App\Controller;
use App\Controller\Traits\SecurityTrait;
use App\Exception\reCaptcha3Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Services\{ParameterService, reCaptcha3ValidatorService, TenantFlowService, TenantService, TranslatorService};
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
use SecurityTrait;
private $tenant;
private $tenantFlowService;
private $translatorService;
private $reCaptcha3ValidatorService;
public function __construct(
TenantService $tenantService,
TenantFlowService $tenantFlowService,
TranslatorService $translatorService,
ParameterService $parameterService,
reCaptcha3ValidatorService $reCaptcha3ValidatorService
)
{
$this->tenant = $tenantService->defineTenant();
$this->tenantFlowService = $tenantFlowService;
$this->translatorService = $translatorService;
$this->reCaptcha3ValidatorService = $reCaptcha3ValidatorService;
$this->reCaptcha3ValidatorService->setAccess(
$parameterService->getParameter($this->tenant->getSettingsArrayAssoc()['config'] ?? 'non-existent', 'reCaptcha3.secretKey'),
$parameterService->getParameter($this->tenant->getSettingsArrayAssoc()['config'] ?? 'non-existent', 'reCaptcha3.allowableScore')
);
}
/**
* @Route("/log-in", name="user_sign_in")
*/
public function controlSignIn(AuthenticationUtils $authenticationUtils, Security $security): Response
{
if (!is_null($security->getUser())) return $this->redirectToRoute('user_profile');
return $this->render("{$this->tenant->getRootPath()}/pages/sign-in.html.twig", [
'rootPath' => $this->tenant->getRootPath(),
'last_username' => $authenticationUtils->getLastUsername(),
'message' => $authenticationUtils->getLastAuthenticationError()
]
+ $this->tenantFlowService->prepareTemplateArguments('signUp', $this->tenant)
+ $this->tenantFlowService->prepareTemplateArguments('menu', $this->tenant)
);
}
/**
* @Route("/ask-access", name="ask_access")
*/
public function controlAskingOneTimeAuthorization(Request $request): Response
{
if ( $this->isPost() ) {
try {
if ( ! $this->isCsrfTokenExistsAndValid('AskAccessForm') ) {
throw new \Exception('Invalid CSRF token.');
}
$this->reCaptcha3ValidatorService->setToken($request->request->get('reCAPTCHA_token'));
if ($this->reCaptcha3ValidatorService->validate()) {
throw new reCaptcha3Exception('reCaptcha3 validation failed.');
}
return $this->tenantFlowService->controlRequest($this->tenant, $request);
} catch (\Exception $exception) {
$message = $exception->getMessage();
}
}
return $this->render("{$this->tenant->getRootPath()}/pages/ask-access.html.twig", [
'rootPath' => $this->tenant->getRootPath(),
'message' => $message ?? ''
]
+ $this->tenantFlowService->prepareTemplateArguments('signUp', $this->tenant)
+ $this->tenantFlowService->prepareTemplateArguments('menu', $this->tenant)
);
}
/**
* @Route("/use-access-code", name="use_access_code", methods={"GET", "HEAD"})
*/
public function controlUseAccessCode(Request $request): Response
{
try {
if (is_null($request->query->get('code'))) return $this->redirectToRoute('user_sign_in');
return $this->tenantFlowService->controlRequest($this->tenant, $request);
} catch (\Exception $exception) {
$message = $exception->getMessage();
}
return $this->redirectToRoute('user_sign_in');
}
/**
* @Route("/sign-out", name="user_sign_out")
* @throws \RuntimeException
*/
public function controlSignOut()
{
throw new \RuntimeException('Intercepted by the logout key on your firewall.');
}
}