Permalink
Browse files

Adding patch from 'Ceeram' and 'TehTreag' to implement automatic upda…

…ting of Aro/Aco nodes created AclBehavior. Test cases added. Fixes #33 and #4261
  • Loading branch information...
1 parent b95d9e1 commit d5376e64aadab9597fada0fb2d0a55e2dd315ff2 @markstory markstory committed Sep 19, 2009
Showing with 37 additions and 15 deletions.
  1. +15 −15 cake/libs/model/behaviors/acl.php
  2. +22 −0 cake/tests/cases/libs/model/behaviors/acl.test.php
View
30 cake/libs/model/behaviors/acl.php
@@ -90,22 +90,22 @@ function node(&$model, $ref = null) {
* @access public
*/
function afterSave(&$model, $created) {
- if ($created) {
- $type = $this->__typeMaps[strtolower($this->settings[$model->name]['type'])];
- $parent = $model->parentNode();
- if (!empty($parent)) {
- $parent = $this->node($model, $parent);
- } else {
- $parent = null;
- }
-
- $model->{$type}->create();
- $model->{$type}->save(array(
- 'parent_id' => Set::extract($parent, "0.{$type}.id"),
- 'model' => $model->name,
- 'foreign_key' => $model->id
- ));
+ $type = $this->__typeMaps[strtolower($this->settings[$model->alias]['type'])];
+ $parent = $model->parentNode();
+ if (!empty($parent)) {
+ $parent = $this->node($model, $parent);
+ }
+ $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;
}
+ $model->{$type}->create();
+ $model->{$type}->save($data);
}
/**
View
22 cake/tests/cases/libs/model/behaviors/acl.test.php
@@ -314,6 +314,28 @@ function testAfterSave() {
$this->assertEqual(sizeof($node), 2);
$this->assertEqual($node[0]['Aro']['parent_id'], 5);
$this->assertEqual($node[1]['Aro']['parent_id'], null);
+
+ $aroData = array(
+ 'Aro' => array(
+ 'model' => 'AclPerson',
+ 'foreign_key' => 1,
+ 'parent_id' => null
+ )
+ );
+ $this->Aro->create();
+ $this->Aro->save($aroData);
+
+ $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->assertEqual(sizeof($node), 2);
+ $this->assertEqual($node[0]['Aro']['parent_id'], 7);
+ $this->assertEqual($node[1]['Aro']['parent_id'], null);
}
/**

0 comments on commit d5376e6

Please sign in to comment.