2012-06-04 10:14:02 +02:00
|
|
|
<?php
|
2017-11-29 03:20:09 +01:00
|
|
|
|
|
|
|
namespace SilverStripe\RestfulServer;
|
|
|
|
|
|
|
|
use SilverStripe\ORM\ArrayList;
|
2017-12-06 04:18:14 +01:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\ORM\DataList;
|
2017-11-29 03:20:09 +01:00
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\Control\Director;
|
2017-12-06 04:18:14 +01:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
|
|
|
use SilverStripe\ORM\SS_List;
|
2017-11-29 03:20:09 +01:00
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\Security\Security;
|
2017-12-06 04:18:14 +01:00
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
2017-11-29 03:20:09 +01:00
|
|
|
|
2012-06-04 10:14:02 +02:00
|
|
|
/**
|
|
|
|
* Generic RESTful server, which handles webservice access to arbitrary DataObjects.
|
|
|
|
* Relies on serialization/deserialization into different formats provided
|
|
|
|
* by the DataFormatter APIs in core.
|
2017-11-02 22:20:11 +01:00
|
|
|
*
|
2012-06-04 10:14:02 +02:00
|
|
|
* @todo Implement PUT/POST/DELETE for relations
|
2017-11-02 22:20:11 +01:00
|
|
|
* @todo Access-Control for relations (you might be allowed to view Members and Groups,
|
2012-10-31 00:23:29 +01:00
|
|
|
* but not their relation with each other)
|
2012-06-04 10:14:02 +02:00
|
|
|
* @todo Make SearchContext specification customizeable for each class
|
|
|
|
* @todo Allow for range-searches (e.g. on Created column)
|
|
|
|
* @todo Filter relation listings by $api_access and canView() permissions
|
2017-11-02 22:20:11 +01:00
|
|
|
* @todo Exclude relations when "fields" are specified through URL (they should be explicitly
|
2012-10-31 00:23:29 +01:00
|
|
|
* requested in this case)
|
2017-11-02 22:20:11 +01:00
|
|
|
* @todo Custom filters per DataObject subclass, e.g. to disallow showing unpublished pages in
|
2012-10-31 00:23:29 +01:00
|
|
|
* SiteTree/Versioned/Hierarchy
|
2017-11-02 22:20:11 +01:00
|
|
|
* @todo URL parameter namespacing for search-fields, limit, fields, add_fields
|
2012-10-31 00:23:29 +01:00
|
|
|
* (might all be valid dataobject properties)
|
2017-11-02 22:20:11 +01:00
|
|
|
* e.g. you wouldn't be able to search for a "limit" property on your subclass as
|
2012-10-31 00:23:29 +01:00
|
|
|
* its overlayed with the search logic
|
2012-06-04 10:14:02 +02:00
|
|
|
* @todo i18n integration (e.g. Page/1.xml?lang=de_DE)
|
|
|
|
* @todo Access to extendable methods/relations like SiteTree/1/Versions or SiteTree/1/Version/22
|
|
|
|
* @todo Respect $api_access array notation in search contexts
|
|
|
|
*/
|
2015-11-21 07:21:34 +01:00
|
|
|
class RestfulServer extends Controller
|
|
|
|
{
|
2017-11-29 03:20:09 +01:00
|
|
|
private static $url_handlers = array(
|
|
|
|
'$ClassName!/$ID/$Relation' => 'handleAction',
|
|
|
|
'' => 'notFound'
|
2015-11-21 07:21:34 +01:00
|
|
|
);
|
2012-06-04 10:14:02 +02:00
|
|
|
|
2017-12-05 22:49:45 +01:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var string root of the api route, MUST have a trailing slash
|
|
|
|
*/
|
2017-11-29 03:20:09 +01:00
|
|
|
private static $api_base = "api/v1/";
|
2012-06-04 10:14:02 +02:00
|
|
|
|
2017-12-05 22:49:45 +01:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var string Class name for an authenticator to use on API access
|
|
|
|
*/
|
2017-11-29 03:20:09 +01:00
|
|
|
private static $authenticator = BasicRestfulAuthenticator::class;
|
2013-05-01 16:52:55 +02:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
/**
|
|
|
|
* If no extension is given in the request, resolve to this extension
|
|
|
|
* (and subsequently the {@link self::$default_mimetype}.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2017-11-29 03:20:09 +01:00
|
|
|
private static $default_extension = "xml";
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
/**
|
|
|
|
* If no extension is given, resolve the request to this mimetype.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected static $default_mimetype = "text/xml";
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
/**
|
|
|
|
* @uses authenticate()
|
|
|
|
* @var Member
|
|
|
|
*/
|
|
|
|
protected $member;
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2017-11-29 03:20:09 +01:00
|
|
|
private static $allowed_actions = array(
|
|
|
|
'index',
|
|
|
|
'notFound'
|
2015-11-21 07:21:34 +01:00
|
|
|
);
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
/* This sets up SiteTree the same as when viewing a page through the frontend. Versioned defaults
|
|
|
|
* to Stage, and then when viewing the front-end Versioned::choose_site_stage changes it to Live.
|
|
|
|
* TODO: In 3.2 we should make the default Live, then change to Stage in the admin area (with a nicer API)
|
|
|
|
*/
|
2017-11-29 03:20:09 +01:00
|
|
|
if (class_exists(SiteTree::class)) {
|
|
|
|
singleton(SiteTree::class)->extend('modelascontrollerInit', $this);
|
2015-11-21 07:21:34 +01:00
|
|
|
}
|
|
|
|
parent::init();
|
|
|
|
}
|
2013-07-04 23:58:39 +02:00
|
|
|
|
2017-11-29 03:20:09 +01:00
|
|
|
/**
|
|
|
|
* Backslashes in fully qualified class names (e.g. NameSpaced\ClassName)
|
|
|
|
* kills both requests (i.e. URIs) and XML (invalid character in a tag name)
|
|
|
|
* So we'll replace them with a hyphen (-), as it's also unambiguious
|
|
|
|
* in both cases (invalid in a php class name, and safe in an xml tag name)
|
|
|
|
*
|
|
|
|
* @param string $classname
|
|
|
|
* @return string 'escaped' class name
|
|
|
|
*/
|
|
|
|
protected function sanitiseClassName($className)
|
|
|
|
{
|
|
|
|
return str_replace('\\', '-', $className);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert hyphen escaped class names back into fully qualified
|
|
|
|
* PHP safe variant.
|
|
|
|
*
|
|
|
|
* @param string $classname
|
|
|
|
* @return string syntactically valid classname
|
|
|
|
*/
|
|
|
|
protected function unsanitiseClassName($className)
|
|
|
|
{
|
|
|
|
return str_replace('-', '\\', $className);
|
|
|
|
}
|
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
/**
|
|
|
|
* This handler acts as the switchboard for the controller.
|
|
|
|
* Since no $Action url-param is set, all requests are sent here.
|
|
|
|
*/
|
2017-11-29 03:20:09 +01:00
|
|
|
public function index(HTTPRequest $request)
|
2015-11-21 07:21:34 +01:00
|
|
|
{
|
2017-11-29 03:20:09 +01:00
|
|
|
$className = $this->unsanitiseClassName($request->param('ClassName'));
|
|
|
|
$id = $request->param('ID') ?: null;
|
|
|
|
$relation = $request->param('Relation') ?: null;
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
// Check input formats
|
|
|
|
if (!class_exists($className)) {
|
|
|
|
return $this->notFound();
|
|
|
|
}
|
|
|
|
if ($id && !is_numeric($id)) {
|
|
|
|
return $this->notFound();
|
|
|
|
}
|
2017-11-29 03:20:09 +01:00
|
|
|
if ($relation
|
2015-11-21 07:21:34 +01:00
|
|
|
&& !preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $relation)
|
2017-12-06 04:18:14 +01:00
|
|
|
) {
|
2015-11-21 07:21:34 +01:00
|
|
|
return $this->notFound();
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
// if api access is disabled, don't proceed
|
2017-12-06 04:18:14 +01:00
|
|
|
$apiAccess = Config::inst()->get($className, 'api_access');
|
2015-11-21 07:21:34 +01:00
|
|
|
if (!$apiAccess) {
|
|
|
|
return $this->permissionFailure();
|
|
|
|
}
|
2012-06-04 10:14:02 +02:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
// authenticate through HTTP BasicAuth
|
|
|
|
$this->member = $this->authenticate();
|
2012-06-04 10:14:02 +02:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
// handle different HTTP verbs
|
|
|
|
if ($this->request->isGET() || $this->request->isHEAD()) {
|
|
|
|
return $this->getHandler($className, $id, $relation);
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
if ($this->request->isPOST()) {
|
|
|
|
return $this->postHandler($className, $id, $relation);
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
if ($this->request->isPUT()) {
|
|
|
|
return $this->putHandler($className, $id, $relation);
|
|
|
|
}
|
2012-10-31 00:23:29 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
if ($this->request->isDELETE()) {
|
|
|
|
return $this->deleteHandler($className, $id, $relation);
|
|
|
|
}
|
2012-06-04 10:14:02 +02:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
// if no HTTP verb matches, return error
|
|
|
|
return $this->methodNotAllowed();
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
/**
|
|
|
|
* Handler for object read.
|
2017-11-02 22:20:11 +01:00
|
|
|
*
|
2015-11-21 07:21:34 +01:00
|
|
|
* The data object will be returned in the following format:
|
|
|
|
*
|
|
|
|
* <ClassName>
|
|
|
|
* <FieldName>Value</FieldName>
|
|
|
|
* ...
|
|
|
|
* <HasOneRelName id="ForeignID" href="LinkToForeignRecordInAPI" />
|
|
|
|
* ...
|
|
|
|
* <HasManyRelName>
|
|
|
|
* <ForeignClass id="ForeignID" href="LinkToForeignRecordInAPI" />
|
|
|
|
* <ForeignClass id="ForeignID" href="LinkToForeignRecordInAPI" />
|
|
|
|
* </HasManyRelName>
|
|
|
|
* ...
|
|
|
|
* <ManyManyRelName>
|
|
|
|
* <ForeignClass id="ForeignID" href="LinkToForeignRecordInAPI" />
|
|
|
|
* <ForeignClass id="ForeignID" href="LinkToForeignRecordInAPI" />
|
|
|
|
* </ManyManyRelName>
|
|
|
|
* </ClassName>
|
|
|
|
*
|
|
|
|
* Access is controlled by two variables:
|
2017-11-02 22:20:11 +01:00
|
|
|
*
|
2015-11-21 07:21:34 +01:00
|
|
|
* - static $api_access must be set. This enables the API on a class by class basis
|
|
|
|
* - $obj->canView() must return true. This lets you implement record-level security
|
2017-11-02 22:20:11 +01:00
|
|
|
*
|
2015-11-21 07:21:34 +01:00
|
|
|
* @todo Access checking
|
2017-11-02 22:20:11 +01:00
|
|
|
*
|
2017-12-06 04:18:14 +01:00
|
|
|
* @param string $className
|
2015-11-21 07:21:34 +01:00
|
|
|
* @param Int $id
|
2017-12-06 04:18:14 +01:00
|
|
|
* @param string $relation
|
|
|
|
* @return string The serialized representation of the requested object(s) - usually XML or JSON.
|
2015-11-21 07:21:34 +01:00
|
|
|
*/
|
|
|
|
protected function getHandler($className, $id, $relationName)
|
|
|
|
{
|
|
|
|
$sort = '';
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
if ($this->request->getVar('sort')) {
|
|
|
|
$dir = $this->request->getVar('dir');
|
|
|
|
$sort = array($this->request->getVar('sort') => ($dir ? $dir : 'ASC'));
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
$limit = array(
|
|
|
|
'start' => $this->request->getVar('start'),
|
|
|
|
'limit' => $this->request->getVar('limit')
|
|
|
|
);
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
$params = $this->request->getVars();
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
$responseFormatter = $this->getResponseDataFormatter($className);
|
|
|
|
if (!$responseFormatter) {
|
|
|
|
return $this->unsupportedMediaType();
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
// $obj can be either a DataObject or a SS_List,
|
|
|
|
// depending on the request
|
|
|
|
if ($id) {
|
|
|
|
// Format: /api/v1/<MyClass>/<ID>
|
|
|
|
$obj = $this->getObjectQuery($className, $id, $params)->First();
|
|
|
|
if (!$obj) {
|
|
|
|
return $this->notFound();
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
if (!$obj->canView($this->getMember())) {
|
2015-11-21 07:21:34 +01:00
|
|
|
return $this->permissionFailure();
|
|
|
|
}
|
2012-06-04 10:14:02 +02:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
// Format: /api/v1/<MyClass>/<ID>/<Relation>
|
|
|
|
if ($relationName) {
|
|
|
|
$obj = $this->getObjectRelationQuery($obj, $params, $sort, $limit, $relationName);
|
|
|
|
if (!$obj) {
|
|
|
|
return $this->notFound();
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
// TODO Avoid creating data formatter again for relation class (see above)
|
|
|
|
$responseFormatter = $this->getResponseDataFormatter($obj->dataClass());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Format: /api/v1/<MyClass>
|
|
|
|
$obj = $this->getObjectsQuery($className, $params, $sort, $limit);
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
$this->getResponse()->addHeader('Content-Type', $responseFormatter->getOutputContentType());
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
$rawFields = $this->request->getVar('fields');
|
|
|
|
$fields = $rawFields ? explode(',', $rawFields) : null;
|
2012-06-04 10:14:02 +02:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
if ($obj instanceof SS_List) {
|
2017-11-17 02:09:23 +01:00
|
|
|
$objs = ArrayList::create($obj->toArray());
|
2015-11-21 07:21:34 +01:00
|
|
|
foreach ($objs as $obj) {
|
2017-11-02 22:20:11 +01:00
|
|
|
if (!$obj->canView($this->getMember())) {
|
2015-11-21 07:21:34 +01:00
|
|
|
$objs->remove($obj);
|
|
|
|
}
|
|
|
|
}
|
2017-11-17 02:09:23 +01:00
|
|
|
$responseFormatter->setTotalSize($objs->count());
|
2015-11-21 07:21:34 +01:00
|
|
|
return $responseFormatter->convertDataObjectSet($objs, $fields);
|
2017-11-17 02:09:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$obj) {
|
2015-11-21 07:21:34 +01:00
|
|
|
$responseFormatter->setTotalSize(0);
|
|
|
|
return $responseFormatter->convertDataObjectSet(new ArrayList(), $fields);
|
|
|
|
}
|
2017-11-17 02:09:23 +01:00
|
|
|
|
|
|
|
return $responseFormatter->convertDataObject($obj, $fields);
|
2015-11-21 07:21:34 +01:00
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
/**
|
|
|
|
* Uses the default {@link SearchContext} specified through
|
|
|
|
* {@link DataObject::getDefaultSearchContext()} to augument
|
|
|
|
* an existing query object (mostly a component query from {@link DataObject})
|
2017-11-02 22:20:11 +01:00
|
|
|
* with search clauses.
|
|
|
|
*
|
2015-11-21 07:21:34 +01:00
|
|
|
* @todo Allow specifying of different searchcontext getters on model-by-model basis
|
|
|
|
*
|
|
|
|
* @param string $className
|
|
|
|
* @param array $params
|
|
|
|
* @return SS_List
|
|
|
|
*/
|
2017-11-29 03:20:09 +01:00
|
|
|
protected function getSearchQuery(
|
|
|
|
$className,
|
|
|
|
$params = null,
|
|
|
|
$sort = null,
|
|
|
|
$limit = null,
|
|
|
|
$existingQuery = null
|
2015-11-21 07:21:34 +01:00
|
|
|
) {
|
|
|
|
if (singleton($className)->hasMethod('getRestfulSearchContext')) {
|
|
|
|
$searchContext = singleton($className)->{'getRestfulSearchContext'}();
|
|
|
|
} else {
|
|
|
|
$searchContext = singleton($className)->getDefaultSearchContext();
|
|
|
|
}
|
|
|
|
return $searchContext->getQuery($params, $sort, $limit, $existingQuery);
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
/**
|
|
|
|
* Returns a dataformatter instance based on the request
|
|
|
|
* extension or mimetype. Falls back to {@link self::$default_extension}.
|
2017-11-02 22:20:11 +01:00
|
|
|
*
|
|
|
|
* @param boolean $includeAcceptHeader Determines wether to inspect and prioritize any HTTP Accept headers
|
2017-12-06 04:18:14 +01:00
|
|
|
* @param string Classname of a DataObject
|
2015-11-21 07:21:34 +01:00
|
|
|
* @return DataFormatter
|
|
|
|
*/
|
|
|
|
protected function getDataFormatter($includeAcceptHeader = false, $className = null)
|
|
|
|
{
|
|
|
|
$extension = $this->request->getExtension();
|
|
|
|
$contentTypeWithEncoding = $this->request->getHeader('Content-Type');
|
|
|
|
preg_match('/([^;]*)/', $contentTypeWithEncoding, $contentTypeMatches);
|
|
|
|
$contentType = $contentTypeMatches[0];
|
|
|
|
$accept = $this->request->getHeader('Accept');
|
|
|
|
$mimetypes = $this->request->getAcceptMimetypes();
|
|
|
|
if (!$className) {
|
2017-11-29 03:20:09 +01:00
|
|
|
$className = $this->unsanitiseClassName($this->request->param('ClassName'));
|
2015-11-21 07:21:34 +01:00
|
|
|
}
|
2012-06-04 10:14:02 +02:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
// get formatter
|
|
|
|
if (!empty($extension)) {
|
|
|
|
$formatter = DataFormatter::for_extension($extension);
|
|
|
|
} elseif ($includeAcceptHeader && !empty($accept) && $accept != '*/*') {
|
|
|
|
$formatter = DataFormatter::for_mimetypes($mimetypes);
|
|
|
|
if (!$formatter) {
|
|
|
|
$formatter = DataFormatter::for_extension(self::$default_extension);
|
|
|
|
}
|
|
|
|
} elseif (!empty($contentType)) {
|
|
|
|
$formatter = DataFormatter::for_mimetype($contentType);
|
|
|
|
} else {
|
|
|
|
$formatter = DataFormatter::for_extension(self::$default_extension);
|
|
|
|
}
|
2012-06-04 10:14:02 +02:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
if (!$formatter) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
// set custom fields
|
|
|
|
if ($customAddFields = $this->request->getVar('add_fields')) {
|
|
|
|
$formatter->setCustomAddFields(explode(',', $customAddFields));
|
|
|
|
}
|
|
|
|
if ($customFields = $this->request->getVar('fields')) {
|
|
|
|
$formatter->setCustomFields(explode(',', $customFields));
|
|
|
|
}
|
|
|
|
$formatter->setCustomRelations($this->getAllowedRelations($className));
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2017-12-06 04:18:14 +01:00
|
|
|
$apiAccess = Config::inst()->get($className, 'api_access');
|
2015-11-21 07:21:34 +01:00
|
|
|
if (is_array($apiAccess)) {
|
|
|
|
$formatter->setCustomAddFields(
|
|
|
|
array_intersect((array)$formatter->getCustomAddFields(), (array)$apiAccess['view'])
|
|
|
|
);
|
|
|
|
if ($formatter->getCustomFields()) {
|
|
|
|
$formatter->setCustomFields(
|
|
|
|
array_intersect((array)$formatter->getCustomFields(), (array)$apiAccess['view'])
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$formatter->setCustomFields((array)$apiAccess['view']);
|
|
|
|
}
|
|
|
|
if ($formatter->getCustomRelations()) {
|
|
|
|
$formatter->setCustomRelations(
|
|
|
|
array_intersect((array)$formatter->getCustomRelations(), (array)$apiAccess['view'])
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$formatter->setCustomRelations((array)$apiAccess['view']);
|
|
|
|
}
|
|
|
|
}
|
2012-06-04 10:14:02 +02:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
// set relation depth
|
|
|
|
$relationDepth = $this->request->getVar('relationdepth');
|
|
|
|
if (is_numeric($relationDepth)) {
|
|
|
|
$formatter->relationDepth = (int)$relationDepth;
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
return $formatter;
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
/**
|
2017-12-06 04:18:14 +01:00
|
|
|
* @param string Classname of a DataObject
|
2015-11-21 07:21:34 +01:00
|
|
|
* @return DataFormatter
|
|
|
|
*/
|
|
|
|
protected function getRequestDataFormatter($className = null)
|
|
|
|
{
|
|
|
|
return $this->getDataFormatter(false, $className);
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
/**
|
2017-12-06 04:18:14 +01:00
|
|
|
* @param string Classname of a DataObject
|
2015-11-21 07:21:34 +01:00
|
|
|
* @return DataFormatter
|
|
|
|
*/
|
|
|
|
protected function getResponseDataFormatter($className = null)
|
|
|
|
{
|
|
|
|
return $this->getDataFormatter(true, $className);
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
/**
|
|
|
|
* Handler for object delete
|
|
|
|
*/
|
|
|
|
protected function deleteHandler($className, $id)
|
|
|
|
{
|
|
|
|
$obj = DataObject::get_by_id($className, $id);
|
|
|
|
if (!$obj) {
|
|
|
|
return $this->notFound();
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
if (!$obj->canDelete($this->getMember())) {
|
2015-11-21 07:21:34 +01:00
|
|
|
return $this->permissionFailure();
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
$obj->delete();
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
$this->getResponse()->setStatusCode(204); // No Content
|
|
|
|
return true;
|
|
|
|
}
|
2012-06-04 10:14:02 +02:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
/**
|
|
|
|
* Handler for object write
|
|
|
|
*/
|
|
|
|
protected function putHandler($className, $id)
|
|
|
|
{
|
|
|
|
$obj = DataObject::get_by_id($className, $id);
|
|
|
|
if (!$obj) {
|
|
|
|
return $this->notFound();
|
|
|
|
}
|
2017-11-29 03:20:09 +01:00
|
|
|
|
2017-11-02 22:20:11 +01:00
|
|
|
if (!$obj->canEdit($this->getMember())) {
|
2015-11-21 07:21:34 +01:00
|
|
|
return $this->permissionFailure();
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
$reqFormatter = $this->getRequestDataFormatter($className);
|
|
|
|
if (!$reqFormatter) {
|
|
|
|
return $this->unsupportedMediaType();
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
$responseFormatter = $this->getResponseDataFormatter($className);
|
|
|
|
if (!$responseFormatter) {
|
|
|
|
return $this->unsupportedMediaType();
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2017-11-17 01:55:08 +01:00
|
|
|
/** @var DataObject|string */
|
2015-11-21 07:21:34 +01:00
|
|
|
$obj = $this->updateDataObject($obj, $reqFormatter);
|
2017-11-17 01:55:08 +01:00
|
|
|
if (is_string($obj)) {
|
|
|
|
return $obj;
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
$this->getResponse()->setStatusCode(200); // Success
|
|
|
|
$this->getResponse()->addHeader('Content-Type', $responseFormatter->getOutputContentType());
|
2012-06-04 10:14:02 +02:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
// Append the default extension for the output format to the Location header
|
|
|
|
// or else we'll use the default (XML)
|
|
|
|
$types = $responseFormatter->supportedExtensions();
|
|
|
|
$type = '';
|
|
|
|
if (count($types)) {
|
|
|
|
$type = ".{$types[0]}";
|
|
|
|
}
|
2012-06-04 10:14:02 +02:00
|
|
|
|
2017-11-29 03:20:09 +01:00
|
|
|
$urlSafeClassName = $this->sanitiseClassName(get_class($obj));
|
2017-12-05 22:49:45 +01:00
|
|
|
$apiBase = $this->config()->api_base;
|
|
|
|
$objHref = Director::absoluteURL($apiBase . "$urlSafeClassName/$obj->ID" . $type);
|
2015-11-21 07:21:34 +01:00
|
|
|
$this->getResponse()->addHeader('Location', $objHref);
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
return $responseFormatter->convertDataObject($obj);
|
|
|
|
}
|
2012-06-04 10:14:02 +02:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
/**
|
|
|
|
* Handler for object append / method call.
|
2017-11-02 22:20:11 +01:00
|
|
|
*
|
2015-11-21 07:21:34 +01:00
|
|
|
* @todo Posting to an existing URL (without a relation)
|
|
|
|
* current resolves in creatig a new element,
|
|
|
|
* rather than a "Conflict" message.
|
|
|
|
*/
|
|
|
|
protected function postHandler($className, $id, $relation)
|
|
|
|
{
|
|
|
|
if ($id) {
|
|
|
|
if (!$relation) {
|
|
|
|
$this->response->setStatusCode(409);
|
|
|
|
return 'Conflict';
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
$obj = DataObject::get_by_id($className, $id);
|
|
|
|
if (!$obj) {
|
|
|
|
return $this->notFound();
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
if (!$obj->hasMethod($relation)) {
|
|
|
|
return $this->notFound();
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2017-12-06 04:18:14 +01:00
|
|
|
if (!Config::inst()->get($className, 'allowed_actions') ||
|
|
|
|
!in_array($relation, Config::inst()->get($className, 'allowed_actions'))) {
|
2015-11-21 07:21:34 +01:00
|
|
|
return $this->permissionFailure();
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
$obj->$relation();
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
$this->getResponse()->setStatusCode(204); // No Content
|
|
|
|
return true;
|
2017-11-02 22:20:11 +01:00
|
|
|
}
|
2012-06-04 10:14:02 +02:00
|
|
|
|
2017-11-02 22:20:11 +01:00
|
|
|
if (!singleton($className)->canCreate($this->getMember())) {
|
|
|
|
return $this->permissionFailure();
|
2015-11-21 07:21:34 +01:00
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
$obj = new $className();
|
|
|
|
|
|
|
|
$reqFormatter = $this->getRequestDataFormatter($className);
|
|
|
|
if (!$reqFormatter) {
|
|
|
|
return $this->unsupportedMediaType();
|
|
|
|
}
|
|
|
|
|
|
|
|
$responseFormatter = $this->getResponseDataFormatter($className);
|
|
|
|
|
2017-11-17 01:55:08 +01:00
|
|
|
/** @var DataObject|string $obj */
|
2017-11-02 22:20:11 +01:00
|
|
|
$obj = $this->updateDataObject($obj, $reqFormatter);
|
2017-11-17 01:55:08 +01:00
|
|
|
if (is_string($obj)) {
|
|
|
|
return $obj;
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
|
|
|
$this->getResponse()->setStatusCode(201); // Created
|
|
|
|
$this->getResponse()->addHeader('Content-Type', $responseFormatter->getOutputContentType());
|
|
|
|
|
|
|
|
// Append the default extension for the output format to the Location header
|
|
|
|
// or else we'll use the default (XML)
|
|
|
|
$types = $responseFormatter->supportedExtensions();
|
|
|
|
$type = '';
|
|
|
|
if (count($types)) {
|
|
|
|
$type = ".{$types[0]}";
|
|
|
|
}
|
|
|
|
|
2017-11-29 03:20:09 +01:00
|
|
|
$urlSafeClassName = $this->sanitiseClassName(get_class($obj));
|
2017-12-05 22:49:45 +01:00
|
|
|
$apiBase = $this->config()->api_base;
|
|
|
|
$objHref = Director::absoluteURL($apiBase . "$urlSafeClassName/$obj->ID" . $type);
|
2017-11-02 22:20:11 +01:00
|
|
|
$this->getResponse()->addHeader('Location', $objHref);
|
|
|
|
|
|
|
|
return $responseFormatter->convertDataObject($obj);
|
2015-11-21 07:21:34 +01:00
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
/**
|
|
|
|
* Converts either the given HTTP Body into an array
|
|
|
|
* (based on the DataFormatter instance), or returns
|
|
|
|
* the POST variables.
|
|
|
|
* Automatically filters out certain critical fields
|
|
|
|
* that shouldn't be set by the client (e.g. ID).
|
|
|
|
*
|
|
|
|
* @param DataObject $obj
|
|
|
|
* @param DataFormatter $formatter
|
2017-11-17 01:55:08 +01:00
|
|
|
* @return DataObject|string The passed object, or "No Content" if incomplete input data is provided
|
2015-11-21 07:21:34 +01:00
|
|
|
*/
|
|
|
|
protected function updateDataObject($obj, $formatter)
|
|
|
|
{
|
|
|
|
// if neither an http body nor POST data is present, return error
|
|
|
|
$body = $this->request->getBody();
|
|
|
|
if (!$body && !$this->request->postVars()) {
|
|
|
|
$this->getResponse()->setStatusCode(204); // No Content
|
|
|
|
return 'No Content';
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
if (!empty($body)) {
|
|
|
|
$data = $formatter->convertStringToArray($body);
|
|
|
|
} else {
|
|
|
|
// assume application/x-www-form-urlencoded which is automatically parsed by PHP
|
|
|
|
$data = $this->request->postVars();
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
// @todo Disallow editing of certain keys in database
|
2017-12-06 04:18:14 +01:00
|
|
|
$data = array_diff_key($data, ['ID', 'Created']);
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2017-11-29 03:20:09 +01:00
|
|
|
$className = $this->unsanitiseClassName($this->request->param('ClassName'));
|
|
|
|
$apiAccess = singleton($className)->config()->api_access;
|
2015-11-21 07:21:34 +01:00
|
|
|
if (is_array($apiAccess) && isset($apiAccess['edit'])) {
|
|
|
|
$data = array_intersect_key($data, array_combine($apiAccess['edit'], $apiAccess['edit']));
|
|
|
|
}
|
2012-06-04 10:14:02 +02:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
$obj->update($data);
|
|
|
|
$obj->write();
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
return $obj;
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
/**
|
|
|
|
* Gets a single DataObject by ID,
|
|
|
|
* through a request like /api/v1/<MyClass>/<MyID>
|
2017-11-02 22:20:11 +01:00
|
|
|
*
|
2015-11-21 07:21:34 +01:00
|
|
|
* @param string $className
|
|
|
|
* @param int $id
|
|
|
|
* @param array $params
|
|
|
|
* @return DataList
|
|
|
|
*/
|
|
|
|
protected function getObjectQuery($className, $id, $params)
|
|
|
|
{
|
2017-12-06 04:18:14 +01:00
|
|
|
return DataList::create($className)->byIDs([$id]);
|
2015-11-21 07:21:34 +01:00
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
/**
|
|
|
|
* @param DataObject $obj
|
|
|
|
* @param array $params
|
|
|
|
* @param int|array $sort
|
|
|
|
* @param int|array $limit
|
|
|
|
* @return SQLQuery
|
|
|
|
*/
|
|
|
|
protected function getObjectsQuery($className, $params, $sort, $limit)
|
|
|
|
{
|
|
|
|
return $this->getSearchQuery($className, $params, $sort, $limit);
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
/**
|
|
|
|
* @param DataObject $obj
|
|
|
|
* @param array $params
|
|
|
|
* @param int|array $sort
|
|
|
|
* @param int|array $limit
|
|
|
|
* @param string $relationName
|
|
|
|
* @return SQLQuery|boolean
|
|
|
|
*/
|
|
|
|
protected function getObjectRelationQuery($obj, $params, $sort, $limit, $relationName)
|
|
|
|
{
|
|
|
|
// The relation method will return a DataList, that getSearchQuery subsequently manipulates
|
|
|
|
if ($obj->hasMethod($relationName)) {
|
2017-11-29 03:20:09 +01:00
|
|
|
// $this->HasOneName() will return a dataobject or null, neither
|
|
|
|
// of which helps us get the classname in a consistent fashion.
|
|
|
|
// So we must use a way that is reliable.
|
|
|
|
if ($relationClass = DataObject::getSchema()->hasOneComponent(get_class($obj), $relationName)) {
|
2015-11-21 07:21:34 +01:00
|
|
|
$joinField = $relationName . 'ID';
|
2017-11-29 03:20:09 +01:00
|
|
|
// Again `byID` will return the wrong type for our purposes. So use `byIDs`
|
|
|
|
$list = DataList::create($relationClass)->byIDs([$obj->$joinField]);
|
2015-11-21 07:21:34 +01:00
|
|
|
} else {
|
|
|
|
$list = $obj->$relationName();
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2017-12-06 04:18:14 +01:00
|
|
|
$apiAccess = Config::inst()->get($list->dataClass(), 'api_access');
|
|
|
|
|
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
if (!$apiAccess) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
return $this->getSearchQuery($list->dataClass(), $params, $sort, $limit, $list);
|
|
|
|
}
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
protected function permissionFailure()
|
|
|
|
{
|
|
|
|
// return a 401
|
|
|
|
$this->getResponse()->setStatusCode(401);
|
|
|
|
$this->getResponse()->addHeader('WWW-Authenticate', 'Basic realm="API Access"');
|
|
|
|
$this->getResponse()->addHeader('Content-Type', 'text/plain');
|
|
|
|
return "You don't have access to this item through the API.";
|
|
|
|
}
|
2012-06-04 10:14:02 +02:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
protected function notFound()
|
|
|
|
{
|
|
|
|
// return a 404
|
|
|
|
$this->getResponse()->setStatusCode(404);
|
|
|
|
$this->getResponse()->addHeader('Content-Type', 'text/plain');
|
|
|
|
return "That object wasn't found";
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
protected function methodNotAllowed()
|
|
|
|
{
|
|
|
|
$this->getResponse()->setStatusCode(405);
|
|
|
|
$this->getResponse()->addHeader('Content-Type', 'text/plain');
|
|
|
|
return "Method Not Allowed";
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
protected function unsupportedMediaType()
|
|
|
|
{
|
|
|
|
$this->response->setStatusCode(415); // Unsupported Media Type
|
|
|
|
$this->getResponse()->addHeader('Content-Type', 'text/plain');
|
|
|
|
return "Unsupported Media Type";
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
/**
|
|
|
|
* A function to authenticate a user
|
|
|
|
*
|
|
|
|
* @return Member|false the logged in member
|
|
|
|
*/
|
|
|
|
protected function authenticate()
|
|
|
|
{
|
2017-11-29 03:20:09 +01:00
|
|
|
$authClass = $this->config()->authenticator;
|
|
|
|
$member = $authClass::authenticate();
|
|
|
|
Security::setCurrentUser($member);
|
|
|
|
return $member;
|
2015-11-21 07:21:34 +01:00
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
2015-11-21 07:21:34 +01:00
|
|
|
/**
|
|
|
|
* Return only relations which have $api_access enabled.
|
|
|
|
* @todo Respect field level permissions once they are available in core
|
2017-11-02 22:20:11 +01:00
|
|
|
*
|
2015-11-21 07:21:34 +01:00
|
|
|
* @param string $class
|
|
|
|
* @param Member $member
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getAllowedRelations($class, $member = null)
|
|
|
|
{
|
2017-12-06 04:18:14 +01:00
|
|
|
$allowedRelations = [];
|
2015-11-21 07:21:34 +01:00
|
|
|
$obj = singleton($class);
|
2017-11-29 03:20:09 +01:00
|
|
|
$relations = (array)$obj->hasOne() + (array)$obj->hasMany() + (array)$obj->manyMany();
|
2015-11-21 07:21:34 +01:00
|
|
|
if ($relations) {
|
|
|
|
foreach ($relations as $relName => $relClass) {
|
2017-11-29 03:20:09 +01:00
|
|
|
//remove dot notation from relation names
|
|
|
|
$parts = explode('.', $relClass);
|
|
|
|
$relClass = array_shift($parts);
|
2017-12-06 04:18:14 +01:00
|
|
|
if (Config::inst()->get($relClass, 'api_access')) {
|
2015-11-21 07:21:34 +01:00
|
|
|
$allowedRelations[] = $relName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $allowedRelations;
|
|
|
|
}
|
2017-11-02 22:20:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current Member, if available
|
|
|
|
*
|
|
|
|
* @return Member|null
|
|
|
|
*/
|
|
|
|
protected function getMember()
|
|
|
|
{
|
2017-11-29 03:20:09 +01:00
|
|
|
return Security::getCurrentUser();
|
2015-11-21 07:21:34 +01:00
|
|
|
}
|
2012-06-04 10:14:02 +02:00
|
|
|
}
|