Skip to content

Commit

Permalink
Fixed check on partial and search allowed fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmoore4 committed Jul 20, 2013
1 parent 5c4e135 commit 3663cad
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 10 additions & 0 deletions controllers/ExampleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@

class ExampleController extends RESTController{

/**
* Sets which fields may be searched against, and which fields are allowed to be returned in
* partial responses.
* @var array
*/
protected $allowedFields = array(
'search' => array('name', 'prince_name'),
'partials' => array('name', 'location', 'prince_name', 'popular')
);

private $exampleRecords = array(
array('id' => 1, 'name' => 'Ariel', 'location' => 'Under The Sea', 'prince_name' => 'Eric', 'popular' => 'false'),
array('id' => 2, 'name' => 'Snow White', 'location' => 'Forest', 'prince_name' => 'The Prince', 'popular' => 'true'),
Expand Down
4 changes: 2 additions & 2 deletions controllers/RESTController.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ protected function parseRequest($allowedFields){
$this->searchFields = $this->parseSearchParameters($searchParams);

// This handly snippet determines if searchFields is a strict subset of allowedFields['search']
if(!array_unique($allowedFields['search'] + $this->searchFields) === $allowedFields['search']){
if(array_diff(array_keys($this->searchFields), $this->allowedFields['search'])){
throw new HTTPException(
"The fields you specified cannot be searched.",
401,
Expand All @@ -165,7 +165,7 @@ protected function parseRequest($allowedFields){
$this->partialFields = $this->parsePartialFields($fields);

// Determines if fields is a strict subset of allowed fields
if(!array_unique($allowedFields['partials'] + $this->partialFields) === $allowedFields['partials']){
if(array_diff($this->partialFields, $this->allowedFields['partials'])){
throw new HTTPException(
"The fields you asked for cannot be returned.",
401,
Expand Down

0 comments on commit 3663cad

Please sign in to comment.