-
Notifications
You must be signed in to change notification settings - Fork 11
/
db_acl.php
executable file
·317 lines (306 loc) · 7.58 KB
/
db_acl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
/* SVN FILE: $Id$ */
/**
* This is core configuration file.
*
* Use it to configure core behaviour ofCake.
*
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @package cake
* @subpackage cake.cake.libs.model
* @since CakePHP(tm) v 0.2.9
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
/**
* Load Model and AppModel
*/
App::import('Model', 'App');
/**
* Short description for file.
*
* Long description for file
*
*
* @package cake
* @subpackage cake.cake.libs.model
*/
class AclNode extends AppModel {
/**
* Explicitly disable in-memory query caching for ACL models
*
* @var boolean
* @access public
*/
var $cacheQueries = false;
/**
* ACL models use the Tree behavior
*
* @var array
* @access public
*/
var $actsAs = array('Tree' => 'nested');
/**
* Constructor
*
*/
function __construct() {
$config = Configure::read('Acl.database');
if (isset($config)) {
$this->useDbConfig = $config;
}
parent::__construct();
}
/**
* Retrieves the Aro/Aco node for this model
*
* @param mixed $ref Array with 'model' and 'foreign_key', model object, or string value
* @return array Node found in database
* @access public
*/
function node($ref = null) {
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$type = $this->alias;
$result = null;
if (!empty($this->useTable)) {
$table = $this->useTable;
} else {
$table = Inflector::pluralize(Inflector::underscore($type));
}
if (empty($ref)) {
return null;
} elseif (is_string($ref)) {
$path = explode('/', $ref);
$start = $path[0];
unset($path[0]);
$queryData = array(
'conditions' => array(
$db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft"),
$db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght")),
'fields' => array('id', 'parent_id', 'model', 'foreign_key', 'alias'),
'joins' => array(array(
'table' => $db->fullTableName($this),
'alias' => "{$type}0",
'type' => 'LEFT',
'conditions' => array("{$type}0.alias" => $start)
)),
'order' => $db->name("{$type}.lft") . ' DESC'
);
foreach ($path as $i => $alias) {
$j = $i - 1;
$queryData['joins'][] = array(
'table' => $db->fullTableName($this),
'alias' => "{$type}{$i}",
'type' => 'LEFT',
'conditions' => array(
$db->name("{$type}{$i}.lft") . ' > ' . $db->name("{$type}{$j}.lft"),
$db->name("{$type}{$i}.rght") . ' < ' . $db->name("{$type}{$j}.rght"),
$db->name("{$type}{$i}.alias") . ' = ' . $db->value($alias, 'string')
)
);
$queryData['conditions'] = array('or' => array(
$db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft") . ' AND ' . $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght"),
$db->name("{$type}.lft") . ' <= ' . $db->name("{$type}{$i}.lft") . ' AND ' . $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}{$i}.rght"))
);
}
$result = $db->read($this, $queryData, -1);
$path = array_values($path);
if (
!isset($result[0][$type]) ||
(!empty($path) && $result[0][$type]['alias'] != $path[count($path) - 1]) ||
(empty($path) && $result[0][$type]['alias'] != $start)
) {
return false;
}
} elseif (is_object($ref) && is_a($ref, 'Model')) {
$ref = array('model' => $ref->alias, 'foreign_key' => $ref->id);
} elseif (is_array($ref) && !(isset($ref['model']) && isset($ref['foreign_key']))) {
$name = key($ref);
if (PHP5) {
$model = ClassRegistry::init(array('class' => $name, 'alias' => $name));
} else {
$model =& ClassRegistry::init(array('class' => $name, 'alias' => $name));
}
if (empty($model)) {
trigger_error("Model class '$name' not found in AclNode::node() when trying to bind {$this->alias} object", E_USER_WARNING);
return null;
}
$tmpRef = null;
if (method_exists($model, 'bindNode')) {
$tmpRef = $model->bindNode($ref);
}
if (empty($tmpRef)) {
$ref = array('model' => $name, 'foreign_key' => $ref[$name][$model->primaryKey]);
} else {
if (is_string($tmpRef)) {
return $this->node($tmpRef);
}
$ref = $tmpRef;
}
}
if (is_array($ref)) {
if (is_array(current($ref)) && is_string(key($ref))) {
$name = key($ref);
$ref = current($ref);
}
foreach ($ref as $key => $val) {
if (strpos($key, $type) !== 0 && strpos($key, '.') === false) {
unset($ref[$key]);
$ref["{$type}0.{$key}"] = $val;
}
}
$queryData = array(
'conditions' => $ref,
'fields' => array('id', 'parent_id', 'model', 'foreign_key', 'alias'),
'joins' => array(array(
'table' => $db->fullTableName($this),
'alias' => "{$type}0",
'type' => 'LEFT',
'conditions' => array(
$db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft"),
$db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght")
)
)),
'order' => $db->name("{$type}.lft") . ' DESC'
);
$result = $db->read($this, $queryData, -1);
if (!$result) {
trigger_error("AclNode::node() - Couldn't find {$type} node identified by \"" . print_r($ref, true) . "\"", E_USER_WARNING);
}
}
return $result;
}
}
/**
* Access Control Object
*
* @package cake
* @subpackage cake.cake.libs.model
*/
class Aco extends AclNode {
/**
* Model name
*
* @var string
* @access public
*/
var $name = 'Aco';
/**
* Binds to ARO nodes through permissions settings
*
* @var array
* @access public
*/
var $hasAndBelongsToMany = array('Aro' => array('with' => 'Permission'));
}
/**
* Action for Access Control Object
*
* @package cake
* @subpackage cake.cake.libs.model
*/
class AcoAction extends AppModel {
/**
* Model name
*
* @var string
* @access public
*/
var $name = 'AcoAction';
/**
* ACO Actions belong to ACOs
*
* @var array
* @access public
*/
var $belongsTo = array('Aco');
}
/**
* Access Request Object
*
* @package cake
* @subpackage cake.cake.libs.model
*/
class Aro extends AclNode {
/**
* Model name
*
* @var string
* @access public
*/
var $name = 'Aro';
/**
* AROs are linked to ACOs by means of Permission
*
* @var array
* @access public
*/
var $hasAndBelongsToMany = array('Aco' => array('with' => 'Permission'));
}
/**
* Permissions linking AROs with ACOs
*
* @package cake
* @subpackage cake.cake.libs.model
*/
class Permission extends AppModel {
/**
* Model name
*
* @var string
* @access public
*/
var $name = 'Permission';
/**
* Explicitly disable in-memory query caching
*
* @var boolean
* @access public
*/
var $cacheQueries = false;
/**
* Override default table name
*
* @var string
* @access public
*/
var $useTable = 'aros_acos';
/**
* Permissions link AROs with ACOs
*
* @var array
* @access public
*/
var $belongsTo = array('Aro', 'Aco');
/**
* No behaviors for this model
*
* @var array
* @access public
*/
var $actsAs = null;
/**
* Constructor, used to tell this model to use the
* database configured for ACL
*/
function __construct() {
$config = Configure::read('Acl.database');
if (!empty($config)) {
$this->useDbConfig = $config;
}
parent::__construct();
}
}
?>