mirror of
https://github.com/silverstripe/silverstripe-restfulserver
synced 2024-10-22 14:05:58 +02:00
FIX Total items count in output respects canView on records
This commit is contained in:
parent
4ba5bf5853
commit
be255c2af1
@ -229,20 +229,22 @@ class RestfulServer extends Controller
|
|||||||
$fields = $rawFields ? explode(',', $rawFields) : null;
|
$fields = $rawFields ? explode(',', $rawFields) : null;
|
||||||
|
|
||||||
if ($obj instanceof SS_List) {
|
if ($obj instanceof SS_List) {
|
||||||
$responseFormatter->setTotalSize($obj->dataQuery()->query()->unlimitedRowCount());
|
$objs = ArrayList::create($obj->toArray());
|
||||||
$objs = new ArrayList($obj->toArray());
|
|
||||||
foreach ($objs as $obj) {
|
foreach ($objs as $obj) {
|
||||||
if (!$obj->canView($this->getMember())) {
|
if (!$obj->canView($this->getMember())) {
|
||||||
$objs->remove($obj);
|
$objs->remove($obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
$responseFormatter->setTotalSize($objs->count());
|
||||||
return $responseFormatter->convertDataObjectSet($objs, $fields);
|
return $responseFormatter->convertDataObjectSet($objs, $fields);
|
||||||
} elseif (!$obj) {
|
}
|
||||||
|
|
||||||
|
if (!$obj) {
|
||||||
$responseFormatter->setTotalSize(0);
|
$responseFormatter->setTotalSize(0);
|
||||||
return $responseFormatter->convertDataObjectSet(new ArrayList(), $fields);
|
return $responseFormatter->convertDataObjectSet(new ArrayList(), $fields);
|
||||||
} else {
|
|
||||||
return $responseFormatter->convertDataObject($obj, $fields);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $responseFormatter->convertDataObject($obj, $fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -463,6 +463,8 @@ class RestfulServerTest extends SapphireTest
|
|||||||
$response = Director::test($url, null, null, 'GET');
|
$response = Director::test($url, null, null, 'GET');
|
||||||
$this->assertEquals($response->getStatusCode(), 200);
|
$this->assertEquals($response->getStatusCode(), 200);
|
||||||
$this->assertNotContains('Unspeakable', $response->getBody());
|
$this->assertNotContains('Unspeakable', $response->getBody());
|
||||||
|
$responseArray = Convert::json2array($response->getBody());
|
||||||
|
$this->assertSame(0, $responseArray['totalSize']);
|
||||||
|
|
||||||
// With authentication
|
// With authentication
|
||||||
$_SERVER['PHP_AUTH_USER'] = 'editor@test.com';
|
$_SERVER['PHP_AUTH_USER'] = 'editor@test.com';
|
||||||
@ -471,6 +473,9 @@ class RestfulServerTest extends SapphireTest
|
|||||||
$response = Director::test($url, null, null, 'GET');
|
$response = Director::test($url, null, null, 'GET');
|
||||||
$this->assertEquals($response->getStatusCode(), 200);
|
$this->assertEquals($response->getStatusCode(), 200);
|
||||||
$this->assertContains('Unspeakable', $response->getBody());
|
$this->assertContains('Unspeakable', $response->getBody());
|
||||||
|
// Assumption: default formatter is XML
|
||||||
|
$responseArray = Convert::xml2array($response->getBody());
|
||||||
|
$this->assertEquals(1, $responseArray['@attributes']['totalSize']);
|
||||||
unset($_SERVER['PHP_AUTH_USER']);
|
unset($_SERVER['PHP_AUTH_USER']);
|
||||||
unset($_SERVER['PHP_AUTH_PW']);
|
unset($_SERVER['PHP_AUTH_PW']);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user