Skip to content

Commit

Permalink
Merge pull request #67 from supervillians/issues/session-not-set-on-p…
Browse files Browse the repository at this point in the history
…review

FIX versioned class not found and preview session bugfix
  • Loading branch information
robbieaverill authored Oct 13, 2017
2 parents 6d1a22c + 8e435ed commit e3187a3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
20 changes: 14 additions & 6 deletions src/Controllers/ShareDraftController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

namespace SilverStripe\ShareDraftContent\Controllers;

use BadMethodCallException;
use Exception;
use PageController;
use SilverStripe\CMS\Controllers\ModelAsController;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\Session;
use SilverStripe\ORM\Versioning\Versioned;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ShareDraftContent\Models\ShareToken;
use SilverStripe\Versioned\Versioned;
use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements;

Expand Down Expand Up @@ -48,7 +50,12 @@ public function preview(HTTPRequest $request)
{
$key = $request->param('Key');
$token = $request->param('Token');

try {
$session = $this->getRequest()->getSession();
} catch (BadMethodCallException $e) {
// Create a new session
$session = $this->getRequest()->setSession(Injector::inst()->create(Session::class));
}
/**
* @var ShareToken $shareToken
*/
Expand All @@ -72,16 +79,16 @@ public function preview(HTTPRequest $request)
Requirements::css('silverstripe/sharedraftcontent:css/top-bar.css');

// Temporarily un-secure the draft site and switch to draft
$oldSecured = Session::get('unsecuredDraftSite');
$oldSecured = $session->get('unsecuredDraftSite');
$oldMode = Versioned::get_reading_mode();
$restore = function () use ($oldSecured, $oldMode) {
Session::set('unsecuredDraftSite', $oldSecured);
$restore = function () use ($oldSecured, $oldMode, $session) {
$session->set('unsecuredDraftSite', $oldSecured);
Versioned::set_reading_mode($oldMode);
};

// Process page inside an unsecured draft container
try {
Session::set('unsecuredDraftSite', true);
$session->set('unsecuredDraftSite', true);
Versioned::set_stage('Stage');

// Hack to get around ContentController::init() redirecting on home page
Expand All @@ -90,6 +97,7 @@ public function preview(HTTPRequest $request)
// Create mock request; Simplify request to single top level request
$pageRequest = new HTTPRequest('GET', $page->URLSegment);
$pageRequest->match('$URLSegment//$Action/$ID/$OtherID', true);
$pageRequest->setSession($session);
$rendered = $controller->handleRequest($pageRequest, $this->model);

// Render draft heading
Expand Down
2 changes: 1 addition & 1 deletion src/Extensions/ShareDraftContentSiteTreeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected function getNewShareToken()
))->first();
}

$config = Config::inst()->forClass(__CLASS__);
$config = Config::forClass(__CLASS__);

$validForDays = $config->valid_for_days;

Expand Down

0 comments on commit e3187a3

Please sign in to comment.