diff --git a/cake/libs/model/behaviors/acl.php b/cake/libs/model/behaviors/acl.php index fcb4790b9..47c2fdd44 100644 --- a/cake/libs/model/behaviors/acl.php +++ b/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); } /** diff --git a/cake/tests/cases/libs/model/behaviors/acl.test.php b/cake/tests/cases/libs/model/behaviors/acl.test.php index 0d12ba514..438d6f125 100644 --- a/cake/tests/cases/libs/model/behaviors/acl.test.php +++ b/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); } /**