1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
19:
20: App::uses('Model', 'Model');
21:
22: 23: 24: 25: 26:
27: class AclNode extends Model {
28:
29: 30: 31: 32: 33:
34: public $cacheQueries = false;
35:
36: 37: 38: 39: 40:
41: public $actsAs = array('Tree' => array('type' => 'nested'));
42:
43: 44: 45: 46:
47: public function __construct() {
48: $config = Configure::read('Acl.database');
49: if (isset($config)) {
50: $this->useDbConfig = $config;
51: }
52: parent::__construct();
53: }
54:
55: 56: 57: 58: 59: 60: 61:
62: public function node($ref = null) {
63: $db = $this->getDataSource();
64: $type = $this->alias;
65: $result = null;
66:
67: if (!empty($this->useTable)) {
68: $table = $this->useTable;
69: } else {
70: $table = Inflector::pluralize(Inflector::underscore($type));
71: }
72:
73: if (empty($ref)) {
74: return null;
75: } elseif (is_string($ref)) {
76: $path = explode('/', $ref);
77: $start = $path[0];
78: unset($path[0]);
79:
80: $queryData = array(
81: 'conditions' => array(
82: $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft"),
83: $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght")),
84: 'fields' => array('id', 'parent_id', 'model', 'foreign_key', 'alias'),
85: 'joins' => array(array(
86: 'table' => $table,
87: 'alias' => "{$type}0",
88: 'type' => 'INNER',
89: 'conditions' => array("{$type}0.alias" => $start)
90: )),
91: 'order' => $db->name("{$type}.lft") . ' DESC'
92: );
93:
94: foreach ($path as $i => $alias) {
95: $j = $i - 1;
96:
97: $queryData['joins'][] = array(
98: 'table' => $table,
99: 'alias' => "{$type}{$i}",
100: 'type' => 'INNER',
101: 'conditions' => array(
102: $db->name("{$type}{$i}.lft") . ' > ' . $db->name("{$type}{$j}.lft"),
103: $db->name("{$type}{$i}.rght") . ' < ' . $db->name("{$type}{$j}.rght"),
104: $db->name("{$type}{$i}.alias") . ' = ' . $db->value($alias, 'string'),
105: $db->name("{$type}{$j}.id") . ' = ' . $db->name("{$type}{$i}.parent_id")
106: )
107: );
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: $result = $db->read($this, $queryData, -1);
115: $path = array_values($path);
116:
117: if (
118: !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) && is_a($ref, '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: