Browse Source

Adding patch from 'Ceeram' and 'TehTreag' to implement automatic updating of Aro/Aco nodes created AclBehavior. Test cases added. Fixes #33 and #4261

mark_story 16 years ago
parent
commit
d5376e64aa
2 changed files 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

+ 15 - 15
cake/libs/model/behaviors/acl.php

@@ -90,22 +90,22 @@ class AclBehavior extends ModelBehavior {
  * @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);
 	}
 
 /**

+ 22 - 0
cake/tests/cases/libs/model/behaviors/acl.test.php

@@ -314,6 +314,28 @@ class AclBehaviorTestCase extends CakeTestCase {
 		$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);
 	}
 
 /**