FIX Total items count in output respects canView on records

This commit is contained in:
Robbie Averill 2017-11-17 14:09:23 +13:00
parent 4ba5bf5853
commit be255c2af1
2 changed files with 12 additions and 5 deletions

View File

@ -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);
} }
/** /**

View File

@ -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']);
} }