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