1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17:
18:
19: App::uses('AppModel', 'Model');
20:
21: 22: 23: 24: 25:
26: class AclNode extends AppModel {
27:
28: 29: 30: 31: 32:
33: public $cacheQueries = false;
34:
35: 36: 37: 38: 39:
40: public $actsAs = array('Tree' => array('type' => 'nested'));
41:
42: 43: 44: 45:
46: public function __construct() {
47: $config = Configure::read('Acl.database');
48: if (isset($config)) {
49: $this->useDbConfig = $config;
50: }
51: parent::__construct();
52: }
53:
54: 55: 56: 57: 58: 59:
60: public function node($ref = null) {
61: $db = $this->getDataSource();
62: $type = $this->alias;
63: $result = null;
64:
65: if (!empty($this->useTable)) {
66: $table = $this->useTable;
67: } else {
68: $table = Inflector::pluralize(Inflector::underscore($type));
69: }
70:
71: if (empty($ref)) {
72: return null;
73: } elseif (is_string($ref)) {
74: $path = explode('/', $ref);
75: $start = $path[0];
76: unset($path[0]);
77:
78: $queryData = array(
79: 'conditions' => array(
80: $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft"),
81: $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght")),
82: 'fields' => array('id', 'parent_id', 'model', 'foreign_key', 'alias'),
83: 'joins' => array(array(
84: 'table' => $table,
85: 'alias' => "{$type}0",
86: 'type' => 'LEFT',
87: 'conditions' => array("{$type}0.alias" => $start)
88: )),
89: 'order' => $db->name("{$type}.lft") . ' DESC'
90: );
91:
92: foreach ($path as $i => $alias) {
93: $j = $i - 1;
94:
95: $queryData['joins'][] = array(
96: 'table' => $table,
97: 'alias' => "{$type}{$i}",
98: 'type' => 'LEFT',
99: 'conditions' => array(
100: $db->name("{$type}{$i}.lft") . ' > ' . $db->name("{$type}{$j}.lft"),
101: $db->name("{$type}{$i}.rght") . ' < ' . $db->name("{$type}{$j}.rght"),
102: $db->name("{$type}{$i}.alias") . ' = ' . $db->value($alias, 'string'),
103: $db->name("{$type}{$j}.id") . ' = ' . $db->name("{$type}{$i}.parent_id")
104: )
105: );
106:
107: $queryData['conditions'] = array('or' => array(
108: $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft") . ' AND ' . $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght"),
109: $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}{$i}.lft") . ' AND ' . $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}{$i}.rght"))
110: );
111: }
112: $result = $db->read($this, $queryData, -1);
113: $path = array_values($path);
114:
115: if (
116: !isset($result[0][$type]) ||
117: (!empty($path) && $result[0][$type]['alias'] != $path[count($path) - 1]) ||
118: (empty($path) && $result[0][$type]['alias'] != $start)
119: ) {
120: return false;
121: }
122: } elseif (is_object($ref) && is_a($ref, 'Model')) {
123: $ref = array('model' => $ref->name, 'foreign_key' => $ref->id);
124: } elseif (is_array($ref) && !(isset($ref['model']) && isset($ref['foreign_key']))) {
125: $name = key($ref);
126: list($plugin, $alias) = pluginSplit($name);
127:
128: $model = ClassRegistry::init(array('class' => $name, 'alias' => $alias));
129:
130: if (empty($model)) {
131: trigger_error(__d('cake_dev', "Model class '%s' not found in AclNode::node() when trying to bind %s object", $type, $this->alias), E_USER_WARNING);
132: return null;
133: }
134:
135: $tmpRef = null;
136: if (method_exists($model, 'bindNode')) {
137: $tmpRef = $model->bindNode($ref);
138: }
139: if (empty($tmpRef)) {
140: $ref = array('model' => $alias, 'foreign_key' => $ref[$name][$model->primaryKey]);
141: } else {
142: if (is_string($tmpRef)) {
143: return $this->node($tmpRef);
144: }
145: $ref = $tmpRef;
146: }
147: }
148: if (is_array($ref)) {
149: if (is_array(current($ref)) && is_string(key($ref))) {
150: $name = key($ref);
151: $ref = current($ref);
152: }
153: foreach ($ref as $key => $val) {
154: if (strpos($key, $type) !== 0 && strpos($key, '.') === false) {
155: unset($ref[$key]);
156: $ref["{$type}0.{$key}"] = $val;
157: }
158: }
159: $queryData = array(
160: 'conditions' => $ref,
161: 'fields' => array('id', 'parent_id', 'model', 'foreign_key', 'alias'),
162: 'joins' => array(array(
163: 'table' => $table,
164: 'alias' => "{$type}0",
165: 'type' => 'LEFT',
166: 'conditions' => array(
167: $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft"),
168: $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght")
169: )
170: )),
171: 'order' => $db->name("{$type}.lft") . ' DESC'
172: );
173: $result = $db->read($this, $queryData, -1);
174:
175: if (!$result) {
176: trigger_error(__d('cake_dev', "AclNode::node() - Couldn't find %s node identified by \"%s\"", $type, print_r($ref, true)), E_USER_WARNING);
177: }
178: }
179: return $result;
180: }
181: }
182: