AclNode.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. *
  4. * PHP 5
  5. *
  6. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  7. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  8. *
  9. * Licensed under The MIT License
  10. * For full copyright and license information, please see the LICENSE.txt
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.Model
  16. * @since CakePHP(tm) v 0.2.9
  17. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  18. */
  19. App::uses('Model', 'Model');
  20. /**
  21. * ACL Node
  22. *
  23. * @package Cake.Model
  24. */
  25. class AclNode extends Model {
  26. /**
  27. * Explicitly disable in-memory query caching for ACL models
  28. *
  29. * @var boolean
  30. */
  31. public $cacheQueries = false;
  32. /**
  33. * ACL models use the Tree behavior
  34. *
  35. * @var array
  36. */
  37. public $actsAs = array('Tree' => array('type' => 'nested'));
  38. /**
  39. * Constructor
  40. *
  41. */
  42. public function __construct() {
  43. $config = Configure::read('Acl.database');
  44. if (isset($config)) {
  45. $this->useDbConfig = $config;
  46. }
  47. parent::__construct();
  48. }
  49. /**
  50. * Retrieves the Aro/Aco node for this model
  51. *
  52. * @param string|array|Model $ref Array with 'model' and 'foreign_key', model object, or string value
  53. * @return array Node found in database
  54. * @throws CakeException when binding to a model that doesn't exist.
  55. */
  56. public function node($ref = null) {
  57. $db = $this->getDataSource();
  58. $type = $this->alias;
  59. $result = null;
  60. if (!empty($this->useTable)) {
  61. $table = $this->useTable;
  62. } else {
  63. $table = Inflector::pluralize(Inflector::underscore($type));
  64. }
  65. if (empty($ref)) {
  66. return null;
  67. } elseif (is_string($ref)) {
  68. $path = explode('/', $ref);
  69. $start = $path[0];
  70. unset($path[0]);
  71. $queryData = array(
  72. 'conditions' => array(
  73. $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft"),
  74. $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght")),
  75. 'fields' => array('id', 'parent_id', 'model', 'foreign_key', 'alias'),
  76. 'joins' => array(array(
  77. 'table' => $table,
  78. 'alias' => "{$type}0",
  79. 'type' => 'INNER',
  80. 'conditions' => array("{$type}0.alias" => $start)
  81. )),
  82. 'order' => $db->name("{$type}.lft") . ' DESC'
  83. );
  84. foreach ($path as $i => $alias) {
  85. $j = $i - 1;
  86. $queryData['joins'][] = array(
  87. 'table' => $table,
  88. 'alias' => "{$type}{$i}",
  89. 'type' => 'INNER',
  90. 'conditions' => array(
  91. $db->name("{$type}{$i}.lft") . ' > ' . $db->name("{$type}{$j}.lft"),
  92. $db->name("{$type}{$i}.rght") . ' < ' . $db->name("{$type}{$j}.rght"),
  93. $db->name("{$type}{$i}.alias") . ' = ' . $db->value($alias, 'string'),
  94. $db->name("{$type}{$j}.id") . ' = ' . $db->name("{$type}{$i}.parent_id")
  95. )
  96. );
  97. $queryData['conditions'] = array('or' => array(
  98. $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft") . ' AND ' . $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght"),
  99. $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}{$i}.lft") . ' AND ' . $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}{$i}.rght"))
  100. );
  101. }
  102. $result = $db->read($this, $queryData, -1);
  103. $path = array_values($path);
  104. if (
  105. !isset($result[0][$type]) ||
  106. (!empty($path) && $result[0][$type]['alias'] != $path[count($path) - 1]) ||
  107. (empty($path) && $result[0][$type]['alias'] != $start)
  108. ) {
  109. return false;
  110. }
  111. } elseif (is_object($ref) && $ref instanceof Model) {
  112. $ref = array('model' => $ref->name, 'foreign_key' => $ref->id);
  113. } elseif (is_array($ref) && !(isset($ref['model']) && isset($ref['foreign_key']))) {
  114. $name = key($ref);
  115. list(, $alias) = pluginSplit($name);
  116. $model = ClassRegistry::init(array('class' => $name, 'alias' => $alias));
  117. if (empty($model)) {
  118. throw new CakeException('cake_dev', "Model class '%s' not found in AclNode::node() when trying to bind %s object", $type, $this->alias);
  119. }
  120. $tmpRef = null;
  121. if (method_exists($model, 'bindNode')) {
  122. $tmpRef = $model->bindNode($ref);
  123. }
  124. if (empty($tmpRef)) {
  125. $ref = array('model' => $alias, 'foreign_key' => $ref[$name][$model->primaryKey]);
  126. } else {
  127. if (is_string($tmpRef)) {
  128. return $this->node($tmpRef);
  129. }
  130. $ref = $tmpRef;
  131. }
  132. }
  133. if (is_array($ref)) {
  134. if (is_array(current($ref)) && is_string(key($ref))) {
  135. $name = key($ref);
  136. $ref = current($ref);
  137. }
  138. foreach ($ref as $key => $val) {
  139. if (strpos($key, $type) !== 0 && strpos($key, '.') === false) {
  140. unset($ref[$key]);
  141. $ref["{$type}0.{$key}"] = $val;
  142. }
  143. }
  144. $queryData = array(
  145. 'conditions' => $ref,
  146. 'fields' => array('id', 'parent_id', 'model', 'foreign_key', 'alias'),
  147. 'joins' => array(array(
  148. 'table' => $table,
  149. 'alias' => "{$type}0",
  150. 'type' => 'INNER',
  151. 'conditions' => array(
  152. $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft"),
  153. $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght")
  154. )
  155. )),
  156. 'order' => $db->name("{$type}.lft") . ' DESC'
  157. );
  158. $result = $db->read($this, $queryData, -1);
  159. if (!$result) {
  160. throw new CakeException(__d('cake_dev', "AclNode::node() - Couldn't find %s node identified by \"%s\"", $type, print_r($ref, true)));
  161. }
  162. }
  163. return $result;
  164. }
  165. }