1: <?php
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: class AclBehavior extends ModelBehavior {
31:
32: 33: 34: 35: 36: 37:
38: var $__typeMaps = array('requester' => 'Aro', 'controlled' => 'Aco');
39:
40: 41: 42: 43: 44: 45: 46:
47: function setup(&$model, $config = array()) {
48: if (is_string($config)) {
49: $config = array('type' => $config);
50: }
51: $this->settings[$model->name] = array_merge(array('type' => 'requester'), (array)$config);
52: $this->settings[$model->name]['type'] = strtolower($this->settings[$model->name]['type']);
53:
54: $type = $this->__typeMaps[$this->settings[$model->name]['type']];
55: if (!class_exists('AclNode')) {
56: require LIBS . 'model' . DS . 'db_acl.php';
57: }
58: if (PHP5) {
59: $model->{$type} = ClassRegistry::init($type);
60: } else {
61: $model->{$type} =& ClassRegistry::init($type);
62: }
63: if (!method_exists($model, 'parentNode')) {
64: trigger_error(sprintf(__('Callback parentNode() not defined in %s', true), $model->alias), E_USER_WARNING);
65: }
66: }
67:
68: 69: 70: 71: 72: 73: 74: 75:
76: function node(&$model, $ref = null) {
77: $type = $this->__typeMaps[$this->settings[$model->name]['type']];
78: if (empty($ref)) {
79: $ref = array('model' => $model->name, 'foreign_key' => $model->id);
80: }
81: return $model->{$type}->node($ref);
82: }
83:
84: 85: 86: 87: 88: 89: 90:
91: function afterSave(&$model, $created) {
92: $type = $this->__typeMaps[$this->settings[$model->name]['type']];
93: $parent = $model->parentNode();
94: if (!empty($parent)) {
95: $parent = $this->node($model, $parent);
96: }
97: $data = array(
98: 'parent_id' => isset($parent[0][$type]['id']) ? $parent[0][$type]['id'] : null,
99: 'model' => $model->name,
100: 'foreign_key' => $model->id
101: );
102: if (!$created) {
103: $node = $this->node($model);
104: $data['id'] = isset($node[0][$type]['id']) ? $node[0][$type]['id'] : null;
105: }
106: $model->{$type}->create();
107: $model->{$type}->save($data);
108: }
109:
110: 111: 112: 113: 114: 115:
116: function afterDelete(&$model) {
117: $type = $this->__typeMaps[$this->settings[$model->name]['type']];
118: $node = Set::extract($this->node($model), "0.{$type}.id");
119: if (!empty($node)) {
120: $model->{$type}->delete($node);
121: }
122: }
123: }
124: