forked from silverstripe/silverstripe-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request silverstripe#17 from open-sausages/director-middle…
…ware-but-damian API Update for HTTPMiddleware
- Loading branch information
Showing
4 changed files
with
73 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Versioned; | ||
|
||
use SilverStripe\Control\Controller; | ||
use SilverStripe\Control\Director; | ||
use SilverStripe\Control\HTTPRequest; | ||
use SilverStripe\Control\HTTPResponse; | ||
use SilverStripe\Control\Middleware\HTTPMiddleware; | ||
use SilverStripe\Core\Convert; | ||
use SilverStripe\Security\Security; | ||
|
||
/** | ||
* Initialises the versioned stage when a request is made. | ||
*/ | ||
class VersionedHTTPMiddleware implements HTTPMiddleware | ||
{ | ||
public function process(HTTPRequest $request, callable $next) | ||
{ | ||
// Ensure Controller::curr() is available | ||
$dummyController = new Controller(); | ||
$dummyController->setRequest($request); | ||
$dummyController->pushCurrent(); | ||
|
||
// Permission check | ||
try { | ||
$result = $this->checkPermissions($request); | ||
if ($result instanceof HTTPResponse) { | ||
return $result; | ||
} else { | ||
// Set stage | ||
Versioned::choose_site_stage($request); | ||
} | ||
} finally { | ||
// Reset dummy controller | ||
$dummyController->popCurrent(); | ||
} | ||
|
||
// Process | ||
return $next($request); | ||
} | ||
|
||
/** | ||
* @param HTTPRequest $request | ||
* @return HTTPResponse|true True if ok, httpresponse if error | ||
*/ | ||
protected function checkPermissions(HTTPRequest $request) | ||
{ | ||
// Block non-authenticated users from setting the stage mode | ||
if (Versioned::can_choose_site_stage($request)) { | ||
return true; | ||
} | ||
|
||
// Build error message | ||
$link = Convert::raw2xml(Controller::join_links(Director::baseURL(), $request->getURL(), "?stage=Live")); | ||
$permissionMessage = _t( | ||
__CLASS__.'.DRAFT_SITE_ACCESS_RESTRICTION', | ||
'You must log in with your CMS password in order to view the draft or archived content. ' | ||
. '<a href="{link}">Click here to go back to the published site.</a>', | ||
[ 'link' => $link ] | ||
); | ||
|
||
// Force output since RequestFilter::preRequest doesn't support response overriding | ||
return Security::permissionFailure(null, $permissionMessage); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters