mirror of
https://github.com/silverstripe/silverstripe-restfulserver
synced 2024-10-22 14:05:58 +02:00
Line length fixes (no logic changes)
This commit is contained in:
parent
86ac971948
commit
746ff0f823
@ -6,15 +6,20 @@
|
|||||||
*
|
*
|
||||||
* @todo Finish RestfulServer_Item and RestfulServer_List implementation and re-enable $url_handlers
|
* @todo Finish RestfulServer_Item and RestfulServer_List implementation and re-enable $url_handlers
|
||||||
* @todo Implement PUT/POST/DELETE for relations
|
* @todo Implement PUT/POST/DELETE for relations
|
||||||
* @todo Access-Control for relations (you might be allowed to view Members and Groups, but not their relation with each other)
|
* @todo Access-Control for relations (you might be allowed to view Members and Groups,
|
||||||
|
* but not their relation with each other)
|
||||||
* @todo Make SearchContext specification customizeable for each class
|
* @todo Make SearchContext specification customizeable for each class
|
||||||
* @todo Allow for range-searches (e.g. on Created column)
|
* @todo Allow for range-searches (e.g. on Created column)
|
||||||
* @todo Allow other authentication methods (currently only HTTP BasicAuth)
|
* @todo Allow other authentication methods (currently only HTTP BasicAuth)
|
||||||
* @todo Filter relation listings by $api_access and canView() permissions
|
* @todo Filter relation listings by $api_access and canView() permissions
|
||||||
* @todo Exclude relations when "fields" are specified through URL (they should be explicitly requested in this case)
|
* @todo Exclude relations when "fields" are specified through URL (they should be explicitly
|
||||||
* @todo Custom filters per DataObject subclass, e.g. to disallow showing unpublished pages in SiteTree/Versioned/Hierarchy
|
* requested in this case)
|
||||||
* @todo URL parameter namespacing for search-fields, limit, fields, add_fields (might all be valid dataobject properties)
|
* @todo Custom filters per DataObject subclass, e.g. to disallow showing unpublished pages in
|
||||||
* e.g. you wouldn't be able to search for a "limit" property on your subclass as its overlayed with the search logic
|
* SiteTree/Versioned/Hierarchy
|
||||||
|
* @todo URL parameter namespacing for search-fields, limit, fields, add_fields
|
||||||
|
* (might all be valid dataobject properties)
|
||||||
|
* e.g. you wouldn't be able to search for a "limit" property on your subclass as
|
||||||
|
* its overlayed with the search logic
|
||||||
* @todo i18n integration (e.g. Page/1.xml?lang=de_DE)
|
* @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 Access to extendable methods/relations like SiteTree/1/Versions or SiteTree/1/Version/22
|
||||||
* @todo Respect $api_access array notation in search contexts
|
* @todo Respect $api_access array notation in search contexts
|
||||||
@ -79,7 +84,12 @@ class RestfulServer extends Controller {
|
|||||||
// Check input formats
|
// Check input formats
|
||||||
if(!class_exists($className)) return $this->notFound();
|
if(!class_exists($className)) return $this->notFound();
|
||||||
if($id && !is_numeric($id)) return $this->notFound();
|
if($id && !is_numeric($id)) return $this->notFound();
|
||||||
if($relation && !preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $relation)) return $this->notFound();
|
if(
|
||||||
|
$relation
|
||||||
|
&& !preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $relation)
|
||||||
|
) {
|
||||||
|
return $this->notFound();
|
||||||
|
}
|
||||||
|
|
||||||
// if api access is disabled, don't proceed
|
// if api access is disabled, don't proceed
|
||||||
$apiAccess = singleton($className)->stat('api_access');
|
$apiAccess = singleton($className)->stat('api_access');
|
||||||
@ -89,10 +99,21 @@ class RestfulServer extends Controller {
|
|||||||
$this->member = $this->authenticate();
|
$this->member = $this->authenticate();
|
||||||
|
|
||||||
// handle different HTTP verbs
|
// handle different HTTP verbs
|
||||||
if($this->request->isGET() || $this->request->isHEAD()) return $this->getHandler($className, $id, $relation);
|
if($this->request->isGET() || $this->request->isHEAD()) {
|
||||||
if($this->request->isPOST()) return $this->postHandler($className, $id, $relation);
|
return $this->getHandler($className, $id, $relation);
|
||||||
if($this->request->isPUT()) return $this->putHandler($className, $id, $relation);
|
}
|
||||||
if($this->request->isDELETE()) return $this->deleteHandler($className, $id, $relation);
|
|
||||||
|
if($this->request->isPOST()) {
|
||||||
|
return $this->postHandler($className, $id, $relation);
|
||||||
|
}
|
||||||
|
|
||||||
|
if($this->request->isPUT()) {
|
||||||
|
return $this->putHandler($className, $id, $relation);
|
||||||
|
}
|
||||||
|
|
||||||
|
if($this->request->isDELETE()) {
|
||||||
|
return $this->deleteHandler($className, $id, $relation);
|
||||||
|
}
|
||||||
|
|
||||||
// if no HTTP verb matches, return error
|
// if no HTTP verb matches, return error
|
||||||
return $this->methodNotAllowed();
|
return $this->methodNotAllowed();
|
||||||
@ -201,7 +222,9 @@ class RestfulServer extends Controller {
|
|||||||
* @param array $params
|
* @param array $params
|
||||||
* @return SS_List
|
* @return SS_List
|
||||||
*/
|
*/
|
||||||
protected function getSearchQuery($className, $params = null, $sort = null, $limit = null, $existingQuery = null) {
|
protected function getSearchQuery($className, $params = null, $sort = null,
|
||||||
|
$limit = null, $existingQuery = null
|
||||||
|
) {
|
||||||
if(singleton($className)->hasMethod('getRestfulSearchContext')) {
|
if(singleton($className)->hasMethod('getRestfulSearchContext')) {
|
||||||
$searchContext = singleton($className)->{'getRestfulSearchContext'}();
|
$searchContext = singleton($className)->{'getRestfulSearchContext'}();
|
||||||
} else {
|
} else {
|
||||||
@ -242,20 +265,30 @@ class RestfulServer extends Controller {
|
|||||||
if(!$formatter) return false;
|
if(!$formatter) return false;
|
||||||
|
|
||||||
// set custom fields
|
// set custom fields
|
||||||
if($customAddFields = $this->request->getVar('add_fields')) $formatter->setCustomAddFields(explode(',',$customAddFields));
|
if($customAddFields = $this->request->getVar('add_fields')) {
|
||||||
if($customFields = $this->request->getVar('fields')) $formatter->setCustomFields(explode(',',$customFields));
|
$formatter->setCustomAddFields(explode(',',$customAddFields));
|
||||||
|
}
|
||||||
|
if($customFields = $this->request->getVar('fields')) {
|
||||||
|
$formatter->setCustomFields(explode(',',$customFields));
|
||||||
|
}
|
||||||
$formatter->setCustomRelations($this->getAllowedRelations($className));
|
$formatter->setCustomRelations($this->getAllowedRelations($className));
|
||||||
|
|
||||||
$apiAccess = singleton($className)->stat('api_access');
|
$apiAccess = singleton($className)->stat('api_access');
|
||||||
if(is_array($apiAccess)) {
|
if(is_array($apiAccess)) {
|
||||||
$formatter->setCustomAddFields(array_intersect((array)$formatter->getCustomAddFields(), (array)$apiAccess['view']));
|
$formatter->setCustomAddFields(
|
||||||
|
array_intersect((array)$formatter->getCustomAddFields(), (array)$apiAccess['view'])
|
||||||
|
);
|
||||||
if($formatter->getCustomFields()) {
|
if($formatter->getCustomFields()) {
|
||||||
$formatter->setCustomFields(array_intersect((array)$formatter->getCustomFields(), (array)$apiAccess['view']));
|
$formatter->setCustomFields(
|
||||||
|
array_intersect((array)$formatter->getCustomFields(), (array)$apiAccess['view'])
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
$formatter->setCustomFields((array)$apiAccess['view']);
|
$formatter->setCustomFields((array)$apiAccess['view']);
|
||||||
}
|
}
|
||||||
if($formatter->getCustomRelations()) {
|
if($formatter->getCustomRelations()) {
|
||||||
$formatter->setCustomRelations(array_intersect((array)$formatter->getCustomRelations(), (array)$apiAccess['view']));
|
$formatter->setCustomRelations(
|
||||||
|
array_intersect((array)$formatter->getCustomRelations(), (array)$apiAccess['view'])
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
$formatter->setCustomRelations((array)$apiAccess['view']);
|
$formatter->setCustomRelations((array)$apiAccess['view']);
|
||||||
}
|
}
|
||||||
|
@ -176,7 +176,10 @@ class RestfulServerTest extends SapphireTest {
|
|||||||
$this->assertNotEquals($responseArr['ID'], $comment1->ID);
|
$this->assertNotEquals($responseArr['ID'], $comment1->ID);
|
||||||
$this->assertEquals($responseArr['Comment'], 'created');
|
$this->assertEquals($responseArr['Comment'], 'created');
|
||||||
$this->assertEquals($responseArr['Name'], 'New Comment');
|
$this->assertEquals($responseArr['Name'], 'New Comment');
|
||||||
$this->assertEquals($response->getHeader('Location'), Controller::join_links(Director::absoluteBaseURL(), $url, $responseArr['ID']));
|
$this->assertEquals(
|
||||||
|
$response->getHeader('Location'),
|
||||||
|
Controller::join_links(Director::absoluteBaseURL(), $url, $responseArr['ID'])
|
||||||
|
);
|
||||||
|
|
||||||
unset($_SERVER['PHP_AUTH_USER']);
|
unset($_SERVER['PHP_AUTH_USER']);
|
||||||
unset($_SERVER['PHP_AUTH_PW']);
|
unset($_SERVER['PHP_AUTH_PW']);
|
||||||
@ -202,7 +205,10 @@ class RestfulServerTest extends SapphireTest {
|
|||||||
$body = '{"Comment":"updated"}';
|
$body = '{"Comment":"updated"}';
|
||||||
$response = Director::test($url, null, null, 'PUT', $body);
|
$response = Director::test($url, null, null, 'PUT', $body);
|
||||||
$this->assertEquals($response->getStatusCode(), 200); // Updated
|
$this->assertEquals($response->getStatusCode(), 200); // Updated
|
||||||
$this->assertEquals($response->getHeader('Location'), Controller::join_links(Director::absoluteBaseURL(), $url));
|
$this->assertEquals(
|
||||||
|
$response->getHeader('Location'),
|
||||||
|
Controller::join_links(Director::absoluteBaseURL(), $url)
|
||||||
|
);
|
||||||
$obj = Convert::json2obj($response->getBody());
|
$obj = Convert::json2obj($response->getBody());
|
||||||
$this->assertEquals($obj->ID, $comment1->ID);
|
$this->assertEquals($obj->ID, $comment1->ID);
|
||||||
$this->assertEquals($obj->Comment, 'updated');
|
$this->assertEquals($obj->Comment, 'updated');
|
||||||
@ -231,7 +237,10 @@ class RestfulServerTest extends SapphireTest {
|
|||||||
$body = '<RestfulServerTest_Comment><Comment>updated</Comment></RestfulServerTest_Comment>';
|
$body = '<RestfulServerTest_Comment><Comment>updated</Comment></RestfulServerTest_Comment>';
|
||||||
$response = Director::test($url, null, null, 'PUT', $body);
|
$response = Director::test($url, null, null, 'PUT', $body);
|
||||||
$this->assertEquals($response->getStatusCode(), 200); // Updated
|
$this->assertEquals($response->getStatusCode(), 200); // Updated
|
||||||
$this->assertEquals($response->getHeader('Location'), Controller::join_links(Director::absoluteBaseURL(), $url));
|
$this->assertEquals(
|
||||||
|
$response->getHeader('Location'),
|
||||||
|
Controller::join_links(Director::absoluteBaseURL(), $url)
|
||||||
|
);
|
||||||
$obj = Convert::xml2array($response->getBody());
|
$obj = Convert::xml2array($response->getBody());
|
||||||
$this->assertEquals($obj['ID'], $comment1->ID);
|
$this->assertEquals($obj['ID'], $comment1->ID);
|
||||||
$this->assertEquals($obj['Comment'], 'updated');
|
$this->assertEquals($obj['Comment'], 'updated');
|
||||||
@ -401,7 +410,8 @@ class RestfulServerTest extends SapphireTest {
|
|||||||
);
|
);
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
$formatter->convertDataObjectSet($set, array("FirstName", "Email")),
|
$formatter->convertDataObjectSet($set, array("FirstName", "Email")),
|
||||||
'{"totalSize":null,"items":[{"FirstName":"Editor","Email":"editor@test.com"},{"FirstName":"User","Email":"user@test.com"}]}',
|
'{"totalSize":null,"items":[{"FirstName":"Editor","Email":"editor@test.com"},' .
|
||||||
|
'{"FirstName":"User","Email":"user@test.com"}]}',
|
||||||
"Correct JSON formatting on a dataobjectset with field filter");
|
"Correct JSON formatting on a dataobjectset with field filter");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user