Refactoring du AdminController pour être compatible Symfony 5

This commit is contained in:
Mathieu Lemoine
2020-05-07 14:32:30 +02:00
parent 424c3b5704
commit b5ef6512d7
11 changed files with 249 additions and 155 deletions
@@ -73,9 +73,9 @@
<div class="toc">
<div class="ui vertical inverted menu accordion">
<div class="item logo">
<img src="/templates/administration/images/logo_afup.png" style="max-width: 100%"/>
<img src="/templates/administration/images/logo_afup.png" style="max-width: 100%" alt="Logo AFUP"/>
</div>
{{ render(controller('AppBundle:Admin:getMenu')) }}
{{ render(controller('AppBundle\\Controller\\Admin\\GetMenuAction')) }}
</div>
</div>
</div>
+1 -1
View File
@@ -1,4 +1,4 @@
{% extends ':admin/association/membership:_base.html.twig' %}
{% extends 'admin/association/membership/_base.html.twig' %}
{% block page_title %}{% endblock %}
+2 -2
View File
@@ -12,7 +12,7 @@ admin_void:
admin_login:
path: /login
defaults: {_controller: AppBundle:Admin:login}
defaults: {_controller: AppBundle\Controller\Admin\LoginAction}
admin_logout:
path: /logout
@@ -23,7 +23,7 @@ legacy_inscription:
admin_password:
path: /password
defaults: {_controller: AppBundle:Admin:lostPassword}
defaults: {_controller: AppBundle\Controller\Admin\LostPasswordAction}
admin_event_routes:
resource: "admin_event.yml"
+12 -3
View File
@@ -36,10 +36,19 @@ services:
autowire: true
autoconfigure: true
AppBundle\Security\MyGithubAuthenticator:
AppBundle\Controller\Admin\:
resource: '../../sources/AppBundle/Controller/Admin/*'
autowire: true
autoconfigure: true
AppBundle\Controller\CFPController:
AppBundle\Controller\Admin\GetMenuAction:
autowire: true
arguments:
$backOfficePages: '%app.pages_backoffice%'
AppBundle\Controller\BlocksHandler: ~
AppBundle\Security\MyGithubAuthenticator:
autowire: true
AppBundle\Controller\SpeakerSuggestionController:
@@ -81,7 +90,7 @@ services:
- { name: kernel.event_listener, event: kernel.request, priority: 100 }
AppBundle\Listener\LegacySiteListener:
arguments: ["@security.token_storage"]
autowire: true
tags:
- { name: kernel.event_listener, event: kernel.controller, priority: 100 }
@@ -0,0 +1,59 @@
<?php
namespace AppBundle\Controller\Admin;
use Assert\Assertion;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;
class GetMenuAction
{
/** @var array<string, mixed> */
private $backOfficePages;
/** @var Environment */
private $twig;
/** @var RequestStack */
private $requestStack;
public function __construct(
RequestStack $requestStack,
Environment $twig,
array $backOfficePages
) {
$this->backOfficePages = $backOfficePages;
$this->requestStack = $requestStack;
$this->twig = $twig;
}
public function __invoke()
{
$masterRequest = $this->requestStack->getMasterRequest();
Assertion::notNull($masterRequest);
$page = $masterRequest->query->get('page');
$route = $masterRequest->get('_route');
$currentGroupKey = null;
$currentElementKey = null;
foreach ($this->backOfficePages as $groupKey => $group) {
if (isset($group['elements'])) {
foreach ($group['elements'] as $elementKey => $element) {
if ($elementKey === $page
|| (isset($element['extra_routes']) && in_array($route, $element['extra_routes'], true))
|| (isset($element['extra_pages']) && in_array($page, $element['extra_pages'], true))
) {
$currentGroupKey = $groupKey;
$currentElementKey = $elementKey;
}
}
}
}
return new Response($this->twig->render(':admin:menu.html.twig', [
'pages' => $this->backOfficePages,
'current_group_key' => $currentGroupKey,
'current_element_key' => $currentElementKey,
]));
}
}
@@ -0,0 +1,52 @@
<?php
namespace AppBundle\Controller\Admin;
use AppBundle\Controller\BlocksHandler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Twig\Environment;
class LoginAction
{
/** @var AuthenticationUtils */
private $authenticationUtils;
/** @var BlocksHandler */
private $blocksHandler;
/** @var Environment */
private $twig;
public function __construct(
AuthenticationUtils $authenticationUtils,
BlocksHandler $blocksHandler,
Environment $twig
) {
$this->authenticationUtils = $authenticationUtils;
$this->blocksHandler = $blocksHandler;
$this->twig = $twig;
}
public function __invoke(Request $request)
{
// get the login error if there is one
$error = $this->authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $this->authenticationUtils->getLastUsername();
$actualUrl = $request->getSchemeAndHttpHost() . $request->getRequestUri();
$targetUri = $request->query->get('target');
$noDomain = parse_url($targetUri, PHP_URL_HOST) === null;
$targetPath = $targetUri !== $actualUrl && $noDomain ? $targetUri : null;
return new Response($this->twig->render('admin/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
'target_path' => $targetPath,
'title' => 'Connexion',
'page' => 'connexion',
'class' => 'panel-page',
] + $this->blocksHandler->getDefaultBlocks()));
}
}
@@ -0,0 +1,66 @@
<?php
namespace AppBundle\Controller\Admin;
use Afup\Site\Association\Personnes_Physiques;
use AppBundle\Controller\BlocksHandler;
use AppBundle\LegacyModelFactory;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Twig\Environment;
class LostPasswordAction
{
/** @var FormFactoryInterface */
private $formFactory;
/** @var LegacyModelFactory */
private $legacyModelFactory;
/** @var BlocksHandler */
private $blocksHandler;
/** @var Environment */
private $twig;
/** @var FlashBagInterface */
private $flashBag;
public function __construct(
FormFactoryInterface $formFactory,
LegacyModelFactory $legacyModelFactory,
BlocksHandler $blocksHandler,
Environment $twig,
FlashBagInterface $flashBag
) {
$this->formFactory = $formFactory;
$this->legacyModelFactory = $legacyModelFactory;
$this->blocksHandler = $blocksHandler;
$this->twig = $twig;
$this->flashBag = $flashBag;
}
public function __invoke(Request $request)
{
$form = $this->formFactory->createBuilder(FormType::class)
->add('email', EmailType::class)
->add('submit', SubmitType::class, ['label' => 'Demander un nouveau mot de passe'])
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var $personnesPhysiques Personnes_Physiques */
$personnesPhysiques = $this->legacyModelFactory->createObject(Personnes_Physiques::class);
$personnesPhysiques->envoyerMotDePasse($form->getData()['email']);
$this->flashBag->add('notice', 'Votre demande a été prise en compte. Si un compte correspond à cet email vous recevez un nouveau mot de passe rapidement.');
}
return new Response($this->twig->render('admin/lost_password.html.twig', [
'form' => $form->createView(),
'title' => 'Mot de passe perdu',
'page' => 'motdepasse_perdu',
'class' => 'panel-page',
] + $this->blocksHandler->getDefaultBlocks()));
}
}
@@ -1,108 +0,0 @@
<?php
namespace AppBundle\Controller;
use Afup\Site\Association\Personnes_Physiques;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;
class AdminController extends SiteBaseController
{
public function loginAction(Request $request)
{
$authenticationUtils = $this->get('security.authentication_utils');
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
$actualUrl = $request->getSchemeAndHttpHost() . $request->getRequestUri();
$targetPath = null;
if (
$request->query->has('target')
and $targetUri = $request->query->get('target')
and $targetUri !== $actualUrl
and parse_url($targetUri, PHP_URL_HOST) === null // Ensure there is no domain here
) {
$targetPath = $targetUri;
}
return $this->render('admin/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
'target_path' => $targetPath,
'title' => "Connexion",
'page' => 'connexion',
'class' => 'panel-page'
]);
}
public function lostPasswordAction(Request $request)
{
$form = $this->createFormBuilder()
->add('email', EmailType::class)
->add('submit', SubmitType::class, ['label' => 'Demander un nouveau mot de passe'])
->getForm()
;
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/**
* @var $personnesPhysiques Personnes_Physiques
*/
$personnesPhysiques = $this->get(\AppBundle\LegacyModelFactory::class)->createObject(Personnes_Physiques::class);
$personnesPhysiques->envoyerMotDePasse($form->getData()['email']);
$this->addFlash('notice', 'Votre demande a été prise en compte. Si un compte correspond à cet email vous recevez un nouveau mot de passe rapidement.');
}
return $this->render('admin/lost_password.html.twig',
[
'form' => $form->createView(),
'title' => 'Mot de passe perdu',
'page' => 'motdepasse_perdu',
'class' => 'panel-page'
]
);
}
public function getMenuAction()
{
$pages = $this->getParameter('app.pages_backoffice');
$masterRequest = $this->get('request_stack')->getMasterRequest();
$page = $masterRequest->query->get('page');
$route = $masterRequest->get('_route');
$currentGroupKey = null;
$currentElementKey = null;
foreach ($pages as $groupKey => $group) {
if (isset($group['elements'])) {
foreach ($group['elements'] as $elementKey => $element) {
if ($elementKey == $page
|| (isset($element['extra_routes']) && in_array($route, $element['extra_routes']))
|| (isset($element['extra_pages']) && in_array($page, $element['extra_pages']))
) {
$currentGroupKey = $groupKey;
$currentElementKey = $elementKey;
}
}
}
}
return $this->render(
':admin:menu.html.twig',
[
'pages' => $pages,
'current_group_key' => $currentGroupKey,
'current_element_key' => $currentElementKey,
]
);
}
}
@@ -0,0 +1,33 @@
<?php
namespace AppBundle\Controller;
class BlocksHandler
{
/** @var array<string, string> */
protected $defaultBlocks = [];
/**
* @param array{
* community: string,
* header: string,
* sidebar: string,
* social: string,
* footer: string
* }
*
* @return void
*/
public function setDefaultBlocks(array $blocks)
{
$this->defaultBlocks = $blocks;
}
/**
* @return array<string, string>
*/
public function getDefaultBlocks()
{
return $this->defaultBlocks;
}
}
@@ -18,6 +18,7 @@ abstract class SiteBaseController extends Controller implements SiteControllerIn
/**
* @inheritDoc
* @deprecated use BlocksHandler
*/
public function setDefaultBlocks(array $blocks)
{
@@ -4,21 +4,22 @@
namespace AppBundle\Listener;
use Afup\Site\Corporate\Page;
use Afup\Site\Utils\Base_De_Donnees;
use AppBundle\Controller\BlocksHandler;
use AppBundle\Controller\SiteControllerInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Security\Core\Security;
class LegacySiteListener
{
/**
* @var TokenStorage
*/
private $tokenStorage;
/** @var Security */
private $security;
/** @var BlocksHandler */
private $blocksHandler;
public function __construct(TokenStorage $tokenStorage)
public function __construct(Security $security, BlocksHandler $blocksHandler)
{
$this->tokenStorage = $tokenStorage;
$this->security = $security;
$this->blocksHandler = $blocksHandler;
}
public function onKernelController(FilterControllerEvent $event)
@@ -27,44 +28,25 @@ class LegacySiteListener
return;
}
$controller = $event->getController();
if (!is_array($controller) || [] === $controller || !$controller[0] instanceof SiteControllerInterface) {
return;
}
/**
* @var $controller SiteControllerInterface
*/
$controller = $controller[0];
require_once __DIR__ . '/../../Afup/Bootstrap/Http.php';
/**
* @var $bdd Base_De_Donnees
*/
$page = new Page($GLOBALS['AFUP_DB']);
$controller->setDefaultBlocks([
$blocks = [
'community' => $page->community(),
'header' => $page->header($_SERVER['REQUEST_URI'], $this->getUser()),
'header' => $page->header($_SERVER['REQUEST_URI'], $this->security->getUser()),
'sidebar' => $page->getRightColumn(),
'social' => $page->social(),
'footer' => $page->footer()
]);
];
$this->blocksHandler->setDefaultBlocks($blocks);
// TODO: remove once SiteControllerInterface is removed in favor of BlocksHandler
$controller = $event->getController();
if (!is_array($controller) || [] === $controller || !$controller[0] instanceof SiteControllerInterface) {
return;
}
/** @var $controller SiteControllerInterface */
$controller = $controller[0];
$controller->setDefaultBlocks($blocks);
$controller->setConfiguration($GLOBALS['AFUP_CONF']);
}
protected function getUser()
{
if (null === $token = $this->tokenStorage->getToken()) {
return null;
}
if (!is_object($user = $token->getUser())) {
return null;
}
return $user;
}
}