<?php
namespace App\Controller\Admin;
use App\Entity\Page as PageEntity;
use App\EventListener\FeatureFlagListener;
use App\Renderer\Page;
use Flagception\Manager\FeatureManagerInterface;
use FOS\UserBundle\Controller\ResettingController;
use FOS\UserBundle\Controller\SecurityController as BaseController;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\Event\GetResponseUserEvent;
use FOS\UserBundle\Form\Factory\FactoryInterface;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Model\UserManagerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class SecurityController extends BaseController
{
public const TOKEN_LIFETIME_IN_HOURS = 2;
private RequestStack $requestStack;
private Page $pageRenderer;
private RouterInterface $router;
private ResettingController $resettingController;
private UserManagerInterface $userManager;
private FactoryInterface $formFactory;
private EventDispatcherInterface $eventDispatcher;
private FeatureManagerInterface $featureManager;
private ParameterBagInterface $parameterBag;
public function __construct(
AuthenticationUtils $authenticationUtils,
CsrfTokenManagerInterface $tokenManager,
RequestStack $requestStack,
Page $pageRenderer,
RouterInterface $router,
ResettingController $resettingController,
UserManagerInterface $userManager,
FactoryInterface $formFactory,
EventDispatcherInterface $eventDispatcher,
FeatureManagerInterface $featureManager,
ParameterBagInterface $parameterBag
) {
parent::__construct($authenticationUtils, $tokenManager);
$this->requestStack = $requestStack;
$this->pageRenderer = $pageRenderer;
$this->router = $router;
$this->resettingController = $resettingController;
$this->userManager = $userManager;
$this->formFactory = $formFactory;
$this->eventDispatcher = $eventDispatcher;
$this->featureManager = $featureManager;
$this->parameterBag = $parameterBag;
}
/**
* {@inheritdoc}
*
* @throws \Exception
*/
protected function renderLogin(array $data): Response
{
if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
return $this->redirect($this->generateUrl('admin_dashboard_dashboard_index'));
}
$template = '@admin/Security/login.html.twig';
$requestAttributes = $this->requestStack->getMainRequest()->attributes;
$data['enableRegistration'] = false;
$data['enableForgotPassword'] = true;
$data['enableCompanyRegistration'] = false;
$data['loginCheck'] = $this->router->generate('fos_user_security_check');
$data['forgotPasswordLink'] = $this->router->generate('admin_forgot_password');
$data['azureLogin'] = $this->featureManager->isActive(FeatureFlagListener::FEATURE_OAUTH_SSO_AZURE);
$data['googleLogin'] = $this->featureManager->isActive(FeatureFlagListener::FEATURE_OAUTH_SSO_GOOGLE);
$data['getnoticedGoogleLogin'] = $this->featureManager->isActive(FeatureFlagListener::FEATURE_OAUTH_SSO_GETNOTICED_GOOGLE);
$data['pingfederateLogin'] = $this->featureManager->isActive(FeatureFlagListener::FEATURE_OAUTH_SSO_PINGFEDERATE);
if ('user_login' === $requestAttributes->get('_route')) {
if (true !== $this->parameterBag->get('site_user_active')) {
return $this->redirectToRoute('homepage');
}
$data['loginCheck'] = $this->router->generate('user_check');
$data['forgotPasswordLink'] = $this->router->generate('pages_user_forgot_password');
$data['enableRegistration'] = $this->parameterBag->get('site_user_enable_registration');
$data['enableForgotPassword'] = $this->parameterBag->get('site_user_enable_forgot_password');
$data['enableCompanyRegistration'] = $this->parameterBag->get('site_user_enable_company_registration');
$page = (new PageEntity())
->setTitle('login')
->setBody($this->renderView('@default/user/Security/login_content.html.twig', $data))
;
$response = $this->render('@default/pages/pages_index.html.twig', [
'header' => $this->pageRenderer->getHeader(),
'footer' => $this->pageRenderer->getFooter(),
'page' => $page,
]);
$response->setSharedMaxAge(0);
return $response;
}
$response = $this->render($template, $data);
$response->setSharedMaxAge(0);
return $response;
}
public function forgotPasswordAction(Request $request): Response
{
if ($request->get('username')) {
$response = $this->resettingController->sendEmailAction($request);
if ($response instanceof RedirectResponse) {
return $this->redirectToRoute('admin_check_mail');
}
}
return $this->render('@admin/Security/forgot_password.html.twig');
}
public function checkMailAction(): Response
{
return $this->render('@admin/Security/check_mail.html.twig', [
'tokenLifetime' => self::TOKEN_LIFETIME_IN_HOURS,
]);
}
public function resetAction(Request $request, $token, TranslatorInterface $translator, RouterInterface $router): Response
{
$user = $this->userManager->findUserByConfirmationToken($token);
if (null === $user) {
return new RedirectResponse($router->generate('admin_dashboard_dashboard_index'));
}
$event = new GetResponseUserEvent($user, $request);
$this->eventDispatcher->dispatch($event, FOSUserEvents::RESETTING_RESET_INITIALIZE);
if (null !== $event->getResponse()) {
return $event->getResponse();
}
$locale = $request->getLocale();
$form = $this->formFactory->createForm();
$form->setData($user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$event = new FormEvent($form, $request);
$this->eventDispatcher->dispatch($event, FOSUserEvents::RESETTING_RESET_SUCCESS);
$this->userManager->updateUser($user);
if (null === $response = $event->getResponse()) {
$url = $this->generateUrl('admin_dashboard_dashboard_index');
$response = new RedirectResponse($url);
}
$this->eventDispatcher->dispatch(
new FilterUserResponseEvent($user, $request, $response),
FOSUserEvents::RESETTING_RESET_COMPLETED
);
return $response;
}
return $this->render('@admin/Security/reset.html.twig', [
'token' => $token,
'form' => $form->createView(),
'site_page_title' => $translator->trans('Reset Password', [], 'admin', $locale),
]);
}
}