2013-10-03 16:20:32 +02:00
|
|
|
<?php
|
2015-12-07 05:25:27 +01:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Control;
|
|
|
|
|
|
|
|
use SilverStripe\Core\Convert;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataModel;
|
|
|
|
use SilverStripe\ORM\Versioning\Versioned;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\Security;
|
|
|
|
|
2013-10-03 16:20:32 +02:00
|
|
|
/**
|
|
|
|
* Initialises the versioned stage when a request is made.
|
|
|
|
*/
|
2014-03-04 05:43:17 +01:00
|
|
|
class VersionedRequestFilter implements RequestFilter {
|
2013-10-03 16:20:32 +02:00
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
public function preRequest(HTTPRequest $request, Session $session, DataModel $model) {
|
2015-12-07 05:25:27 +01:00
|
|
|
// Bootstrap session so that Session::get() accesses the right instance
|
|
|
|
$dummyController = new Controller();
|
|
|
|
$dummyController->setSession($session);
|
|
|
|
$dummyController->setRequest($request);
|
|
|
|
$dummyController->pushCurrent();
|
|
|
|
|
|
|
|
// Block non-authenticated users from setting the stage mode
|
|
|
|
if(!Versioned::can_choose_site_stage($request)) {
|
|
|
|
$permissionMessage = sprintf(
|
|
|
|
_t(
|
|
|
|
"ContentController.DRAFT_SITE_ACCESS_RESTRICTION",
|
|
|
|
'You must log in with your CMS password in order to view the draft or archived content. '.
|
|
|
|
'<a href="%s">Click here to go back to the published site.</a>'
|
|
|
|
),
|
2016-05-03 13:13:01 +02:00
|
|
|
Convert::raw2xml(Controller::join_links(Director::baseURL(), $request->getURL(), "?stage=Live"))
|
2015-12-07 05:25:27 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Force output since RequestFilter::preRequest doesn't support response overriding
|
|
|
|
$response = Security::permissionFailure($dummyController, $permissionMessage);
|
|
|
|
$session->inst_save();
|
|
|
|
$dummyController->popCurrent();
|
|
|
|
// Prevent output in testing
|
2016-08-19 00:51:35 +02:00
|
|
|
if(class_exists('SilverStripe\\Dev\\SapphireTest', false) && SapphireTest::is_running_test()) {
|
2016-09-09 08:43:05 +02:00
|
|
|
throw new HTTPResponse_Exception($response);
|
2015-12-07 05:25:27 +01:00
|
|
|
}
|
|
|
|
$response->output();
|
|
|
|
die;
|
|
|
|
}
|
|
|
|
|
|
|
|
Versioned::choose_site_stage();
|
|
|
|
$dummyController->popCurrent();
|
2014-03-04 05:43:17 +01:00
|
|
|
return true;
|
2013-10-03 16:20:32 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
public function postRequest(HTTPRequest $request, HTTPResponse $response, DataModel $model) {
|
2014-03-04 05:43:17 +01:00
|
|
|
return true;
|
2013-10-03 16:20:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|