Subversion-Projekte lars-tiefland.cakephp

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* SVN FILE: $Id: acl.php 7945 2008-12-19 02:16:01Z gwoo $ */
3
/**
4
 * Access Control List factory class.
5
 *
6
 * Permissions system.
7
 *
8
 * PHP versions 4 and 5
9
 *
10
 * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
11
 * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
12
 *
13
 * Licensed under The MIT License
14
 * Redistributions of files must retain the above copyright notice.
15
 *
16
 * @filesource
17
 * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
18
 * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
19
 * @package       cake
20
 * @subpackage    cake.cake.libs.controller.components
21
 * @since         CakePHP(tm) v 0.10.0.1076
22
 * @version       $Revision: 7945 $
23
 * @modifiedby    $LastChangedBy: gwoo $
24
 * @lastmodified  $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
25
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
26
 */
27
/**
28
 * Access Control List factory class.
29
 *
30
 * Looks for ACL implementation class in core config, and returns an instance of that class.
31
 *
32
 * @package       cake
33
 * @subpackage    cake.cake.libs.controller.components
34
 */
35
class AclComponent extends Object {
36
/**
37
 * Instance of an ACL class
38
 *
39
 * @var object
40
 * @access protected
41
 */
42
	var $_Instance = null;
43
/**
44
 * Constructor. Will return an instance of the correct ACL class.
45
 *
46
 */
47
	function __construct() {
48
		$name = Inflector::camelize(strtolower(Configure::read('Acl.classname')));
49
		if (!class_exists($name)) {
50
			if (App::import('Component', $name)) {
51
				if (strpos($name, '.') !== false) {
52
					list($plugin, $name) = explode('.', $name);
53
				}
54
				$name .= 'Component';
55
			} else {
56
				trigger_error(sprintf(__('Could not find %s.', true), $name), E_USER_WARNING);
57
			}
58
		}
59
		$this->_Instance =& new $name();
60
		$this->_Instance->initialize($this);
61
	}
62
/**
63
 * Startup is not used
64
 *
65
 * @param object $controller Controller using this component
66
 * @return boolean Proceed with component usage (true), or fail (false)
67
 * @access public
68
 */
69
	function startup(&$controller) {
70
		return true;
71
	}
72
/**
73
 * Empty class defintion, to be overridden in subclasses.
74
 *
75
 * @access protected
76
 */
77
	function _initACL() {
78
	}
79
/**
80
 * Pass-thru function for ACL check instance.
81
 *
82
 * @param string $aro ARO
83
 * @param string $aco ACO
84
 * @param string $action Action (defaults to *)
85
 * @return boolean Success
86
 * @access public
87
 */
88
	function check($aro, $aco, $action = "*") {
89
		return $this->_Instance->check($aro, $aco, $action);
90
	}
91
/**
92
 * Pass-thru function for ACL allow instance.
93
 *
94
 * @param string $aro ARO
95
 * @param string $aco ACO
96
 * @param string $action Action (defaults to *)
97
 * @return boolean Success
98
 * @access public
99
 */
100
	function allow($aro, $aco, $action = "*") {
101
		return $this->_Instance->allow($aro, $aco, $action);
102
	}
103
/**
104
 * Pass-thru function for ACL deny instance.
105
 *
106
 * @param string $aro ARO
107
 * @param string $aco ACO
108
 * @param string $action Action (defaults to *)
109
 * @return boolean Success
110
 * @access public
111
 */
112
	function deny($aro, $aco, $action = "*") {
113
		return $this->_Instance->deny($aro, $aco, $action);
114
	}
115
/**
116
 * Pass-thru function for ACL inherit instance.
117
 *
118
 * @param string $aro ARO
119
 * @param string $aco ACO
120
 * @param string $action Action (defaults to *)
121
 * @return boolean Success
122
 * @access public
123
 */
124
	function inherit($aro, $aco, $action = "*") {
125
		return $this->_Instance->inherit($aro, $aco, $action);
126
	}
127
/**
128
 * Pass-thru function for ACL grant instance.
129
 *
130
 * @param string $aro ARO
131
 * @param string $aco ACO
132
 * @param string $action Action (defaults to *)
133
 * @return boolean Success
134
 * @access public
135
 */
136
	function grant($aro, $aco, $action = "*") {
137
		return $this->_Instance->grant($aro, $aco, $action);
138
	}
139
/**
140
 * Pass-thru function for ACL grant instance.
141
 *
142
 * @param string $aro ARO
143
 * @param string $aco ACO
144
 * @param string $action Action (defaults to *)
145
 * @return boolean Success
146
 * @access public
147
 */
148
	function revoke($aro, $aco, $action = "*") {
149
		return $this->_Instance->revoke($aro, $aco, $action);
150
	}
151
}
152
/**
153
 * Access Control List abstract class. Not to be instantiated.
154
 * Subclasses of this class are used by AclComponent to perform ACL checks in Cake.
155
 *
156
 * @package       cake
157
 * @subpackage    cake.cake.libs.controller.components
158
 * @abstract
159
 */
160
class AclBase extends Object {
161
/**
162
 * This class should never be instantiated, just subclassed.
163
 *
164
 */
165
	function __construct() {
166
		if (strcasecmp(get_class($this), "AclBase") == 0 || !is_subclass_of($this, "AclBase")) {
167
			trigger_error(__("[acl_base] The AclBase class constructor has been called, or the class was instantiated. This class must remain abstract. Please refer to the Cake docs for ACL configuration.", true), E_USER_ERROR);
168
			return NULL;
169
		}
170
	}
171
/**
172
 * Empty method to be overridden in subclasses
173
 *
174
 * @param string $aro ARO
175
 * @param string $aco ACO
176
 * @param string $action Action (defaults to *)
177
 * @access public
178
 */
179
	function check($aro, $aco, $action = "*") {
180
	}
181
/**
182
 * Empty method to be overridden in subclasses
183
 *
184
 * @param object $component Component
185
 * @access public
186
 */
187
	function initialize(&$component) {
188
	}
189
}
190
/**
191
 * In this file you can extend the AclBase.
192
 *
193
 * @package       cake
194
 * @subpackage    cake.cake.libs.model
195
 */
196
class DbAcl extends AclBase {
197
/**
198
 * Constructor
199
 *
200
 */
201
	function __construct() {
202
		parent::__construct();
203
		if (!class_exists('AclNode')) {
204
			uses('model' . DS . 'db_acl');
205
		}
206
		$this->Aro =& ClassRegistry::init(array('class' => 'Aro', 'alias' => 'Aro'));
207
		$this->Aco =& ClassRegistry::init(array('class' => 'Aco', 'alias' => 'Aco'));
208
	}
209
/**
210
 * Enter description here...
211
 *
212
 * @param object $component
213
 * @return void
214
 * @access public
215
 */
216
	function initialize(&$component) {
217
		$component->Aro = $this->Aro;
218
		$component->Aco = $this->Aco;
219
	}
220
/**
221
 * Checks if the given $aro has access to action $action in $aco
222
 *
223
 * @param string $aro ARO
224
 * @param string $aco ACO
225
 * @param string $action Action (defaults to *)
226
 * @return boolean Success (true if ARO has access to action in ACO, false otherwise)
227
 * @access public
228
 */
229
	function check($aro, $aco, $action = "*") {
230
		if ($aro == null || $aco == null) {
231
			return false;
232
		}
233
 
234
		$permKeys = $this->_getAcoKeys($this->Aro->Permission->schema());
235
		$aroPath = $this->Aro->node($aro);
236
		$acoPath = $this->Aco->node($aco);
237
 
238
		if (empty($aroPath) || empty($acoPath)) {
239
			trigger_error("DbAcl::check() - Failed ARO/ACO node lookup in permissions check.  Node references:\nAro: " . print_r($aro, true) . "\nAco: " . print_r($aco, true), E_USER_WARNING);
240
			return false;
241
		}
242
 
243
		if ($acoPath == null || $acoPath == array()) {
244
			trigger_error("DbAcl::check() - Failed ACO node lookup in permissions check.  Node references:\nAro: " . print_r($aro, true) . "\nAco: " . print_r($aco, true), E_USER_WARNING);
245
			return false;
246
		}
247
 
248
		$aroNode = $aroPath[0];
249
		$acoNode = $acoPath[0];
250
 
251
		if ($action != '*' && !in_array('_' . $action, $permKeys)) {
252
			trigger_error(sprintf(__("ACO permissions key %s does not exist in DbAcl::check()", true), $action), E_USER_NOTICE);
253
			return false;
254
		}
255
 
256
		$inherited = array();
257
		$acoIDs = Set::extract($acoPath, '{n}.' . $this->Aco->alias . '.id');
258
 
259
		$count = count($aroPath);
260
		for ($i = 0 ; $i < $count; $i++) {
261
			$permAlias = $this->Aro->Permission->alias;
262
 
263
			$perms = $this->Aro->Permission->find('all', array(
264
				'conditions' => array(
265
					"{$permAlias}.aro_id" => $aroPath[$i][$this->Aro->alias]['id'],
266
					"{$permAlias}.aco_id" => $acoIDs
267
				),
268
				'order' => array($this->Aco->alias . '.lft' => 'desc'),
269
				'recursive' => 0
270
			));
271
 
272
			if (empty($perms)) {
273
				continue;
274
			} else {
275
				$perms = Set::extract($perms, '{n}.' . $this->Aro->Permission->alias);
276
				foreach ($perms as $perm) {
277
					if ($action == '*') {
278
 
279
						foreach ($permKeys as $key) {
280
							if (!empty($perm)) {
281
								if ($perm[$key] == -1) {
282
									return false;
283
								} elseif ($perm[$key] == 1) {
284
									$inherited[$key] = 1;
285
								}
286
							}
287
						}
288
 
289
						if (count($inherited) === count($permKeys)) {
290
							return true;
291
						}
292
					} else {
293
						switch ($perm['_' . $action]) {
294
							case -1:
295
								return false;
296
							case 0:
297
								continue;
298
							break;
299
							case 1:
300
								return true;
301
							break;
302
						}
303
					}
304
				}
305
			}
306
		}
307
		return false;
308
	}
309
/**
310
 * Allow $aro to have access to action $actions in $aco
311
 *
312
 * @param string $aro ARO
313
 * @param string $aco ACO
314
 * @param string $actions Action (defaults to *)
315
 * @param integer $value Value to indicate access type (1 to give access, -1 to deny, 0 to inherit)
316
 * @return boolean Success
317
 * @access public
318
 */
319
	function allow($aro, $aco, $actions = "*", $value = 1) {
320
		$perms = $this->getAclLink($aro, $aco);
321
		$permKeys = $this->_getAcoKeys($this->Aro->Permission->schema());
322
		$save = array();
323
 
324
		if ($perms == false) {
325
			trigger_error(__('DbAcl::allow() - Invalid node', true), E_USER_WARNING);
326
			return false;
327
		}
328
		if (isset($perms[0])) {
329
			$save = $perms[0][$this->Aro->Permission->alias];
330
		}
331
 
332
		if ($actions == "*") {
333
			$permKeys = $this->_getAcoKeys($this->Aro->Permission->schema());
334
			$save = array_combine($permKeys, array_pad(array(), count($permKeys), $value));
335
		} else {
336
			if (!is_array($actions)) {
337
				$actions = array('_' . $actions);
338
			}
339
			if (is_array($actions)) {
340
				foreach ($actions as $action) {
341
					if ($action{0} != '_') {
342
						$action = '_' . $action;
343
					}
344
					if (in_array($action, $permKeys)) {
345
						$save[$action] = $value;
346
					}
347
				}
348
			}
349
		}
350
		list($save['aro_id'], $save['aco_id']) = array($perms['aro'], $perms['aco']);
351
 
352
		if ($perms['link'] != null && count($perms['link']) > 0) {
353
			$save['id'] = $perms['link'][0][$this->Aro->Permission->alias]['id'];
354
		} else {
355
			unset($save['id']);
356
			$this->Aro->Permission->id = null;
357
		}
358
		return ($this->Aro->Permission->save($save) !== false);
359
	}
360
/**
361
 * Deny access for $aro to action $action in $aco
362
 *
363
 * @param string $aro ARO
364
 * @param string $aco ACO
365
 * @param string $actions Action (defaults to *)
366
 * @return boolean Success
367
 * @access public
368
 */
369
	function deny($aro, $aco, $action = "*") {
370
		return $this->allow($aro, $aco, $action, -1);
371
	}
372
/**
373
 * Let access for $aro to action $action in $aco be inherited
374
 *
375
 * @param string $aro ARO
376
 * @param string $aco ACO
377
 * @param string $actions Action (defaults to *)
378
 * @return boolean Success
379
 * @access public
380
 */
381
	function inherit($aro, $aco, $action = "*") {
382
		return $this->allow($aro, $aco, $action, 0);
383
	}
384
/**
385
 * Allow $aro to have access to action $actions in $aco
386
 *
387
 * @param string $aro ARO
388
 * @param string $aco ACO
389
 * @param string $actions Action (defaults to *)
390
 * @return boolean Success
391
 * @see allow()
392
 * @access public
393
 */
394
	function grant($aro, $aco, $action = "*") {
395
		return $this->allow($aro, $aco, $action);
396
	}
397
/**
398
 * Deny access for $aro to action $action in $aco
399
 *
400
 * @param string $aro ARO
401
 * @param string $aco ACO
402
 * @param string $actions Action (defaults to *)
403
 * @return boolean Success
404
 * @see deny()
405
 * @access public
406
 */
407
	function revoke($aro, $aco, $action = "*") {
408
		return $this->deny($aro, $aco, $action);
409
	}
410
/**
411
 * Get an array of access-control links between the given Aro and Aco
412
 *
413
 * @param string $aro ARO
414
 * @param string $aco ACO
415
 * @return array Indexed array with: 'aro', 'aco' and 'link'
416
 * @access public
417
 */
418
	function getAclLink($aro, $aco) {
419
		$obj = array();
420
		$obj['Aro'] = $this->Aro->node($aro);
421
		$obj['Aco'] = $this->Aco->node($aco);
422
 
423
		if (empty($obj['Aro']) || empty($obj['Aco'])) {
424
			return false;
425
		}
426
 
427
		return array(
428
			'aro' => Set::extract($obj, 'Aro.0.'.$this->Aro->alias.'.id'),
429
			'aco'  => Set::extract($obj, 'Aco.0.'.$this->Aco->alias.'.id'),
430
			'link' => $this->Aro->Permission->find('all', array('conditions' => array(
431
				$this->Aro->Permission->alias . '.aro_id' => Set::extract($obj, 'Aro.0.'.$this->Aro->alias.'.id'),
432
				$this->Aro->Permission->alias . '.aco_id' => Set::extract($obj, 'Aco.0.'.$this->Aco->alias.'.id')
433
			)))
434
		);
435
	}
436
/**
437
 * Get the keys used in an ACO
438
 *
439
 * @param array $keys Permission model info
440
 * @return array ACO keys
441
 * @access protected
442
 */
443
	function _getAcoKeys($keys) {
444
		$newKeys = array();
445
		$keys = array_keys($keys);
446
		foreach ($keys as $key) {
447
			if (!in_array($key, array('id', 'aro_id', 'aco_id'))) {
448
				$newKeys[] = $key;
449
			}
450
		}
451
		return $newKeys;
452
	}
453
}
454
/**
455
 * In this file you can extend the AclBase.
456
 *
457
 * @package       cake
458
 * @subpackage    cake.cake.libs.model.iniacl
459
 */
460
class IniAcl extends AclBase {
461
/**
462
 * Array with configuration, parsed from ini file
463
 *
464
 * @var array
465
 * @access public
466
 */
467
	var $config = null;
468
/**
469
 * The constructor must be overridden, as AclBase is abstract.
470
 *
471
 */
472
	function __construct() {
473
	}
474
/**
475
 * Main ACL check function. Checks to see if the ARO (access request object) has access to the ACO (access control object).
476
 * Looks at the acl.ini.php file for permissions (see instructions in /config/acl.ini.php).
477
 *
478
 * @param string $aro ARO
479
 * @param string $aco ACO
480
 * @param string $aco_action Action
481
 * @return boolean Success
482
 * @access public
483
 */
484
	function check($aro, $aco, $aco_action = null) {
485
		if ($this->config == null) {
486
			$this->config = $this->readConfigFile(CONFIGS . 'acl.ini.php');
487
		}
488
		$aclConfig = $this->config;
489
 
490
		if (isset($aclConfig[$aro]['deny'])) {
491
			$userDenies = $this->arrayTrim(explode(",", $aclConfig[$aro]['deny']));
492
 
493
			if (array_search($aco, $userDenies)) {
494
				return false;
495
			}
496
		}
497
 
498
		if (isset($aclConfig[$aro]['allow'])) {
499
			$userAllows = $this->arrayTrim(explode(",", $aclConfig[$aro]['allow']));
500
 
501
			if (array_search($aco, $userAllows)) {
502
				return true;
503
			}
504
		}
505
 
506
		if (isset($aclConfig[$aro]['groups'])) {
507
			$userGroups = $this->arrayTrim(explode(",", $aclConfig[$aro]['groups']));
508
 
509
			foreach ($userGroups as $group) {
510
				if (array_key_exists($group, $aclConfig)) {
511
					if (isset($aclConfig[$group]['deny'])) {
512
						$groupDenies=$this->arrayTrim(explode(",", $aclConfig[$group]['deny']));
513
 
514
						if (array_search($aco, $groupDenies)) {
515
							return false;
516
						}
517
					}
518
 
519
					if (isset($aclConfig[$group]['allow'])) {
520
						$groupAllows = $this->arrayTrim(explode(",", $aclConfig[$group]['allow']));
521
 
522
						if (array_search($aco, $groupAllows)) {
523
							return true;
524
						}
525
					}
526
				}
527
			}
528
		}
529
		return false;
530
	}
531
/**
532
 * Parses an INI file and returns an array that reflects the INI file's section structure. Double-quote friendly.
533
 *
534
 * @param string $fileName File
535
 * @return array INI section structure
536
 * @access public
537
 */
538
	function readConfigFile($fileName) {
539
		$fileLineArray = file($fileName);
540
 
541
		foreach ($fileLineArray as $fileLine) {
542
			$dataLine = trim($fileLine);
543
			$firstChar = substr($dataLine, 0, 1);
544
 
545
			if ($firstChar != ';' && $dataLine != '') {
546
				if ($firstChar == '[' && substr($dataLine, -1, 1) == ']') {
547
					$sectionName = preg_replace('/[\[\]]/', '', $dataLine);
548
				} else {
549
					$delimiter = strpos($dataLine, '=');
550
 
551
					if ($delimiter > 0) {
552
						$key = strtolower(trim(substr($dataLine, 0, $delimiter)));
553
						$value = trim(substr($dataLine, $delimiter + 1));
554
 
555
						if (substr($value, 0, 1) == '"' && substr($value, -1) == '"') {
556
							$value = substr($value, 1, -1);
557
						}
558
 
559
						$iniSetting[$sectionName][$key]=stripcslashes($value);
560
					} else {
561
						if (!isset($sectionName)) {
562
							$sectionName = '';
563
						}
564
 
565
						$iniSetting[$sectionName][strtolower(trim($dataLine))]='';
566
					}
567
				}
568
			}
569
		}
570
 
571
		return $iniSetting;
572
	}
573
/**
574
 * Removes trailing spaces on all array elements (to prepare for searching)
575
 *
576
 * @param array $array Array to trim
577
 * @return array Trimmed array
578
 * @access public
579
 */
580
	function arrayTrim($array) {
581
		foreach ($array as $key => $value) {
582
			$array[$key] = trim($value);
583
		}
584
		array_unshift($array, "");
585
		return $array;
586
	}
587
}
588
?>