AclNode.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @package Cake.Model
  13. * @since CakePHP(tm) v 0.2.9
  14. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  15. */
  16. App::uses('Model', 'Model');
  17. /**
  18. * ACL Node
  19. *
  20. * @package Cake.Model
  21. */
  22. class AclNode extends Model {
  23. /**
  24. * Explicitly disable in-memory query caching for ACL models
  25. *
  26. * @var bool
  27. */
  28. public $cacheQueries = false;
  29. /**
  30. * ACL models use the Tree behavior
  31. *
  32. * @var array
  33. */
  34. public $actsAs = array('Tree' => array('type' => 'nested'));
  35. /**
  36. * Constructor
  37. *
  38. */
  39. public function __construct() {
  40. $config = Configure::read('Acl.database');
  41. if (isset($config)) {
  42. $this->useDbConfig = $config;
  43. }
  44. parent::__construct();
  45. }
  46. /**
  47. * Retrieves the Aro/Aco node for this model
  48. *
  49. * @param string|array|Model $ref Array with 'model' and 'foreign_key', model object, or string value
  50. * @return array Node found in database
  51. * @throws CakeException when binding to a model that doesn't exist.
  52. */
  53. public function node($ref = null) {
  54. $db = $this->getDataSource();
  55. $type = $this->alias;
  56. $result = null;
  57. if (!empty($this->useTable)) {
  58. $table = $this->useTable;
  59. } else {
  60. $table = Inflector::pluralize(Inflector::underscore($type));
  61. }
  62. if (empty($ref)) {
  63. return null;
  64. } elseif (is_string($ref)) {
  65. $path = explode('/', $ref);
  66. $start = $path[0];
  67. unset($path[0]);
  68. $queryData = array(
  69. 'conditions' => array(
  70. $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft"),
  71. $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght")),
  72. 'fields' => array('id', 'parent_id', 'model', 'foreign_key', 'alias'),
  73. 'joins' => array(array(
  74. 'table' => $table,
  75. 'alias' => "{$type}0",
  76. 'type' => 'INNER',
  77. 'conditions' => array("{$type}0.alias" => $start)
  78. )),
  79. 'order' => $db->name("{$type}.lft") . ' DESC'
  80. );
  81. $conditionsAfterJoin = array();
  82. foreach ($path as $i => $alias) {
  83. $j = $i - 1;
  84. $queryData['joins'][] = array(
  85. 'table' => $table,
  86. 'alias' => "{$type}{$i}",
  87. 'type' => 'INNER',
  88. 'conditions' => array(
  89. $db->name("{$type}{$i}.alias") . ' = ' . $db->value($alias, 'string')
  90. )
  91. );
  92. // it will be better if this conditions will performs after join operation
  93. $conditionsAfterJoin[] = $db->name("{$type}{$j}.id") . ' = ' . $db->name("{$type}{$i}.parent_id");
  94. $conditionsAfterJoin[] = $db->name("{$type}{$i}.rght") . ' < ' . $db->name("{$type}{$j}.rght");
  95. $conditionsAfterJoin[] = $db->name("{$type}{$i}.lft") . ' > ' . $db->name("{$type}{$j}.lft");
  96. $queryData['conditions'] = array('or' => array(
  97. $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft") . ' AND ' . $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght"),
  98. $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}{$i}.lft") . ' AND ' . $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}{$i}.rght"))
  99. );
  100. }
  101. $queryData['conditions'] = array_merge($queryData['conditions'], $conditionsAfterJoin);
  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. }