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