Optimizing ACL node querying

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4517 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2007-02-14 07:26:42 +00:00
parent b45f8984f8
commit c78b777db7
5 changed files with 30 additions and 27 deletions

View file

@ -71,19 +71,21 @@ class DB_ACL extends AclBase {
} }
$permKeys = $this->_getAcoKeys($Perms->loadInfo()); $permKeys = $this->_getAcoKeys($Perms->loadInfo());
$aroNode = $Aro->node($aro); $aroPath = $Aro->node($aro);
$acoNode = $Aco->node($aco); $acoPath = new Set($Aco->node($aco));
if (empty($aroNode) || empty($acoNode)) { if (empty($aroPath) || empty($acoPath)) {
trigger_error("DB_ACL::check() - Attempted to check permissions on/with a node that does not exist. Node references:\nAro: " . print_r($aro, true) . "\nAco: " . print_r($aco, true), E_USER_WARNING); trigger_error("DB_ACL::check() - Attempted to check permissions on/with a node that does not exist. Node references:\nAro: " . print_r($aro, true) . "\nAco: " . print_r($aco, true), E_USER_WARNING);
return false; return false;
} }
$aroPath = $Aro->getPath($aroNode['id']);
$acoPath = new Set($Aco->getPath($acoNode['id']));
if ($acoPath->get() == null || $acoPath->get() == array()) { if ($acoPath->get() == null || $acoPath->get() == array()) {
return false; return false;
} }
$aroNode = $aroPath[0];
$acoNode = $acoPath->get();
$acoNode = $acoNode[0];
if ($action != '*' && !in_array('_' . $action, $permKeys)) { if ($action != '*' && !in_array('_' . $action, $permKeys)) {
trigger_error(sprintf(__("ACO permissions key %s does not exist in DB_ACL::check()", true), $action), E_USER_NOTICE); trigger_error(sprintf(__("ACO permissions key %s does not exist in DB_ACL::check()", true), $action), E_USER_NOTICE);
return false; return false;

View file

@ -71,7 +71,9 @@ class AclNode extends AppModel {
$start = $path[count($path) - 1]; $start = $path[count($path) - 1];
unset($path[count($path) - 1]); unset($path[count($path) - 1]);
$query = "SELECT {$type}0.* From {$prefix}{$table} AS {$type}0 "; $query = "SELECT {$type}.* From {$prefix}{$table} AS {$type} ";
$query .= "LEFT JOIN {$prefix}{$table} AS {$type}0 ";
$query .= "ON {$type}0.alias = " . $db->value($start) . " ";
foreach ($path as $i => $alias) { foreach ($path as $i => $alias) {
$j = $i - 1; $j = $i - 1;
$k = $i + 1; $k = $i + 1;
@ -79,11 +81,7 @@ class AclNode extends AppModel {
$query .= "ON {$type}{$k}.lft > {$type}{$i}.lft && {$type}{$k}.rght < {$type}{$i}.rght "; $query .= "ON {$type}{$k}.lft > {$type}{$i}.lft && {$type}{$k}.rght < {$type}{$i}.rght ";
$query .= "AND {$type}{$k}.alias = " . $db->value($alias) . " "; $query .= "AND {$type}{$k}.alias = " . $db->value($alias) . " ";
} }
$result = $this->query("{$query} WHERE {$type}0.alias = " . $db->value($start)); $result = $this->query("{$query} WHERE {$type}.lft <= {$type}0.lft AND {$type}.rght >= {$type}0.rght ORDER BY {$type}.lft DESC");
if (!empty($result)) {
$result = $result[0]["{$type}0"];
}
} elseif (is_object($ref) && is_a($ref, 'Model')) { } elseif (is_object($ref) && is_a($ref, 'Model')) {
$ref = array('model' => $ref->name, 'foreign_key' => $ref->id); $ref = array('model' => $ref->name, 'foreign_key' => $ref->id);
} elseif (is_array($ref) && !(isset($ref['model']) && isset($ref['foreign_key']))) { } elseif (is_array($ref) && !(isset($ref['model']) && isset($ref['foreign_key']))) {
@ -111,9 +109,19 @@ class AclNode extends AppModel {
} }
} }
if (is_array($ref)) { if (is_array($ref)) {
$result = $this->find($ref, null, null, -1); foreach ($ref as $key => $val) {
if ($result) { if (strpos($key, $type) !== 0) {
list($result) = array_values($result); unset($ref[$key]);
$ref["{$type}0.{$key}"] = $val;
}
}
$query = "SELECT {$type}.* From {$prefix}{$table} AS {$type} ";
$query .= "LEFT JOIN {$prefix}{$table} AS {$type}0 ";
$query .= "ON {$type}.lft <= {$type}0.lft AND {$type}.rght >= {$type}0.rght ";
$result = $this->query("{$query} " . $db->conditions($ref) ." ORDER BY {$type}.lft DESC");
if (!$result) {
trigger_error("AclNode::node() - Couldn't find {$type} node identified by \"" . print_r($ref, true) . "\"", E_USER_WARNING);
} }
} }
return $result; return $result;

View file

@ -97,15 +97,9 @@ class AclBehavior extends ModelBehavior {
$type = $this->__typeMaps[low($this->settings[$model->name]['type'])]; $type = $this->__typeMaps[low($this->settings[$model->name]['type'])];
$parent = $this->node($model, $model->parentNode()); $parent = $this->node($model, $model->parentNode());
if(isset($parent['id'])) {
$parent = $parent['id'];
} else {
$parent = null;
}
$model->{$type}->create(); $model->{$type}->create();
$model->{$type}->save(array( $model->{$type}->save(array(
'parent_id' => $parent, 'parent_id' => Set::extract($parent, "0.{$type}.id"),
'model' => $model->name, 'model' => $model->name,
'foreign_key' => $model->id 'foreign_key' => $model->id
)); ));
@ -117,9 +111,11 @@ class AclBehavior extends ModelBehavior {
* @return void * @return void
*/ */
function afterDelete(&$model) { function afterDelete(&$model) {
$node = $this->node($model);
$type = $this->__typeMaps[low($this->settings[$model->name]['type'])]; $type = $this->__typeMaps[low($this->settings[$model->name]['type'])];
$model->{$type}->delete($node['id']); $node = Set::extract($this->node($model), "0.{$type}.id");
if (!empty($node)) {
$model->{$type}->delete($node);
}
} }
} }

View file

@ -53,7 +53,6 @@ class TreeBehavior extends ModelBehavior {
}*/ }*/
$this->settings[$model->name] = $settings; $this->settings[$model->name] = $settings;
} }
/** /**
* Sets the parent of the given node * Sets the parent of the given node
* *

View file

@ -1096,7 +1096,6 @@ class Model extends Overloadable {
} }
} }
} }
if (!empty($joined)) { if (!empty($joined)) {
$this->__saveMulti($joined, $this->id); $this->__saveMulti($joined, $this->id);
} }
@ -1158,7 +1157,6 @@ class Model extends Overloadable {
} }
} }
} }
$total = count($joinTable); $total = count($joinTable);
if(is_array($newValue)) { if(is_array($newValue)) {