1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17:
18:
19: App::uses('Model', 'Model');
20:
21: 22: 23: 24: 25:
26: class AclNode extends Model {
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:
61: public function node($ref = null) {
62: $db = $this->getDataSource();
63: $type = $this->alias;
64: $result = null;
65:
66: if (!empty($this->useTable)) {
67: $table = $this->useTable;
68: } else {
69: $table = Inflector::pluralize(Inflector::underscore($type));
70: }
71:
72: if (empty($ref)) {
73: return null;
74: } elseif (is_string($ref)) {
75: $path = explode('/', $ref);
76: $start = $path[0];
77: unset($path[0]);
78:
79: $queryData = array(
80: 'conditions' => array(
81: $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft"),
82: $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght")),
83: 'fields' => array('id', 'parent_id', 'model', 'foreign_key', 'alias'),
84: 'joins' => array(array(
85: 'table' => $table,
86: 'alias' => "{$type}0",
87: 'type' => 'INNER',
88: 'conditions' => array("{$type}0.alias" => $start)
89: )),
90: 'order' => $db->name("{$type}.lft") . ' DESC'
91: );
92:
93: foreach ($path as $i => $alias) {
94: $j = $i - 1;
95:
96: $queryData['joins'][] = array(
97: 'table' => $table,
98: 'alias' => "{$type}{$i}",
99: 'type' => 'INNER',
100: 'conditions' => array(
101: $db->name("{$type}{$i}.lft") . ' > ' . $db->name("{$type}{$j}.lft"),
102: $db->name("{$type}{$i}.rght") . ' < ' . $db->name("{$type}{$j}.rght"),
103: $db->name("{$type}{$i}.alias") . ' = ' . $db->value($alias, 'string'),
104: $db->name("{$type}{$j}.id") . ' = ' . $db->name("{$type}{$i}.parent_id")
105: )
106: );
107:
108: $queryData['conditions'] = array('or' => array(
109: $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft") . ' AND ' . $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght"),
110: $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}{$i}.lft") . ' AND ' . $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}{$i}.rght"))
111: );
112: }
113: $result = $db->read($this, $queryData, -1);
114: $path = array_values($path);
115:
116: if (
117: !isset($result[0][$type]) ||
118: (!empty($path) && $result[0][$type]['alias'] != $path[count($path) - 1]) ||
119: (empty($path) && $result[0][$type]['alias'] != $start)
120: ) {
121: return false;
122: }
123: } elseif (is_object($ref) && $ref instanceof Model) {
124: $ref = array('model' => $ref->name, 'foreign_key' => $ref->id);
125: } elseif (is_array($ref) && !(isset($ref['model']) && isset($ref['foreign_key']))) {
126: $name = key($ref);
127: list(, $alias) = pluginSplit($name);
128:
129: $model = ClassRegistry::init(array('class' => $name, 'alias' => $alias));
130:
131: if (empty($model)) {
132: throw new CakeException('cake_dev', "Model class '%s' not found in AclNode::node() when trying to bind %s object", $type, $this->alias);
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' => 'INNER',
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: throw new CakeException(__d('cake_dev', "AclNode::node() - Couldn't find %s node identified by \"%s\"", $type, print_r($ref, true)));
177: }
178: }
179: return $result;
180: }
181:
182: }
183: