Skip to content
Snippets Groups Projects
Commit 30a5efbe authored by Patrick Barroca's avatar Patrick Barroca :grin:
Browse files

Capabilities can now be configured as 'index;show;edit...', added filtered fields configuration

parent 996d1753
Branches
No related merge requests found
......@@ -23,16 +23,33 @@ class Restful_Model {
public function index() {
return $this->canIndex()
? call_user_func_array([$this->_storm_model, 'findAllBy'], [[]])
: [];
if (!$this->canIndex())
return [];
$models = call_user_func_array([$this->_storm_model, 'findAllBy'], [[]]);
$response = [];
foreach($models as $model)
$response[] = $this->toJson($model);
return $response;
}
public function show($id) {
return ($this->canShow()
&& $model = call_user_func_array([$this->_storm_model, 'find'], [$id]))
? $model->toArray() : new StdClass;
if (!$this->canShow()
|| !$model = call_user_func_array([$this->_storm_model, 'find'], [$id]))
return new stdclass();
return $this->toJson($model);
}
protected function toJson($model) {
if (!$model)
return new stdClass();
$response = new stdClass();
foreach($this->_configuration->getFields() as $field)
$response->$field = $model->$field;
return $response;
}
......
......@@ -9,14 +9,16 @@ class Restful_Model_Configuration {
/** @var string */
protected $_model;
/** @var array capabilities */
protected $_caps = ['index' => false, 'show' => false, 'create' => false,
'update' => false, 'delete' => false];
protected $_fields = ['id'];
public static function newFor($model) {
if (!isset(self::$_loaded_instances[$model])) {
$caps = self::$_config->models->get($model, null);
$caps = ($caps) ? $caps->toArray() : [];
self::$_loaded_instances[$model] = new self($model, $caps);
self::$_loaded_instances[$model] = new self(
$model,
self::$_config->models->get($model, null));
}
return self::$_loaded_instances[$model];
}
......@@ -39,9 +41,25 @@ class Restful_Model_Configuration {
}
public function __construct($model, $caps=[]) {
public function __construct($model, $conf) {
$this->_model = $model;
$this->_caps = array_merge($this->_caps, $caps);
if (!$conf)
return;
$caps = explode(';', $conf->get('caps', ''));
foreach($caps as $cap)
if (isset($this->_caps[$cap]))
$this->_caps[$cap] = true;
$fields = explode(';', $conf->get('fields', ''));
foreach($fields as $field)
if ($field && !in_array($field, $this->_fields))
$this->_fields[] = $field;
}
public function getFields() {
return $this->_fields;
}
......
......@@ -11,6 +11,29 @@ abstract class RestfulControllerTestCase extends Zend_Test_PHPUnit_ControllerTes
}
public function dispatch($url = null, $throw_exceptions = false) {
// redirector should not exit
$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
$redirector->setExit(false);
// json helper should not exit
$json = Zend_Controller_Action_HelperBroker::getStaticHelper('json');
$json->suppressExit = true;
$request = $this->getRequest();
if (null !== $url)
$request->setRequestUri($url);
$request->setPathInfo(null);
$this->frontController
->setRequest($request)
->setResponse($this->getResponse())
->throwExceptions($throw_exceptions)
->returnResponse(false);
$this->frontController->dispatch();
}
public function tearDown() {
Storm_Model_Abstract::unsetLoaders();
Restful_Model_Configuration::reset();
......@@ -53,7 +76,7 @@ class IndexControllerInCatalogIndexTest extends RestfulControllerTestCase {
$this->fixture('Model_Testing', ['id' => 1]);
$this->fixture('Model_Testing', ['id' => 2]);
Restful_Model_Configuration::setConfig(new Zend_Config(['models' => ['Model_Testing' => ['index' => 1]]]));
Restful_Model_Configuration::setConfig(new Zend_Config(['models' => ['Model_Testing' => ['caps' => 'index']]]));
$this->dispatch('/Model_Testing', true);
}
......@@ -99,7 +122,7 @@ class IndexControllerInCatalogShowTest extends RestfulControllerTestCase {
$this->fixture('Model_Testing', ['id' => 2]);
Restful_Model_Configuration::setConfig(
new Zend_Config(['models' => ['Model_Testing' => ['show' => 1]]]));
new Zend_Config(['models' => ['Model_Testing' => ['caps' => 'show']]]));
$this->dispatch('/Model_Testing/2', true);
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment