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 class AclBehavior extends ModelBehavior {
00036
00037
00038
00039
00040
00041
00042 var $__typeMaps = array('requester' => 'Aro', 'controlled' => 'Aco');
00043
00044
00045
00046
00047
00048
00049
00050 function setup(&$model, $config = array()) {
00051 if (is_string($config)) {
00052 $config = array('type' => $config);
00053 }
00054 $this->settings[$model->name] = array_merge(array('type' => 'requester'), (array)$config);
00055
00056 $type = $this->__typeMaps[$this->settings[$model->name]['type']];
00057 if (!class_exists('AclNode')) {
00058 uses('model' . DS . 'db_acl');
00059 }
00060 $model->{$type} =& ClassRegistry::init($type);
00061 if (!method_exists($model, 'parentNode')) {
00062 trigger_error("Callback parentNode() not defined in {$model->alias}", E_USER_WARNING);
00063 }
00064 }
00065
00066
00067
00068
00069
00070
00071
00072 function node(&$model, $ref = null) {
00073 $type = $this->__typeMaps[strtolower($this->settings[$model->name]['type'])];
00074 if (empty($ref)) {
00075 $ref = array('model' => $model->name, 'foreign_key' => $model->id);
00076 }
00077 return $model->{$type}->node($ref);
00078 }
00079
00080
00081
00082
00083
00084
00085
00086 function afterSave(&$model, $created) {
00087 if ($created) {
00088 $type = $this->__typeMaps[strtolower($this->settings[$model->name]['type'])];
00089 $parent = $model->parentNode();
00090 if (!empty($parent)) {
00091 $parent = $this->node($model, $parent);
00092 } else {
00093 $parent = null;
00094 }
00095
00096 $model->{$type}->create();
00097 $model->{$type}->save(array(
00098 'parent_id' => Set::extract($parent, "0.{$type}.id"),
00099 'model' => $model->name,
00100 'foreign_key' => $model->id
00101 ));
00102 }
00103 }
00104
00105
00106
00107
00108
00109
00110 function afterDelete(&$model) {
00111 $type = $this->__typeMaps[strtolower($this->settings[$model->name]['type'])];
00112 $node = Set::extract($this->node($model), "0.{$type}.id");
00113 if (!empty($node)) {
00114 $model->{$type}->delete($node);
00115 }
00116 }
00117 }
00118
00119 ?>