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