Refactoring AclBehavior

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4501 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2007-02-10 20:28:22 +00:00
parent 3bff0fd2d1
commit c87b48e034
3 changed files with 17 additions and 35 deletions

View file

@ -65,7 +65,7 @@ class AclNode extends AppModel {
$prefix = $this->tablePrefix; $prefix = $this->tablePrefix;
if (empty($ref)) { if (empty($ref)) {
$ref = array('model' => $this->name, 'foreign_key' => $this->id); return null;
} elseif (is_string($ref)) { } elseif (is_string($ref)) {
$path = explode('/', $ref); $path = explode('/', $ref);
$start = $path[count($path) - 1]; $start = $path[count($path) - 1];

View file

@ -39,8 +39,19 @@ if (!defined('ACL_DATABASE')) {
*/ */
class AclBehavior extends ModelBehavior { class AclBehavior extends ModelBehavior {
/**
* Maps ACL type options to ACL models
*
* @var array
* @access protected
*/
var $__typeMaps = array('requester' => 'Aro', 'controlled' => 'Aco'); var $__typeMaps = array('requester' => 'Aro', 'controlled' => 'Aco');
/**
* Sets up the configuation for the model, and loads ACL models if they haven't been already
*
* @param mixed $config
* @return void
*/
function setup(&$model, $config = array()) { function setup(&$model, $config = array()) {
if (is_string($config)) { if (is_string($config)) {
$config = array('type' => $config); $config = array('type' => $config);
@ -69,40 +80,11 @@ class AclBehavior extends ModelBehavior {
* @return array * @return array
*/ */
function node(&$model, $ref = null) { function node(&$model, $ref = null) {
$db =& ConnectionManager::getDataSource($model->useDbConfig);
$type = $this->__typeMaps[low($this->settings[$model->name]['type'])]; $type = $this->__typeMaps[low($this->settings[$model->name]['type'])];
$table = low($type) . 's';
$prefix = $model->tablePrefix;
$axo =& $model->{$type};
if (empty($ref)) { if (empty($ref)) {
$ref = array('model' => $model->name, 'foreign_key' => $model->id); $ref = array('model' => $model->name, 'foreign_key' => $model->id);
} elseif (is_string($ref)) {
$path = explode('/', $ref);
$start = $path[count($path) - 1];
unset($path[count($path) - 1]);
$query = "SELECT {$type}0.* From {$prefix}{$table} AS {$type}0 ";
foreach ($path as $i => $alias) {
$j = $i - 1;
$k = $i + 1;
$query .= "LEFT JOIN {$prefix}{$table} AS {$type}{$k} ";
$query .= "ON {$type}{$k}.lft > {$type}{$i}.lft && {$type}{$k}.rght < {$type}{$i}.rght ";
$query .= "AND {$type}{$k}.alias = " . $db->value($alias) . " ";
}
$result = $axo->query("{$query} WHERE {$type}0.alias = " . $db->value($start));
if (!empty($result)) {
$result = $result[0]["{$type}0"];
}
} elseif (is_object($ref) && is_a($ref, 'Model')) {
$ref = array('model' => $ref->name, 'foreign_key' => $ref->id);
} }
return $model->{$type}->node($ref);
if (is_array($ref) && !empty($ref)) {
list($result) = array_values($axo->find($ref, null, null, -1));
}
return $result;
} }
/** /**
* Creates a new ARO/ACO node bound to this record * Creates a new ARO/ACO node bound to this record

View file

@ -1204,6 +1204,8 @@ class Model extends Overloadable {
* @return boolean True on success * @return boolean True on success
*/ */
function del($id = null, $cascade = true) { function del($id = null, $cascade = true) {
pr($this->name);
pr($this->trace());
if (!empty($id)) { if (!empty($id)) {
$this->id = $id; $this->id = $id;
} }
@ -1226,9 +1228,7 @@ class Model extends Overloadable {
$this->_deleteDependent($this->id, $cascade); $this->_deleteDependent($this->id, $cascade);
if (!empty($this->behaviors)) { if (!empty($this->behaviors)) {
for ($i = 0; $i < $ct; $i++) { for ($i = 0; $i < $ct; $i++) {
if ($this->behaviors[$behaviors[$i]]->afterDelete($this) === false) { $this->behaviors[$behaviors[$i]]->afterDelete($this);
return false;
}
} }
} }
$this->afterDelete(); $this->afterDelete();