2008-08-09 05:19:54 +02:00
|
|
|
<?php
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataModel;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\Security;
|
|
|
|
use SilverStripe\Security\PermissionFailureException;
|
|
|
|
use SilverStripe\Security\Permission;
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
/**
|
2012-03-24 04:38:57 +01:00
|
|
|
* This class is the base class of any SilverStripe object that can be used to handle HTTP requests.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-10-30 23:03:21 +01:00
|
|
|
* Any RequestHandler object can be made responsible for handling its own segment of the URL namespace.
|
2008-08-09 05:19:54 +02:00
|
|
|
* The {@link Director} begins the URL parsing process; it will parse the beginning of the URL to identify which
|
2012-09-26 23:34:00 +02:00
|
|
|
* controller is being used. It will then call {@link handleRequest()} on that Controller, passing it the parameters
|
|
|
|
* that it parsed from the URL, and the {@link SS_HTTPRequest} that contains the remainder of the URL to be parsed.
|
2008-10-06 00:16:07 +02:00
|
|
|
*
|
|
|
|
* You can use ?debug_request=1 to view information about the different components and rule matches for a specific URL.
|
2008-08-09 05:19:54 +02:00
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* In SilverStripe, URL parsing is distributed throughout the object graph. For example, suppose that we have a
|
|
|
|
* search form that contains a {@link TreeMultiSelectField} named "Groups". We want to use ajax to load segments of
|
|
|
|
* this tree as they are needed rather than downloading the tree right at the beginning. We could use this URL to get
|
|
|
|
* the tree segment that appears underneath
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-10-06 00:16:07 +02:00
|
|
|
* Group #36: "admin/crm/SearchForm/field/Groups/treesegment/36"
|
2008-08-09 05:19:54 +02:00
|
|
|
* - Director will determine that admin/crm is controlled by a new ModelAdmin object, and pass control to that.
|
2008-10-06 00:16:07 +02:00
|
|
|
* Matching Director Rule: "admin/crm" => "ModelAdmin" (defined in mysite/_config.php)
|
2012-09-26 23:34:00 +02:00
|
|
|
* - ModelAdmin will determine that SearchForm is controlled by a Form object returned by $this->SearchForm(), and
|
|
|
|
* pass control to that.
|
2008-10-30 23:03:21 +01:00
|
|
|
* Matching $url_handlers: "$Action" => "$Action" (defined in RequestHandler class)
|
2012-09-26 23:34:00 +02:00
|
|
|
* - Form will determine that field/Groups is controlled by the Groups field, a TreeMultiselectField, and pass
|
|
|
|
* control to that.
|
2008-10-06 00:16:07 +02:00
|
|
|
* Matching $url_handlers: 'field/$FieldName!' => 'handleField' (defined in Form class)
|
2012-09-26 23:34:00 +02:00
|
|
|
* - TreeMultiselectField will determine that treesegment/36 is handled by its treesegment() method. This method
|
|
|
|
* will return an HTML fragment that is output to the screen.
|
2008-10-06 00:16:07 +02:00
|
|
|
* Matching $url_handlers: "$Action/$ID" => "handleItem" (defined in TreeMultiSelectField class)
|
2008-08-09 05:19:54 +02:00
|
|
|
*
|
2008-10-30 23:03:21 +01:00
|
|
|
* {@link RequestHandler::handleRequest()} is where this behaviour is implemented.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-03-22 23:59:14 +01:00
|
|
|
* @subpackage control
|
2008-08-09 05:19:54 +02:00
|
|
|
*/
|
2008-10-30 23:03:21 +01:00
|
|
|
class RequestHandler extends ViewableData {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-16 03:36:31 +01:00
|
|
|
/**
|
|
|
|
* @var SS_HTTPRequest $request The request object that the controller was called with.
|
|
|
|
* Set in {@link handleRequest()}. Useful to generate the {}
|
|
|
|
*/
|
2008-08-09 05:54:55 +02:00
|
|
|
protected $request = null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-01 07:33:02 +02:00
|
|
|
/**
|
|
|
|
* The DataModel for this request
|
|
|
|
*/
|
|
|
|
protected $model = null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 09:28:53 +02:00
|
|
|
/**
|
|
|
|
* This variable records whether RequestHandler::__construct()
|
|
|
|
* was called or not. Useful for checking if subclasses have
|
|
|
|
* called parent::__construct()
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $brokenOnConstruct = true;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
/**
|
|
|
|
* The default URL handling rules. This specifies that the next component of the URL corresponds to a method to
|
|
|
|
* be called on this RequestHandlingData object.
|
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* The keys of this array are parse rules. See {@link SS_HTTPRequest::match()} for a description of the rules
|
|
|
|
* available.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* The values of the array are the method to be called if the rule matches. If this value starts with a '$', then
|
|
|
|
* the named parameter of the parsed URL wil be used to determine the method name.
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2008-08-09 05:19:54 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $url_handlers = array(
|
2008-08-09 05:19:54 +02:00
|
|
|
'$Action' => '$Action',
|
|
|
|
);
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
/**
|
|
|
|
* Define a list of action handling methods that are allowed to be called directly by URLs.
|
|
|
|
* The variable should be an array of action names. This sample shows the different values that it can contain:
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* array(
|
2012-09-26 23:34:00 +02:00
|
|
|
* // someaction can be accessed by anyone, any time
|
2014-08-15 08:53:05 +02:00
|
|
|
* 'someaction',
|
2012-09-26 23:34:00 +02:00
|
|
|
* // So can otheraction
|
2014-08-15 08:53:05 +02:00
|
|
|
* 'otheraction' => true,
|
2012-09-26 23:34:00 +02:00
|
|
|
* // restrictedaction can only be people with ADMIN privilege
|
2014-08-15 08:53:05 +02:00
|
|
|
* 'restrictedaction' => 'ADMIN',
|
2012-09-26 23:34:00 +02:00
|
|
|
* // complexaction can only be accessed if $this->canComplexAction() returns true
|
2014-08-15 08:53:05 +02:00
|
|
|
* 'complexaction' '->canComplexAction'
|
2008-08-09 05:19:54 +02:00
|
|
|
* );
|
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-12-20 01:00:38 +01:00
|
|
|
* Form getters count as URL actions as well, and should be included in allowed_actions.
|
2013-01-15 00:34:05 +01:00
|
|
|
* Form actions on the other handed (first argument to {@link FormAction()} should NOT be included,
|
2010-12-20 01:00:38 +01:00
|
|
|
* these are handled separately through {@link Form->httpSubmission}. You can control access on form actions
|
|
|
|
* either by conditionally removing {@link FormAction} in the form construction,
|
|
|
|
* or by defining $allowed_actions in your {@link Form} class.
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2008-08-09 05:19:54 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $allowed_actions = null;
|
2013-06-20 11:40:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var boolean Enforce presence of $allowed_actions when checking acccess.
|
|
|
|
* Defaults to TRUE, meaning all URL actions will be denied.
|
|
|
|
* When set to FALSE, the controller will allow *all* public methods to be called.
|
2014-08-15 08:53:05 +02:00
|
|
|
* In most cases this isn't desireable, and in fact a security risk,
|
2013-06-20 11:40:55 +02:00
|
|
|
* since some helper methods can cause side effects which shouldn't be exposed through URLs.
|
|
|
|
*/
|
|
|
|
private static $require_allowed_actions = true;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 09:28:53 +02:00
|
|
|
public function __construct() {
|
|
|
|
$this->brokenOnConstruct = false;
|
2010-12-16 05:06:13 +01:00
|
|
|
|
|
|
|
// Check necessary to avoid class conflicts before manifest is rebuilt
|
2015-04-30 01:04:08 +02:00
|
|
|
if(class_exists('NullHTTPRequest')) $this->setRequest(new NullHTTPRequest());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-01 04:43:52 +02:00
|
|
|
// This will prevent bugs if setDataModel() isn't called.
|
2011-05-01 07:33:02 +02:00
|
|
|
$this->model = DataModel::inst();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 09:28:53 +02:00
|
|
|
parent::__construct();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-01 07:33:02 +02:00
|
|
|
/**
|
|
|
|
* Set the DataModel for this request.
|
2016-06-23 01:37:22 +02:00
|
|
|
*
|
|
|
|
* @param DataModel $model
|
2011-05-01 07:33:02 +02:00
|
|
|
*/
|
2012-05-01 04:43:52 +02:00
|
|
|
public function setDataModel($model) {
|
2011-05-01 07:33:02 +02:00
|
|
|
$this->model = $model;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
/**
|
|
|
|
* Handles URL requests.
|
|
|
|
*
|
|
|
|
* - ViewableData::handleRequest() iterates through each rule in {@link self::$url_handlers}.
|
|
|
|
* - If the rule matches, the named method will be called.
|
2014-08-15 08:53:05 +02:00
|
|
|
* - If there is still more URL to be processed, then handleRequest()
|
2009-09-07 02:14:11 +02:00
|
|
|
* is called on the object that that method returns.
|
2008-08-09 05:19:54 +02:00
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* Once all of the URL has been processed, the final result is returned.
|
2009-09-07 02:14:11 +02:00
|
|
|
* However, if the final result is an array, this
|
2014-08-15 08:53:05 +02:00
|
|
|
* array is interpreted as being additional template data to customise the
|
2009-09-07 02:14:11 +02:00
|
|
|
* 2nd to last result with, rather than an object
|
2014-08-15 08:53:05 +02:00
|
|
|
* in its own right. This is most frequently used when a Controller's
|
2009-09-07 02:14:11 +02:00
|
|
|
* action will return an array of data with which to
|
2008-08-09 05:19:54 +02:00
|
|
|
* customise the controller.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param SS_HTTPRequest $request The object that is reponsible for distributing URL parsing
|
|
|
|
* @param DataModel $model
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* @return SS_HTTPResponse|RequestHandler|string|array
|
2008-08-09 05:19:54 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function handleRequest(SS_HTTPRequest $request, DataModel $model) {
|
2008-10-30 23:28:01 +01:00
|
|
|
// $handlerClass is used to step up the class hierarchy to implement url_handlers inheritance
|
2008-11-05 02:59:27 +01:00
|
|
|
$handlerClass = ($this->class) ? $this->class : get_class($this);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 09:28:53 +02:00
|
|
|
if($this->brokenOnConstruct) {
|
|
|
|
user_error("parent::__construct() needs to be called on {$handlerClass}::__construct()", E_USER_WARNING);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-04-30 01:04:08 +02:00
|
|
|
$this->setRequest($request);
|
2012-05-01 04:43:52 +02:00
|
|
|
$this->setDataModel($model);
|
2013-01-24 20:44:39 +01:00
|
|
|
|
|
|
|
$match = $this->findAction($request);
|
|
|
|
|
|
|
|
// If nothing matches, return this object
|
|
|
|
if (!$match) return $this;
|
|
|
|
|
|
|
|
// Start to find what action to call. Start by using what findAction returned
|
|
|
|
$action = $match['action'];
|
|
|
|
|
|
|
|
// We used to put "handleAction" as the action on controllers, but (a) this could only be called when
|
|
|
|
// you had $Action in your rule, and (b) RequestHandler didn't have one. $Action is better
|
|
|
|
if ($action == 'handleAction') {
|
2013-03-22 13:30:30 +01:00
|
|
|
// TODO Fix LeftAndMain usage
|
|
|
|
// Deprecation::notice('3.2.0', 'Calling handleAction directly is deprecated - use $Action instead');
|
2013-01-24 20:44:39 +01:00
|
|
|
$action = '$Action';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actions can reference URL parameters, eg, '$Action/$ID/$OtherID' => '$Action',
|
|
|
|
if($action[0] == '$') {
|
|
|
|
$action = str_replace("-", "_", $request->latestParam(substr($action,1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$action) {
|
|
|
|
if(isset($_REQUEST['debug_request'])) {
|
|
|
|
Debug::message("Action not set; using default action method name 'index'");
|
|
|
|
}
|
|
|
|
$action = "index";
|
|
|
|
} else if(!is_string($action)) {
|
|
|
|
user_error("Non-string method name: " . var_export($action, true), E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
2015-11-09 23:27:49 +01:00
|
|
|
$classMessage = Director::isLive() ? 'on this handler' : 'on class '.get_class($this);
|
2013-01-24 20:44:39 +01:00
|
|
|
|
|
|
|
try {
|
2013-04-09 16:43:50 +02:00
|
|
|
if(!$this->hasAction($action)) {
|
2015-11-09 23:27:49 +01:00
|
|
|
return $this->httpError(404, "Action '$action' isn't available $classMessage.");
|
2013-04-09 16:43:50 +02:00
|
|
|
}
|
2015-02-23 14:46:00 +01:00
|
|
|
if(!$this->checkAccessAction($action) || in_array(strtolower($action), array('run', 'doInit'))) {
|
2015-11-09 23:27:49 +01:00
|
|
|
return $this->httpError(403, "Action '$action' isn't allowed $classMessage.");
|
2013-04-09 16:43:50 +02:00
|
|
|
}
|
2013-01-24 20:44:39 +01:00
|
|
|
$result = $this->handleAction($request, $action);
|
|
|
|
}
|
|
|
|
catch (SS_HTTPResponse_Exception $e) {
|
|
|
|
return $e->getResponse();
|
|
|
|
}
|
|
|
|
catch(PermissionFailureException $e) {
|
|
|
|
$result = Security::permissionFailure(null, $e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
if($result instanceof SS_HTTPResponse && $result->isError()) {
|
|
|
|
if(isset($_REQUEST['debug_request'])) Debug::message("Rule resulted in HTTP error; breaking");
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we return a RequestHandler, call handleRequest() on that, even if there is no more URL to
|
|
|
|
// parse. It might have its own handler. However, we only do this if we haven't just parsed an
|
|
|
|
// empty rule ourselves, to prevent infinite loops. Also prevent further handling of controller
|
|
|
|
// actions which return themselves to avoid infinite loops.
|
|
|
|
$matchedRuleWasEmpty = $request->isEmptyPattern($match['rule']);
|
|
|
|
$resultIsRequestHandler = is_object($result) && $result instanceof RequestHandler;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-01-24 20:44:39 +01:00
|
|
|
if($this !== $result && !$matchedRuleWasEmpty && $resultIsRequestHandler) {
|
|
|
|
$returnValue = $result->handleRequest($request, $model);
|
|
|
|
|
|
|
|
// Array results can be used to handle
|
|
|
|
if(is_array($returnValue)) $returnValue = $this->customise($returnValue);
|
|
|
|
|
|
|
|
return $returnValue;
|
|
|
|
|
|
|
|
// If we return some other data, and all the URL is parsed, then return that
|
|
|
|
} else if($request->allParsed()) {
|
|
|
|
return $result;
|
|
|
|
|
|
|
|
// But if we have more content on the URL and we don't know what to do with it, return an error.
|
|
|
|
} else {
|
2015-11-09 23:27:49 +01:00
|
|
|
return $this->httpError(404, "I can't handle sub-URLs $classMessage.");
|
2013-01-24 20:44:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
/**
|
|
|
|
* @param SS_HTTPRequest $request
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-01-24 20:44:39 +01:00
|
|
|
protected function findAction($request) {
|
|
|
|
$handlerClass = ($this->class) ? $this->class : get_class($this);
|
|
|
|
|
2008-10-30 23:28:01 +01:00
|
|
|
// We stop after RequestHandler; in other words, at ViewableData
|
2008-11-05 02:59:27 +01:00
|
|
|
while($handlerClass && $handlerClass != 'ViewableData') {
|
2013-01-24 20:44:39 +01:00
|
|
|
$urlHandlers = Config::inst()->get($handlerClass, 'url_handlers', Config::UNINHERITED);
|
2012-04-18 13:10:57 +02:00
|
|
|
|
2008-10-30 23:28:01 +01:00
|
|
|
if($urlHandlers) foreach($urlHandlers as $rule => $action) {
|
2012-09-26 23:34:00 +02:00
|
|
|
if(isset($_REQUEST['debug_request'])) {
|
|
|
|
Debug::message("Testing '$rule' with '" . $request->remaining() . "' on $this->class");
|
|
|
|
}
|
2013-01-24 20:44:39 +01:00
|
|
|
|
|
|
|
if($request->match($rule, true)) {
|
2008-10-30 23:28:01 +01:00
|
|
|
if(isset($_REQUEST['debug_request'])) {
|
2013-01-24 20:44:39 +01:00
|
|
|
Debug::message(
|
|
|
|
"Rule '$rule' matched to action '$action' on $this->class. ".
|
|
|
|
"Latest request params: " . var_export($request->latestParams(), true)
|
|
|
|
);
|
2008-10-30 23:28:01 +01:00
|
|
|
}
|
2013-01-24 20:44:39 +01:00
|
|
|
|
|
|
|
return array('rule' => $rule, 'action' => $action);
|
2008-10-30 23:28:01 +01:00
|
|
|
}
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
2013-01-24 20:44:39 +01:00
|
|
|
|
2008-10-30 23:28:01 +01:00
|
|
|
$handlerClass = get_parent_class($handlerClass);
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
2013-01-24 20:44:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a request, and an action name, call that action name on this RequestHandler
|
|
|
|
*
|
|
|
|
* Must not raise SS_HTTPResponse_Exceptions - instead it should return
|
|
|
|
*
|
|
|
|
* @param $request
|
|
|
|
* @param $action
|
|
|
|
* @return SS_HTTPResponse
|
|
|
|
*/
|
|
|
|
protected function handleAction($request, $action) {
|
2015-11-09 23:27:49 +01:00
|
|
|
$classMessage = Director::isLive() ? 'on this handler' : 'on class '.get_class($this);
|
2013-01-24 20:44:39 +01:00
|
|
|
|
|
|
|
if(!$this->hasMethod($action)) {
|
2015-11-09 23:27:49 +01:00
|
|
|
return new SS_HTTPResponse("Action '$action' isn't available $classMessage.", 404);
|
2013-01-24 20:44:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$res = $this->extend('beforeCallActionHandler', $request, $action);
|
|
|
|
if ($res) return reset($res);
|
|
|
|
|
|
|
|
$actionRes = $this->$action($request);
|
|
|
|
|
2016-03-13 20:36:47 +01:00
|
|
|
$res = $this->extend('afterCallActionHandler', $request, $action, $actionRes);
|
2013-01-24 20:44:39 +01:00
|
|
|
if ($res) return reset($res);
|
|
|
|
|
|
|
|
return $actionRes;
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:07:26 +02:00
|
|
|
/**
|
2013-02-18 02:53:33 +01:00
|
|
|
* Get a array of allowed actions defined on this controller,
|
|
|
|
* any parent classes or extensions.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-02-18 02:53:33 +01:00
|
|
|
* Caution: Since 3.1, allowed_actions definitions only apply
|
|
|
|
* to methods on the controller they're defined on,
|
|
|
|
* so it is recommended to use the $class argument
|
|
|
|
* when invoking this method.
|
2009-10-11 02:07:26 +02:00
|
|
|
*
|
2015-10-06 07:28:04 +02:00
|
|
|
* @param string $limitToClass
|
2009-10-11 02:07:26 +02:00
|
|
|
* @return array|null
|
|
|
|
*/
|
2013-02-18 02:53:33 +01:00
|
|
|
public function allowedActions($limitToClass = null) {
|
|
|
|
if($limitToClass) {
|
|
|
|
$actions = Config::inst()->get(
|
2014-08-15 08:53:05 +02:00
|
|
|
$limitToClass,
|
|
|
|
'allowed_actions',
|
2013-02-18 02:53:33 +01:00
|
|
|
Config::UNINHERITED | Config::EXCLUDE_EXTRA_SOURCES
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$actions = Config::inst()->get(get_class($this), 'allowed_actions');
|
|
|
|
}
|
2011-12-22 03:31:56 +01:00
|
|
|
|
2013-02-18 02:53:33 +01:00
|
|
|
if(is_array($actions)) {
|
|
|
|
if(array_key_exists('*', $actions)) {
|
|
|
|
Deprecation::notice(
|
2014-08-15 08:53:05 +02:00
|
|
|
'3.0',
|
2013-02-18 02:53:33 +01:00
|
|
|
'Wildcards (*) are no longer valid in $allowed_actions due their ambiguous '
|
|
|
|
. ' and potentially insecure behaviour. Please define all methods explicitly instead.'
|
|
|
|
);
|
|
|
|
}
|
2012-03-09 04:39:12 +01:00
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
// convert all keys and values to lowercase to
|
2009-10-15 23:50:02 +02:00
|
|
|
// allow for easier comparison, unless it is a permission code
|
|
|
|
$actions = array_change_key_case($actions, CASE_LOWER);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:07:26 +02:00
|
|
|
foreach($actions as $key => $value) {
|
|
|
|
if(is_numeric($key)) $actions[$key] = strtolower($value);
|
|
|
|
}
|
2012-03-09 04:39:12 +01:00
|
|
|
|
2009-10-11 02:07:26 +02:00
|
|
|
return $actions;
|
2013-02-18 02:53:33 +01:00
|
|
|
} else {
|
|
|
|
return null;
|
2009-10-11 02:07:26 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:07:23 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Checks if this request handler has a specific action,
|
2013-02-18 02:53:33 +01:00
|
|
|
* even if the current user cannot access it.
|
|
|
|
* Includes class ancestry and extensions in the checks.
|
2009-10-11 02:07:23 +02:00
|
|
|
*
|
|
|
|
* @param string $action
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasAction($action) {
|
|
|
|
if($action == 'index') return true;
|
2013-02-18 02:53:33 +01:00
|
|
|
|
|
|
|
// Don't allow access to any non-public methods (inspect instance plus all extensions)
|
2015-10-06 07:28:08 +02:00
|
|
|
$insts = array_merge(array($this), (array) $this->getExtensionInstances());
|
2013-02-18 02:53:33 +01:00
|
|
|
foreach($insts as $inst) {
|
|
|
|
if(!method_exists($inst, $action)) continue;
|
|
|
|
$r = new ReflectionClass(get_class($inst));
|
|
|
|
$m = $r->getMethod($action);
|
|
|
|
if(!$m || !$m->isPublic()) return false;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:07:23 +02:00
|
|
|
$action = strtolower($action);
|
2009-10-11 02:07:26 +02:00
|
|
|
$actions = $this->allowedActions();
|
2012-06-14 08:45:12 +02:00
|
|
|
|
2013-02-18 02:53:33 +01:00
|
|
|
// Check if the action is defined in the allowed actions of any ancestry class
|
|
|
|
// as either a key or value. Note that if the action is numeric, then keys are not
|
|
|
|
// searched for actions to prevent actual array keys being recognised as actions.
|
2009-10-11 02:07:23 +02:00
|
|
|
if(is_array($actions)) {
|
2010-10-13 05:30:54 +02:00
|
|
|
$isKey = !is_numeric($action) && array_key_exists($action, $actions);
|
2012-06-14 08:45:12 +02:00
|
|
|
$isValue = in_array($action, $actions, true);
|
2013-02-18 02:53:33 +01:00
|
|
|
if($isKey || $isValue) return true;
|
2009-10-11 02:07:23 +02:00
|
|
|
}
|
2012-06-14 08:45:12 +02:00
|
|
|
|
2013-02-18 02:53:33 +01:00
|
|
|
$actionsWithoutExtra = $this->config()->get(
|
|
|
|
'allowed_actions',
|
|
|
|
Config::UNINHERITED | Config::EXCLUDE_EXTRA_SOURCES
|
|
|
|
);
|
|
|
|
if(!is_array($actions) || !$actionsWithoutExtra) {
|
2015-02-23 14:46:00 +01:00
|
|
|
if($action != 'doInit' && $action != 'run' && method_exists($this, $action)) return true;
|
2009-10-11 02:07:23 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:07:23 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-06-19 10:11:50 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the class that defines the given action, so that we know where to check allowed_actions.
|
2016-06-23 01:37:22 +02:00
|
|
|
*
|
|
|
|
* @param string $actionOrigCasing
|
|
|
|
* @return string
|
2013-06-19 10:11:50 +02:00
|
|
|
*/
|
|
|
|
protected function definingClassForAction($actionOrigCasing) {
|
|
|
|
$action = strtolower($actionOrigCasing);
|
|
|
|
|
|
|
|
$definingClass = null;
|
2015-10-06 07:28:08 +02:00
|
|
|
$insts = array_merge(array($this), (array) $this->getExtensionInstances());
|
2013-06-19 10:11:50 +02:00
|
|
|
foreach($insts as $inst) {
|
|
|
|
if(!method_exists($inst, $action)) continue;
|
|
|
|
$r = new ReflectionClass(get_class($inst));
|
|
|
|
$m = $r->getMethod($actionOrigCasing);
|
2014-08-15 08:53:05 +02:00
|
|
|
return $m->getDeclaringClass()->getName();
|
2013-06-19 10:11:50 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
/**
|
|
|
|
* Check that the given action is allowed to be called from a URL.
|
|
|
|
* It will interrogate {@link self::$allowed_actions} to determine this.
|
2016-06-23 01:37:22 +02:00
|
|
|
*
|
|
|
|
* @param string $action
|
|
|
|
* @return bool
|
|
|
|
* @throws Exception
|
2008-08-09 05:19:54 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function checkAccessAction($action) {
|
2010-10-13 03:24:15 +02:00
|
|
|
$actionOrigCasing = $action;
|
2013-02-18 02:53:33 +01:00
|
|
|
$action = strtolower($action);
|
2009-10-11 07:16:18 +02:00
|
|
|
|
2013-02-18 02:53:33 +01:00
|
|
|
$isAllowed = false;
|
|
|
|
$isDefined = false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-19 10:11:50 +02:00
|
|
|
// Get actions for this specific class (without inheritance)
|
|
|
|
$definingClass = $this->definingClassForAction($actionOrigCasing);
|
2014-08-15 08:53:05 +02:00
|
|
|
$allowedActions = $this->allowedActions($definingClass);
|
2013-06-19 10:11:50 +02:00
|
|
|
|
|
|
|
// check if specific action is set
|
|
|
|
if(isset($allowedActions[$action])) {
|
|
|
|
$isDefined = true;
|
|
|
|
$test = $allowedActions[$action];
|
|
|
|
if($test === true || $test === 1 || $test === '1') {
|
|
|
|
// TRUE should always allow access
|
2013-02-18 02:53:33 +01:00
|
|
|
$isAllowed = true;
|
2013-06-19 10:11:50 +02:00
|
|
|
} elseif(substr($test, 0, 2) == '->') {
|
|
|
|
// Determined by custom method with "->" prefix
|
|
|
|
list($method, $arguments) = Object::parse_class_spec(substr($test, 2));
|
|
|
|
$isAllowed = call_user_func_array(array($this, $method), $arguments);
|
|
|
|
} else {
|
|
|
|
// Value is a permission code to check the current member against
|
|
|
|
$isAllowed = Permission::check($test);
|
2010-10-13 03:24:15 +02:00
|
|
|
}
|
2013-06-19 10:11:50 +02:00
|
|
|
} elseif(
|
2014-08-15 08:53:05 +02:00
|
|
|
is_array($allowedActions)
|
|
|
|
&& (($key = array_search($action, $allowedActions, true)) !== false)
|
2013-06-19 10:11:50 +02:00
|
|
|
&& is_numeric($key)
|
|
|
|
) {
|
|
|
|
// Allow numeric array notation (search for array value as action instead of key)
|
|
|
|
$isDefined = true;
|
|
|
|
$isAllowed = true;
|
|
|
|
} elseif(is_array($allowedActions) && !count($allowedActions)) {
|
|
|
|
// If defined as empty array, deny action
|
|
|
|
$isAllowed = false;
|
|
|
|
} elseif($allowedActions === null) {
|
2013-06-20 11:40:55 +02:00
|
|
|
// If undefined, allow action based on configuration
|
|
|
|
$isAllowed = !Config::inst()->get('RequestHandler', 'require_allowed_actions');
|
2013-06-19 10:11:50 +02:00
|
|
|
}
|
2013-02-18 02:53:33 +01:00
|
|
|
|
2013-06-19 10:11:50 +02:00
|
|
|
// If we don't have a match in allowed_actions,
|
2013-06-20 11:40:55 +02:00
|
|
|
// whitelist the 'index' action as well as undefined actions based on configuration.
|
2013-06-19 10:11:50 +02:00
|
|
|
if(!$isDefined && ($action == 'index' || empty($action))) {
|
2013-02-18 02:53:33 +01:00
|
|
|
$isAllowed = true;
|
2008-10-31 03:16:25 +01:00
|
|
|
}
|
2013-02-18 02:53:33 +01:00
|
|
|
|
|
|
|
return $isAllowed;
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
/**
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* Throws a HTTP error response encased in a {@link SS_HTTPResponse_Exception}, which is later caught in
|
2009-06-27 10:48:44 +02:00
|
|
|
* {@link RequestHandler::handleAction()} and returned to the user.
|
|
|
|
*
|
|
|
|
* @param int $errorCode
|
2010-12-02 09:03:17 +01:00
|
|
|
* @param string $errorMessage Plaintext error message
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* @uses SS_HTTPResponse_Exception
|
2016-01-26 04:56:07 +01:00
|
|
|
* @throws SS_HTTPResponse_Exception
|
2008-08-09 05:19:54 +02:00
|
|
|
*/
|
2009-06-27 10:48:44 +02:00
|
|
|
public function httpError($errorCode, $errorMessage = null) {
|
2015-04-30 01:04:08 +02:00
|
|
|
|
|
|
|
$request = $this->getRequest();
|
|
|
|
|
2012-09-27 02:26:25 +02:00
|
|
|
// Call a handler method such as onBeforeHTTPError404
|
2016-07-25 06:56:23 +02:00
|
|
|
$this->extend("onBeforeHTTPError{$errorCode}", $request);
|
2010-12-02 09:03:17 +01:00
|
|
|
|
2012-09-27 02:26:25 +02:00
|
|
|
// Call a handler method such as onBeforeHTTPError, passing 404 as the first arg
|
2015-04-30 01:04:08 +02:00
|
|
|
$this->extend('onBeforeHTTPError', $errorCode, $request);
|
2010-12-02 09:03:17 +01:00
|
|
|
|
2012-09-27 02:26:25 +02:00
|
|
|
// Throw a new exception
|
|
|
|
throw new SS_HTTPResponse_Exception($errorMessage, $errorCode);
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
2012-04-05 14:44:42 +02:00
|
|
|
|
2008-08-11 02:14:48 +02:00
|
|
|
/**
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* Returns the SS_HTTPRequest object that this controller is using.
|
2014-08-15 08:53:05 +02:00
|
|
|
* Returns a placeholder {@link NullHTTPRequest} object unless
|
2010-12-16 05:06:13 +01:00
|
|
|
* {@link handleAction()} or {@link handleRequest()} have been called,
|
|
|
|
* which adds a reference to an actual {@link SS_HTTPRequest} object.
|
2008-08-11 02:14:48 +02:00
|
|
|
*
|
2010-12-16 05:06:13 +01:00
|
|
|
* @return SS_HTTPRequest|NullHTTPRequest
|
2008-08-11 02:14:48 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getRequest() {
|
2008-08-11 02:14:48 +02:00
|
|
|
return $this->request;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-16 03:36:31 +01:00
|
|
|
/**
|
|
|
|
* Typically the request is set through {@link handleAction()}
|
|
|
|
* or {@link handleRequest()}, but in some based we want to set it manually.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-12-16 03:36:31 +01:00
|
|
|
* @param SS_HTTPRequest
|
2015-02-23 14:46:00 +01:00
|
|
|
* @return $this
|
2010-12-16 03:36:31 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setRequest($request) {
|
2010-12-16 03:36:31 +01:00
|
|
|
$this->request = $request;
|
2015-02-23 14:46:00 +01:00
|
|
|
return $this;
|
2010-12-16 03:36:31 +01:00
|
|
|
}
|
2009-06-27 10:48:44 +02:00
|
|
|
}
|