Merge branch 'acl-behavior' into 2.0

Conflicts:
	cake/libs/model/behaviors/acl.php
	cake/tests/cases/libs/model/behaviors/acl.test.php
This commit is contained in:
mark_story 2011-03-19 10:14:10 -04:00
commit f33329ae43
2 changed files with 106 additions and 35 deletions

View file

@ -31,9 +31,8 @@ class AclBehavior extends ModelBehavior {
* Maps ACL type options to ACL models
*
* @var array
* @access protected
*/
private $__typeMaps = array('requester' => 'Aro', 'controlled' => 'Aco');
private $__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
@ -45,14 +44,19 @@ class AclBehavior extends ModelBehavior {
if (is_string($config)) {
$config = array('type' => $config);
}
$this->settings[$model->name] = array_merge(array('type' => 'requester'), (array)$config);
$this->settings[$model->name] = array_merge(array('type' => 'controlled'), (array)$config);
$this->settings[$model->name]['type'] = strtolower($this->settings[$model->name]['type']);
$type = $this->__typeMaps[$this->settings[$model->name]['type']];
$types = $this->__typeMaps[$this->settings[$model->name]['type']];
if (!class_exists('AclNode')) {
require LIBS . 'model' . DS . 'db_acl.php';
}
$model->{$type} = ClassRegistry::init($type);
if (!is_array($types)) {
$types = array($types);
}
foreach ($types as $type) {
$model->{$type} = ClassRegistry::init($type);
}
if (!method_exists($model, 'parentNode')) {
trigger_error(__('Callback parentNode() not defined in %s', $model->alias), E_USER_WARNING);
}
@ -62,11 +66,18 @@ class AclBehavior extends ModelBehavior {
* 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
* @link http://book.cakephp.org/view/1322/node
*/
public function node($model, $ref = null) {
$type = $this->__typeMaps[$this->settings[$model->name]['type']];
public function node($model, $ref = null, $type = null) {
if (empty($type)) {
$type = $this->__typeMaps[$this->settings[$model->name]['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);
}
@ -80,22 +91,27 @@ class AclBehavior extends ModelBehavior {
* @return void
*/
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->name]['type']];
if (!is_array($types)) {
$types = array($types);
}
$data = array(
'parent_id' => isset($parent[0][$type]['id']) ? $parent[0][$type]['id'] : null,
'model' => $model->name,
'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);
}
/**
@ -104,10 +120,15 @@ class AclBehavior extends ModelBehavior {
* @return void
*/
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->name]['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);
}
}
}
}

View file

@ -51,7 +51,7 @@ class AclPerson extends CakeTestModel {
* @var array
* @access public
*/
public $actsAs = array('Acl' => 'requester');
public $actsAs = array('Acl' => 'both');
/**
* belongsTo property
@ -131,7 +131,7 @@ class AclUser extends CakeTestModel {
* @var array
* @access public
*/
public $actsAs = array('Acl');
public $actsAs = array('Acl' => 'requester');
/**
* parentNode
@ -254,6 +254,20 @@ class AclBehaviorTest extends CakeTestCase {
$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
*
@ -286,6 +300,15 @@ class AclBehaviorTest extends CakeTestCase {
);
$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(
@ -301,7 +324,7 @@ class AclBehaviorTest extends CakeTestCase {
$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);
@ -315,17 +338,24 @@ class AclBehaviorTest extends CakeTestCase {
);
$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);
@ -346,6 +376,16 @@ class AclBehaviorTest extends CakeTestCase {
);
$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(
@ -382,7 +422,17 @@ class AclBehaviorTest extends CakeTestCase {
)
);
$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(
'name' => 'Trent',
@ -392,7 +442,7 @@ class AclBehaviorTest extends CakeTestCase {
);
$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);
@ -445,7 +495,7 @@ class AclBehaviorTest extends CakeTestCase {
$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);
}