Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework the front end preview #989

Merged
merged 30 commits into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
414b966
Rework frontend preview
richardhj Nov 25, 2019
4bad3d6
Collapse toolbar
richardhj Nov 25, 2019
2b856c1
Check for frontend request
richardhj Nov 25, 2019
a3a5630
Apply suggestions from code review
richardhj Nov 25, 2019
2098eba
Remove the contao_backend_switch route
richardhj Nov 25, 2019
8eacd71
Merge remote-tracking branch 'richardhj/feature/preview' into feature…
richardhj Nov 25, 2019
3315cc9
Apply suggestions from code review
richardhj Nov 26, 2019
b04c925
Inject js before </body>
richardhj Nov 26, 2019
9ebe83d
Remove non-present route
richardhj Nov 26, 2019
6fae43a
Move logic to PreviewUrlConvertEvent
richardhj Nov 28, 2019
280d16a
Add tests
richardhj Nov 29, 2019
2336e22
Apply suggestions from code review
richardhj Dec 2, 2019
2974a20
Do not inject preview bar if no preview entrypoint defined
richardhj Dec 3, 2019
dfe0d2a
Add ROLE_ALLOWED_TO_SWITCH_MEMBER
richardhj Dec 3, 2019
519124d
Further styling of toolbar
richardhj Dec 6, 2019
0732765
Fix safari incompatibilites
richardhj Dec 6, 2019
cfd83d9
Merge branch 'master' into feature/preview
richardhj Dec 11, 2019
80d06fd
Fix QS
richardhj Dec 11, 2019
f2f36f4
Fix the coding style
leofeyer Dec 19, 2019
0e3ae2e
Merge remote-tracking branch 'upstream/master' into feature/preview
richardhj Dec 30, 2019
4a969b2
Serialize amg in BackendUser.php
richardhj Dec 30, 2019
206d929
Remove CDATA as we use HTML
richardhj Dec 30, 2019
2c1c97d
Do not initialize framework.
richardhj Dec 30, 2019
550a561
Test service registration.
richardhj Dec 30, 2019
836d3c3
Fix the coding style
leofeyer Jan 6, 2020
1371041
Fix the CSS code
leofeyer Jan 6, 2020
8cbf016
Simplify event listener registration.
richardhj Jan 6, 2020
7666ac6
Remove legacy preview script code.
richardhj Jan 6, 2020
b225e1d
Add BackendPreviewSwitchControllerTest.php
richardhj Jan 6, 2020
973a979
Adjust the test method names
leofeyer Jan 6, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions calendar-bundle/src/EventListener/PreviewUrlConvertListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,16 @@
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\Events;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* @internal
*/
class PreviewUrlConvertListener
{
/**
* @var RequestStack
*/
private $requestStack;

/**
* @var ContaoFramework
*/
private $framework;

public function __construct(RequestStack $requestStack, ContaoFramework $framework)
public function __construct(ContaoFramework $framework)
{
$this->requestStack = $requestStack;
$this->framework = $framework;
}

Expand All @@ -49,7 +39,7 @@ public function __invoke(PreviewUrlConvertEvent $event): void
return;
}

$request = $this->requestStack->getCurrentRequest();
$request = $event->getRequest();

if (null === $request || null === ($eventModel = $this->getEventModel($request))) {
return;
Expand Down
1 change: 0 additions & 1 deletion calendar-bundle/src/Resources/config/listener.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ services:
contao_calendar.listener.preview_url_convert:
class: Contao\CalendarBundle\EventListener\PreviewUrlConvertListener
arguments:
- '@request_stack'
- '@contao.framework'
tags:
- { name: kernel.event_listener }
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ public function testRegistersThePreviewUrlConvertListener(): void

$this->assertEquals(
[
new Reference('request_stack'),
new Reference('contao.framework'),
],
$definition->getArguments()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Contao\Events;
use Contao\TestCase\ContaoTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

class PreviewUrlConverterListenerTest extends ContaoTestCase
{
Expand All @@ -30,9 +29,6 @@ public function testConvertsThePreviewUrl(): void
$request->server->set('SERVER_NAME', 'localhost');
$request->server->set('SERVER_PORT', 80);

$requestStack = new RequestStack();
$requestStack->push($request);

$eventModel = $this->createMock(CalendarEventsModel::class);

$adapters = [
Expand All @@ -41,9 +37,9 @@ public function testConvertsThePreviewUrl(): void
];

$framework = $this->mockContaoFramework($adapters);
$event = new PreviewUrlConvertEvent();
$event = new PreviewUrlConvertEvent($request);

$listener = new PreviewUrlConvertListener($requestStack, $framework);
$listener = new PreviewUrlConvertListener($framework);
$listener($event);

$this->assertSame('http://localhost/events/winter-holidays.html', $event->getUrl());
Expand All @@ -57,9 +53,9 @@ public function testDoesNotConvertThePreviewUrlIfTheFrameworkIsNotInitialized():
->willReturn(false)
;

$event = new PreviewUrlConvertEvent();
$event = new PreviewUrlConvertEvent(new Request());

$listener = new PreviewUrlConvertListener(new RequestStack(), $framework);
$listener = new PreviewUrlConvertListener($framework);
$listener($event);

$this->assertNull($event->getUrl());
Expand All @@ -71,13 +67,10 @@ public function testDoesNotConvertThePreviewUrlIfTheCalendarParameterIsNotSet():
$request->server->set('SERVER_NAME', 'localhost');
$request->server->set('SERVER_PORT', 80);

$requestStack = new RequestStack();
$requestStack->push($request);

$framework = $this->mockContaoFramework();
$event = new PreviewUrlConvertEvent();
$event = new PreviewUrlConvertEvent($request);

$listener = new PreviewUrlConvertListener($requestStack, $framework);
$listener = new PreviewUrlConvertListener($framework);
$listener($event);

$this->assertNull($event->getUrl());
Expand All @@ -90,17 +83,14 @@ public function testDoesNotConvertThePreviewUrlIfThereIsNoEvent(): void
$request->server->set('SERVER_NAME', 'localhost');
$request->server->set('SERVER_PORT', 80);

$requestStack = new RequestStack();
$requestStack->push($request);

$adapters = [
CalendarEventsModel::class => $this->mockConfiguredAdapter(['findByPk' => null]),
];

$framework = $this->mockContaoFramework($adapters);
$event = new PreviewUrlConvertEvent();
$event = new PreviewUrlConvertEvent($request);

$listener = new PreviewUrlConvertListener($requestStack, $framework);
$listener = new PreviewUrlConvertListener($framework);
$listener($event);

$this->assertNull($event->getUrl());
Expand Down
32 changes: 0 additions & 32 deletions core-bundle/src/Controller/BackendController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
use Contao\BackendPage;
use Contao\BackendPassword;
use Contao\BackendPopup;
use Contao\BackendPreview;
use Contao\BackendSwitch;
use Contao\CoreBundle\Picker\PickerBuilderInterface;
use Contao\CoreBundle\Picker\PickerConfig;
use Symfony\Component\HttpFoundation\RedirectResponse;
Expand Down Expand Up @@ -94,24 +92,6 @@ public function passwordAction(): Response
return $controller->run();
}

/**
* @Route("/contao/preview", name="contao_backend_preview")
*/
public function previewAction(Request $request): Response
{
$previewScript = $this->getParameter('contao.preview_script');

if ($request->getScriptName() !== $previewScript) {
return $this->redirect($previewScript.$request->getRequestUri());
}

$this->initializeContaoFramework();

$controller = new BackendPreview();

return $controller->run();
}

/**
* @Route("/contao/confirm", name="contao_backend_confirm")
*/
Expand Down Expand Up @@ -172,18 +152,6 @@ public function popupAction(): Response
return $controller->run();
}

/**
* @Route("/contao/switch", name="contao_backend_switch")
*/
public function switchAction(): Response
{
$this->initializeContaoFramework();

$controller = new BackendSwitch();

return $controller->run();
}

/**
* @Route("/contao/alerts", name="contao_backend_alerts")
*/
Expand Down
100 changes: 100 additions & 0 deletions core-bundle/src/Controller/BackendPreviewController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

declare(strict_types=1);

/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/

namespace Contao\CoreBundle\Controller;

use Contao\CoreBundle\Event\PreviewUrlConvertEvent;
use Contao\CoreBundle\Security\Authentication\FrontendPreviewAuthenticator;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

/**
* This controller handles the back end preview call and redirects to the
* requested front end page while ensuring that the /preview.php entry point is
* used. When requested, the front end user gets authenticated.
*
* @Route(defaults={"_scope" = "backend"})
*/
class BackendPreviewController
{
/**
* @var string
*/
private $previewScript;

/**
* @var FrontendPreviewAuthenticator
*/
private $previewAuthenticator;

/**
* @var EventDispatcherInterface
*/
private $dispatcher;

/**
* @var RouterInterface
*/
private $router;

/**
* @var AuthorizationCheckerInterface
*/
private $authorizationChecker;

public function __construct(string $previewScript, FrontendPreviewAuthenticator $previewAuthenticator, EventDispatcherInterface $dispatcher, RouterInterface $router, AuthorizationCheckerInterface $authorizationChecker)
{
$this->previewScript = $previewScript;
$this->previewAuthenticator = $previewAuthenticator;
$this->dispatcher = $dispatcher;
$this->router = $router;
$this->authorizationChecker = $authorizationChecker;
}

/**
* @Route("/contao/preview", name="contao_backend_preview")
*/
public function __invoke(Request $request): Response
{
if ($request->getScriptName() !== $this->previewScript) {
return new RedirectResponse($this->previewScript.$request->getRequestUri());
}

if (!$this->authorizationChecker->isGranted('ROLE_USER')) {
richardhj marked this conversation as resolved.
Show resolved Hide resolved
return new Response('Access denied', Response::HTTP_FORBIDDEN);
}

// Switch to a particular member (see contao/core#6546)
if (
($frontendUser = $request->query->get('user'))
&& !$this->previewAuthenticator->authenticateFrontendUser($frontendUser, false)
) {
$this->previewAuthenticator->removeFrontendAuthentication();
}

$urlConvertEvent = new PreviewUrlConvertEvent($request);

$this->dispatcher->dispatch($urlConvertEvent);

if (null !== $targetUrl = $urlConvertEvent->getUrl()) {
return new RedirectResponse($targetUrl);
}

return new RedirectResponse($this->router->generate('contao_root', [], UrlGeneratorInterface::ABSOLUTE_URL));
}
}
Loading