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
 * ACL behavior class.
5
 *
6
 * Enables objects to easily tie into an ACL system
7
 *
8
 * PHP versions 4 and 5
9
 *
10
 * CakePHP :  Rapid Development Framework (http://www.cakephp.org)
11
 * Copyright 2006-2008, Cake Software Foundation, Inc.
12
 *
13
 * Licensed under The MIT License
14
 * Redistributions of files must retain the above copyright notice.
15
 *
16
 * @filesource
17
 * @copyright     Copyright 2006-2008, Cake Software Foundation, Inc.
18
 * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
19
 * @package       cake
20
 * @subpackage    cake.cake.libs.model.behaviors
21
 * @since         CakePHP v 1.2.0.4487
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
 * Short description for file
29
 *
30
 * Long description for file
31
 *
32
 * @package       cake
33
 * @subpackage    cake.cake.libs.model.behaviors
34
 */
35
class AclBehavior extends ModelBehavior {
36
/**
37
 * Maps ACL type options to ACL models
38
 *
39
 * @var array
40
 * @access protected
41
 */
42
	var $__typeMaps = array('requester' => 'Aro', 'controlled' => 'Aco');
43
/**
44
 * Sets up the configuation for the model, and loads ACL models if they haven't been already
45
 *
46
 * @param mixed $config
47
 * @return void
48
 * @access public
49
 */
50
	function setup(&$model, $config = array()) {
51
		if (is_string($config)) {
52
			$config = array('type' => $config);
53
		}
54
		$this->settings[$model->name] = array_merge(array('type' => 'requester'), (array)$config);
55
 
56
		$type = $this->__typeMaps[$this->settings[$model->name]['type']];
57
		if (!class_exists('AclNode')) {
58
			uses('model' . DS . 'db_acl');
59
		}
60
		$model->{$type} =& ClassRegistry::init($type);
61
		if (!method_exists($model, 'parentNode')) {
62
			trigger_error("Callback parentNode() not defined in {$model->alias}", E_USER_WARNING);
63
		}
64
	}
65
/**
66
 * Retrieves the Aro/Aco node for this model
67
 *
68
 * @param mixed $ref
69
 * @return array
70
 * @access public
71
 */
72
	function node(&$model, $ref = null) {
73
		$type = $this->__typeMaps[strtolower($this->settings[$model->name]['type'])];
74
		if (empty($ref)) {
75
			$ref = array('model' => $model->name, 'foreign_key' => $model->id);
76
		}
77
		return $model->{$type}->node($ref);
78
	}
79
/**
80
 * Creates a new ARO/ACO node bound to this record
81
 *
82
 * @param boolean $created True if this is a new record
83
 * @return void
84
 * @access public
85
 */
86
	function afterSave(&$model, $created) {
87
		if ($created) {
88
			$type = $this->__typeMaps[strtolower($this->settings[$model->name]['type'])];
89
			$parent = $model->parentNode();
90
			if (!empty($parent)) {
91
				$parent = $this->node($model, $parent);
92
			} else {
93
				$parent = null;
94
			}
95
 
96
			$model->{$type}->create();
97
			$model->{$type}->save(array(
98
				'parent_id'		=> Set::extract($parent, "0.{$type}.id"),
99
				'model'			=> $model->name,
100
				'foreign_key'	=> $model->id
101
			));
102
		}
103
	}
104
/**
105
 * Destroys the ARO/ACO node bound to the deleted record
106
 *
107
 * @return void
108
 * @access public
109
 */
110
	function afterDelete(&$model) {
111
		$type = $this->__typeMaps[strtolower($this->settings[$model->name]['type'])];
112
		$node = Set::extract($this->node($model), "0.{$type}.id");
113
		if (!empty($node)) {
114
			$model->{$type}->delete($node);
115
		}
116
	}
117
}
118
 
119
?>