Skip to content

Commit

Permalink
Implementing Aclbehavior to be set as both requester as controlled, t…
Browse files Browse the repository at this point in the history
…ests updated, fixes #346
  • Loading branch information
ceeram committed Feb 27, 2011
1 parent e05c6cd commit f1f5864
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 41 deletions.
82 changes: 52 additions & 30 deletions cake/libs/model/behaviors/acl.php
Expand Up @@ -35,7 +35,7 @@ class AclBehavior extends ModelBehavior {
* @var array
* @access protected
*/
var $__typeMaps = array('requester' => 'Aro', 'controlled' => 'Aco');
var $__typeMaps = array('requester' => 'Aro', 'controlled' => 'Aco', 'both' => array('Aro', 'Aco'));

/**
* Sets up the configuation for the model, and loads ACL models if they haven't been already
Expand All @@ -48,17 +48,22 @@ function setup(&$model, $config = array()) {
if (is_string($config)) {
$config = array('type' => $config);
}
$this->settings[$model->name] = array_merge(array('type' => 'requester'), (array)$config);
$this->settings[$model->name]['type'] = strtolower($this->settings[$model->name]['type']);
$this->settings[$model->alias] = array_merge(array('type' => 'controlled'), (array)$config);
$this->settings[$model->alias]['type'] = strtolower($this->settings[$model->alias]['type']);

$type = $this->__typeMaps[$this->settings[$model->name]['type']];
$types = $this->__typeMaps[$this->settings[$model->alias]['type']];
if (!class_exists('AclNode')) {
require LIBS . 'model' . DS . 'db_acl.php';
}
if (PHP5) {
$model->{$type} = ClassRegistry::init($type);
} else {
$model->{$type} =& ClassRegistry::init($type);
if (!is_array($types)) {
$types = array($types);
}
foreach($types as $type) {
if (PHP5) {
$model->{$type} = ClassRegistry::init($type);
} else {
$model->{$type} =& ClassRegistry::init($type);
}
}
if (!method_exists($model, 'parentNode')) {
trigger_error(sprintf(__('Callback parentNode() not defined in %s', true), $model->alias), E_USER_WARNING);
Expand All @@ -69,14 +74,21 @@ function setup(&$model, $config = array()) {
* Retrieves the Aro/Aco node for this model
*
* @param mixed $ref
* @param string $type Only needed when Acl is set up as 'both', specify 'Aro' or 'Aco' to get the correct node
* @return array
* @access public
* @link http://book.cakephp.org/view/1322/node
*/
function node(&$model, $ref = null) {
$type = $this->__typeMaps[$this->settings[$model->name]['type']];
function node(&$model, $ref = null, $type = null) {
if (empty($type)) {
$type = $this->__typeMaps[$this->settings[$model->alias]['type']];
if (is_array($type)) {
trigger_error(__('AclBehavior is setup with more then one type, please specify type parameter for node()', true), E_USER_WARNING);
return null;
}
}
if (empty($ref)) {
$ref = array('model' => $model->name, 'foreign_key' => $model->id);
$ref = array('model' => $model->alias, 'foreign_key' => $model->id);
}
return $model->{$type}->node($ref);
}
Expand All @@ -89,22 +101,27 @@ function node(&$model, $ref = null) {
* @access public
*/
function afterSave(&$model, $created) {
$type = $this->__typeMaps[$this->settings[$model->name]['type']];
$parent = $model->parentNode();
if (!empty($parent)) {
$parent = $this->node($model, $parent);
$types = $this->__typeMaps[$this->settings[$model->alias]['type']];
if (!is_array($types)) {
$types = array($types);
}
$data = array(
'parent_id' => isset($parent[0][$type]['id']) ? $parent[0][$type]['id'] : null,
'model' => $model->alias,
'foreign_key' => $model->id
);
if (!$created) {
$node = $this->node($model);
$data['id'] = isset($node[0][$type]['id']) ? $node[0][$type]['id'] : null;
foreach ($types as $type) {
$parent = $model->parentNode();
if (!empty($parent)) {
$parent = $this->node($model, $parent, $type);
}
$data = array(
'parent_id' => isset($parent[0][$type]['id']) ? $parent[0][$type]['id'] : null,
'model' => $model->alias,
'foreign_key' => $model->id
);
if (!$created) {
$node = $this->node($model, null, $type);
$data['id'] = isset($node[0][$type]['id']) ? $node[0][$type]['id'] : null;
}
$model->{$type}->create();
$model->{$type}->save($data);
}
$model->{$type}->create();
$model->{$type}->save($data);
}

/**
Expand All @@ -114,10 +131,15 @@ function afterSave(&$model, $created) {
* @access public
*/
function afterDelete(&$model) {
$type = $this->__typeMaps[$this->settings[$model->name]['type']];
$node = Set::extract($this->node($model), "0.{$type}.id");
if (!empty($node)) {
$model->{$type}->delete($node);
$types = $this->__typeMaps[$this->settings[$model->alias]['type']];
if (!is_array($types)) {
$types = array($types);
}
foreach ($types as $type) {
$node = Set::extract($this->node($model, null, $type), "0.{$type}.id");
if (!empty($node)) {
$model->{$type}->delete($node);
}
}
}
}
}
69 changes: 58 additions & 11 deletions cake/tests/cases/libs/model/behaviors/acl.test.php
Expand Up @@ -52,7 +52,7 @@ class AclPerson extends CakeTestModel {
* @var array
* @access public
*/
var $actsAs = array('Acl' => 'requester');
var $actsAs = array('Acl' => 'both');

/**
* belongsTo property
Expand Down Expand Up @@ -133,7 +133,7 @@ class AclUser extends CakeTestModel {
* @var array
* @access public
*/
var $actsAs = array('Acl');
var $actsAs = array('Acl' => 'requester');

/**
* parentNode
Expand Down Expand Up @@ -261,6 +261,20 @@ function testSetup() {
$this->assertTrue(is_object($Post->Aco));
}

/**
* Test Setup of AclBehavior as both requester and controlled
*
* @return void
* @access public
*/
function testSetupMulti() {
$User =& new AclPerson();
$this->assertTrue(isset($User->Behaviors->Acl->settings['AclPerson']));
$this->assertEqual($User->Behaviors->Acl->settings['AclPerson']['type'], 'both');
$this->assertTrue(is_object($User->Aro));
$this->assertTrue(is_object($User->Aco));
}

/**
* test After Save
*
Expand Down Expand Up @@ -294,6 +308,15 @@ function testAfterSave() {
);
$this->Aro->save($aroData);

$acoData = array(
'Aco' => array(
'model' => 'AclPerson',
'foreign_key' => 2,
'parent_id' => null
)
);
$this->Aco->save($acoData);

$Person =& new AclPerson();
$data = array(
'AclPerson' => array(
Expand All @@ -309,7 +332,7 @@ function testAfterSave() {
$this->assertTrue(is_array($result));
$this->assertEqual($result['Aro']['parent_id'], 5);

$node = $Person->node(array('model' => 'AclPerson', 'foreign_key' => 8));
$node = $Person->node(array('model' => 'AclPerson', 'foreign_key' => 8), 'Aro');
$this->assertEqual(count($node), 2);
$this->assertEqual($node[0]['Aro']['parent_id'], 5);
$this->assertEqual($node[1]['Aro']['parent_id'], null);
Expand All @@ -323,17 +346,24 @@ function testAfterSave() {
);
$this->Aro->create();
$this->Aro->save($aroData);

$acoData = array(
'Aco' => array(
'model' => 'AclPerson',
'foreign_key' => 1,
'parent_id' => null
));
$this->Aco->create();
$this->Aco->save($acoData);
$Person->read(null, 8);
$Person->set('mother_id', 1);
$Person->save();
$result = $this->Aro->find('first', array(
'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
));
$this->assertTrue(is_array($result));
$this->assertEqual($result['Aro']['parent_id'], 7);
$node = $Person->node(array('model' => 'AclPerson', 'foreign_key' => 8));
$this->assertTrue(is_array($result));
$this->assertEqual($result['Aro']['parent_id'], 7);

$node = $Person->node(array('model' => 'AclPerson', 'foreign_key' => 8), 'Aro');
$this->assertEqual(sizeof($node), 2);
$this->assertEqual($node[0]['Aro']['parent_id'], 7);
$this->assertEqual($node[1]['Aro']['parent_id'], null);
Expand All @@ -354,6 +384,15 @@ function testAfterSaveUpdateParentIdNotNull() {
);
$this->Aro->save($aroData);

$acoData = array(
'Aco' => array(
'model' => 'AclPerson',
'foreign_key' => 2,
'parent_id' => null
)
);
$this->Aco->save($acoData);

$Person =& new AclPerson();
$data = array(
'AclPerson' => array(
Expand Down Expand Up @@ -391,6 +430,14 @@ function testAfterDelete() {
)
);
$this->Aro->save($aroData);
$acoData = array(
'Aco' => array(
'model' => 'AclPerson',
'foreign_key' => 2,
'parent_id' => null
)
);
$this->Aco->save($acoData);
$Person =& new AclPerson();
$data = array(
'AclPerson' => array(
Expand All @@ -401,7 +448,7 @@ function testAfterDelete() {
);
$Person->save($data);
$id = $Person->id;
$node = $Person->node();
$node = $Person->node(null, 'Aro');
$this->assertEqual(count($node), 2);
$this->assertEqual($node[0]['Aro']['parent_id'], 5);
$this->assertEqual($node[1]['Aro']['parent_id'], null);
Expand Down Expand Up @@ -455,8 +502,8 @@ function testNode() {
$this->Aro->save($aroData);

$Person->id = 2;
$result = $Person->node();
$result = $Person->node(null, 'Aro');
$this->assertTrue(is_array($result));
$this->assertEqual(count($result), 1);
}
}
}

0 comments on commit f1f5864

Please sign in to comment.