From c78b777db78e899f157d6b03e166c2f9a42b6366 Mon Sep 17 00:00:00 2001 From: nate Date: Wed, 14 Feb 2007 07:26:42 +0000 Subject: [PATCH] Optimizing ACL node querying git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4517 3807eeeb-6ff5-0310-8944-8be069107fe0 --- .../controller/components/dbacl/db_acl.php | 14 +++++----- .../components/dbacl/models/aclnode.php | 26 ++++++++++++------- cake/libs/model/behaviors/acl.php | 14 ++++------ cake/libs/model/behaviors/tree.php | 1 - cake/libs/model/model.php | 2 -- 5 files changed, 30 insertions(+), 27 deletions(-) diff --git a/cake/libs/controller/components/dbacl/db_acl.php b/cake/libs/controller/components/dbacl/db_acl.php index 81ada15d9..86e904a1e 100644 --- a/cake/libs/controller/components/dbacl/db_acl.php +++ b/cake/libs/controller/components/dbacl/db_acl.php @@ -71,19 +71,21 @@ class DB_ACL extends AclBase { } $permKeys = $this->_getAcoKeys($Perms->loadInfo()); - $aroNode = $Aro->node($aro); - $acoNode = $Aco->node($aco); + $aroPath = $Aro->node($aro); + $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); return false; } - $aroPath = $Aro->getPath($aroNode['id']); - $acoPath = new Set($Aco->getPath($acoNode['id'])); - if ($acoPath->get() == null || $acoPath->get() == array()) { return false; } + + $aroNode = $aroPath[0]; + $acoNode = $acoPath->get(); + $acoNode = $acoNode[0]; + 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); return false; diff --git a/cake/libs/controller/components/dbacl/models/aclnode.php b/cake/libs/controller/components/dbacl/models/aclnode.php index bbfb271d7..634551e6d 100644 --- a/cake/libs/controller/components/dbacl/models/aclnode.php +++ b/cake/libs/controller/components/dbacl/models/aclnode.php @@ -71,7 +71,9 @@ class AclNode extends AppModel { $start = $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) { $j = $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 .= "AND {$type}{$k}.alias = " . $db->value($alias) . " "; } - $result = $this->query("{$query} WHERE {$type}0.alias = " . $db->value($start)); - - if (!empty($result)) { - $result = $result[0]["{$type}0"]; - } + $result = $this->query("{$query} WHERE {$type}.lft <= {$type}0.lft AND {$type}.rght >= {$type}0.rght ORDER BY {$type}.lft DESC"); } elseif (is_object($ref) && is_a($ref, 'Model')) { $ref = array('model' => $ref->name, 'foreign_key' => $ref->id); } elseif (is_array($ref) && !(isset($ref['model']) && isset($ref['foreign_key']))) { @@ -111,9 +109,19 @@ class AclNode extends AppModel { } } if (is_array($ref)) { - $result = $this->find($ref, null, null, -1); - if ($result) { - list($result) = array_values($result); + foreach ($ref as $key => $val) { + if (strpos($key, $type) !== 0) { + 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; diff --git a/cake/libs/model/behaviors/acl.php b/cake/libs/model/behaviors/acl.php index 685d5d7f8..d686d8afc 100644 --- a/cake/libs/model/behaviors/acl.php +++ b/cake/libs/model/behaviors/acl.php @@ -97,15 +97,9 @@ class AclBehavior extends ModelBehavior { $type = $this->__typeMaps[low($this->settings[$model->name]['type'])]; $parent = $this->node($model, $model->parentNode()); - if(isset($parent['id'])) { - $parent = $parent['id']; - } else { - $parent = null; - } - $model->{$type}->create(); $model->{$type}->save(array( - 'parent_id' => $parent, + 'parent_id' => Set::extract($parent, "0.{$type}.id"), 'model' => $model->name, 'foreign_key' => $model->id )); @@ -117,9 +111,11 @@ class AclBehavior extends ModelBehavior { * @return void */ function afterDelete(&$model) { - $node = $this->node($model); $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); + } } } diff --git a/cake/libs/model/behaviors/tree.php b/cake/libs/model/behaviors/tree.php index f91bdd536..379dd6324 100644 --- a/cake/libs/model/behaviors/tree.php +++ b/cake/libs/model/behaviors/tree.php @@ -53,7 +53,6 @@ class TreeBehavior extends ModelBehavior { }*/ $this->settings[$model->name] = $settings; } - /** * Sets the parent of the given node * diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index 89437b06e..b967c1d45 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -1096,7 +1096,6 @@ class Model extends Overloadable { } } } - if (!empty($joined)) { $this->__saveMulti($joined, $this->id); } @@ -1158,7 +1157,6 @@ class Model extends Overloadable { } } } - $total = count($joinTable); if(is_array($newValue)) {