00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 App::import('Model', 'App');
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 class AclNode extends AppModel {
00041
00042
00043
00044
00045
00046
00047 var $cacheQueries = false;
00048
00049
00050
00051
00052
00053
00054 var $actsAs = array('Tree' => 'nested');
00055
00056
00057
00058
00059 function __construct() {
00060 $config = Configure::read('Acl.database');
00061 if (isset($config)) {
00062 $this->useDbConfig = $config;
00063 }
00064 parent::__construct();
00065 }
00066
00067
00068
00069
00070
00071
00072
00073 function node($ref = null) {
00074 $db =& ConnectionManager::getDataSource($this->useDbConfig);
00075 $type = $this->alias;
00076 $result = null;
00077
00078 if (!empty($this->useTable)) {
00079 $table = $this->useTable;
00080 } else {
00081 $table = Inflector::pluralize(Inflector::underscore($type));
00082 }
00083
00084 if (empty($ref)) {
00085 return null;
00086 } elseif (is_string($ref)) {
00087 $path = explode('/', $ref);
00088 $start = $path[0];
00089 unset($path[0]);
00090
00091 $queryData = array(
00092 'conditions' => array(
00093 $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft"),
00094 $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght")),
00095 'fields' => array('id', 'parent_id', 'model', 'foreign_key', 'alias'),
00096 'joins' => array(array(
00097 'table' => $db->fullTableName($this),
00098 'alias' => "{$type}0",
00099 'type' => 'LEFT',
00100 'conditions' => array("{$type}0.alias" => $start)
00101 )),
00102 'order' => $db->name("{$type}.lft") . ' DESC'
00103 );
00104
00105 foreach ($path as $i => $alias) {
00106 $j = $i - 1;
00107
00108 $queryData['joins'][] = array(
00109 'table' => $db->fullTableName($this),
00110 'alias' => "{$type}{$i}",
00111 'type' => 'LEFT',
00112 'conditions' => array(
00113 $db->name("{$type}{$i}.lft") . ' > ' . $db->name("{$type}{$j}.lft"),
00114 $db->name("{$type}{$i}.rght") . ' < ' . $db->name("{$type}{$j}.rght"),
00115 $db->name("{$type}{$i}.alias") . ' = ' . $db->value($alias, 'string')
00116 )
00117 );
00118
00119 $queryData['conditions'] = array('or' => array(
00120 $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft") . ' AND ' . $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght"),
00121 $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}{$i}.lft") . ' AND ' . $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}{$i}.rght"))
00122 );
00123 }
00124 $result = $db->read($this, $queryData, -1);
00125 $path = array_values($path);
00126
00127 if (
00128 !isset($result[0][$type]) ||
00129 (!empty($path) && $result[0][$type]['alias'] != $path[count($path) - 1]) ||
00130 (empty($path) && $result[0][$type]['alias'] != $start)
00131 ) {
00132 return false;
00133 }
00134 } elseif (is_object($ref) && is_a($ref, 'Model')) {
00135 $ref = array('model' => $ref->alias, 'foreign_key' => $ref->id);
00136 } elseif (is_array($ref) && !(isset($ref['model']) && isset($ref['foreign_key']))) {
00137 $name = key($ref);
00138
00139 if (PHP5) {
00140 $model = ClassRegistry::init(array('class' => $name, 'alias' => $name));
00141 } else {
00142 $model =& ClassRegistry::init(array('class' => $name, 'alias' => $name));
00143 }
00144
00145 if (empty($model)) {
00146 trigger_error("Model class '$name' not found in AclNode::node() when trying to bind {$this->alias} object", E_USER_WARNING);
00147 return null;
00148 }
00149
00150 $tmpRef = null;
00151 if (method_exists($model, 'bindNode')) {
00152 $tmpRef = $model->bindNode($ref);
00153 }
00154 if (empty($tmpRef)) {
00155 $ref = array('model' => $name, 'foreign_key' => $ref[$name][$model->primaryKey]);
00156 } else {
00157 if (is_string($tmpRef)) {
00158 return $this->node($tmpRef);
00159 }
00160 $ref = $tmpRef;
00161 }
00162 }
00163 if (is_array($ref)) {
00164 if (is_array(current($ref)) && is_string(key($ref))) {
00165 $name = key($ref);
00166 $ref = current($ref);
00167 }
00168 foreach ($ref as $key => $val) {
00169 if (strpos($key, $type) !== 0 && strpos($key, '.') === false) {
00170 unset($ref[$key]);
00171 $ref["{$type}0.{$key}"] = $val;
00172 }
00173 }
00174 $queryData = array(
00175 'conditions' => $ref,
00176 'fields' => array('id', 'parent_id', 'model', 'foreign_key', 'alias'),
00177 'joins' => array(array(
00178 'table' => $db->fullTableName($this),
00179 'alias' => "{$type}0",
00180 'type' => 'LEFT',
00181 'conditions' => array(
00182 $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft"),
00183 $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght")
00184 )
00185 )),
00186 'order' => $db->name("{$type}.lft") . ' DESC'
00187 );
00188 $result = $db->read($this, $queryData, -1);
00189
00190 if (!$result) {
00191 trigger_error("AclNode::node() - Couldn't find {$type} node identified by \"" . print_r($ref, true) . "\"", E_USER_WARNING);
00192 }
00193 }
00194 return $result;
00195 }
00196 }
00197
00198
00199
00200
00201
00202
00203 class Aco extends AclNode {
00204
00205
00206
00207
00208
00209
00210 var $name = 'Aco';
00211
00212
00213
00214
00215
00216
00217 var $hasAndBelongsToMany = array('Aro' => array('with' => 'Permission'));
00218 }
00219
00220
00221
00222
00223
00224
00225 class AcoAction extends AppModel {
00226
00227
00228
00229
00230
00231
00232 var $name = 'AcoAction';
00233
00234
00235
00236
00237
00238
00239 var $belongsTo = array('Aco');
00240 }
00241
00242
00243
00244
00245
00246
00247 class Aro extends AclNode {
00248
00249
00250
00251
00252
00253
00254 var $name = 'Aro';
00255
00256
00257
00258
00259
00260
00261 var $hasAndBelongsToMany = array('Aco' => array('with' => 'Permission'));
00262 }
00263
00264
00265
00266
00267
00268
00269 class Permission extends AppModel {
00270
00271
00272
00273
00274
00275
00276 var $name = 'Permission';
00277
00278
00279
00280
00281
00282
00283 var $cacheQueries = false;
00284
00285
00286
00287
00288
00289
00290 var $useTable = 'aros_acos';
00291
00292
00293
00294
00295
00296
00297 var $belongsTo = array('Aro', 'Aco');
00298
00299
00300
00301
00302
00303
00304 var $actsAs = null;
00305
00306
00307
00308
00309 function __construct() {
00310 $config = Configure::read('Acl.database');
00311 if (!empty($config)) {
00312 $this->useDbConfig = $config;
00313 }
00314 parent::__construct();
00315 }
00316 }
00317 ?>