/lib/Cake/Model/AclNode.php

https://github.com/gfarrell/KORDS · PHP · 182 lines · 123 code · 17 blank · 42 comment · 23 complexity · 880dcec6ba1a730f2984079c59ad8374 MD5 · raw file

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