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: 44: 45: 46: 47:
48: public function __construct($id = false, $table = null, $ds = null) {
49: $config = Configure::read('Acl.database');
50: if (isset($config)) {
51: $this->useDbConfig = $config;
52: }
53: parent::__construct($id, $table, $ds);
54: }
55:
56: 57: 58: 59: 60: 61: 62:
63: public function node($ref = null) {
64: $db = $this->getDataSource();
65: $type = $this->alias;
66: $result = null;
67:
68: if (!empty($this->useTable)) {
69: $table = $this->useTable;
70: } else {
71: $table = Inflector::pluralize(Inflector::underscore($type));
72: }
73:
74: if (empty($ref)) {
75: return null;
76: } elseif (is_string($ref)) {
77: $path = explode('/', $ref);
78: $start = $path[0];
79: unset($path[0]);
80:
81: $queryData = array(
82: 'conditions' => array(
83: $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft"),
84: $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght")),
85: 'fields' => array('id', 'parent_id', 'model', 'foreign_key', 'alias'),
86: 'joins' => array(array(
87: 'table' => $table,
88: 'alias' => "{$type}0",
89: 'type' => 'INNER',
90: 'conditions' => array("{$type}0.alias" => $start)
91: )),
92: 'order' => $db->name("{$type}.lft") . ' DESC'
93: );
94:
95: $conditionsAfterJoin = array();
96:
97: foreach ($path as $i => $alias) {
98: $j = $i - 1;
99:
100: $queryData['joins'][] = array(
101: 'table' => $table,
102: 'alias' => "{$type}{$i}",
103: 'type' => 'INNER',
104: 'conditions' => array(
105: "{$type}{$i}.alias" => $alias
106: )
107: );
108:
109:
110: $conditionsAfterJoin[] = $db->name("{$type}{$j}.id") . ' = ' . $db->name("{$type}{$i}.parent_id");
111: $conditionsAfterJoin[] = $db->name("{$type}{$i}.rght") . ' < ' . $db->name("{$type}{$j}.rght");
112: $conditionsAfterJoin[] = $db->name("{$type}{$i}.lft") . ' > ' . $db->name("{$type}{$j}.lft");
113:
114: $queryData['conditions'] = array('or' => array(
115: $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft") . ' AND ' . $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght"),
116: $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}{$i}.lft") . ' AND ' . $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}{$i}.rght"))
117: );
118: }
119: $queryData['conditions'] = array_merge($queryData['conditions'], $conditionsAfterJoin);
120: $result = $db->read($this, $queryData, -1);
121: $path = array_values($path);
122:
123: if (!isset($result[0][$type]) ||
124: (!empty($path) && $result[0][$type]['alias'] != $path[count($path) - 1]) ||
125: (empty($path) && $result[0][$type]['alias'] != $start)
126: ) {
127: return false;
128: }
129: } elseif (is_object($ref) && $ref instanceof Model) {
130: $ref = array('model' => $ref->name, 'foreign_key' => $ref->id);
131: } elseif (is_array($ref) && !(isset($ref['model']) && isset($ref['foreign_key']))) {
132: $name = key($ref);
133: list(, $alias) = pluginSplit($name);
134:
135: $model = ClassRegistry::init(array('class' => $name, 'alias' => $alias));
136:
137: if (empty($model)) {
138: throw new CakeException('cake_dev', "Model class '%s' not found in AclNode::node() when trying to bind %s object", $type, $this->alias);
139: }
140:
141: $tmpRef = null;
142: if (method_exists($model, 'bindNode')) {
143: $tmpRef = $model->bindNode($ref);
144: }
145: if (empty($tmpRef)) {
146: $ref = array('model' => $alias, 'foreign_key' => $ref[$name][$model->primaryKey]);
147: } else {
148: if (is_string($tmpRef)) {
149: return $this->node($tmpRef);
150: }
151: $ref = $tmpRef;
152: }
153: }
154: if (is_array($ref)) {
155: if (is_array(current($ref)) && is_string(key($ref))) {
156: $name = key($ref);
157: $ref = current($ref);
158: }
159: foreach ($ref as $key => $val) {
160: if (strpos($key, $type) !== 0 && strpos($key, '.') === false) {
161: unset($ref[$key]);
162: $ref["{$type}0.{$key}"] = $val;
163: }
164: }
165: $queryData = array(
166: 'conditions' => $ref,
167: 'fields' => array('id', 'parent_id', 'model', 'foreign_key', 'alias'),
168: 'joins' => array(array(
169: 'table' => $table,
170: 'alias' => "{$type}0",
171: 'type' => 'INNER',
172: 'conditions' => array(
173: $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft"),
174: $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght")
175: )
176: )),
177: 'order' => $db->name("{$type}.lft") . ' DESC'
178: );
179: $result = $db->read($this, $queryData, -1);
180:
181: if (!$result) {
182: throw new CakeException(__d('cake_dev', "AclNode::node() - Couldn't find %s node identified by \"%s\"", $type, print_r($ref, true)));
183: }
184: }
185: return $result;
186: }
187:
188: }
189: