PageRenderTime 38ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Model/AclNode.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 180 lines | 122 code | 17 blank | 41 comment | 23 complexity | af1ca68a30b1b9ec1b446fdf3721b768 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * PHP 5
  5. *
  6. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  7. * Copyright 2005-2011, 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-2011, 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('AppModel', 'Model');
  19. /**
  20. * ACL Node
  21. *
  22. * @package Cake.Model
  23. */
  24. class AclNode extends AppModel {
  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 mixed $ref Array with 'model' and 'foreign_key', model object, or string value
  52. * @return array Node found in database
  53. */
  54. public function node($ref = null) {
  55. $db = $this->getDataSource();
  56. $type = $this->alias;
  57. $result = null;
  58. if (!empty($this->useTable)) {
  59. $table = $this->useTable;
  60. } else {
  61. $table = Inflector::pluralize(Inflector::underscore($type));
  62. }
  63. if (empty($ref)) {
  64. return null;
  65. } elseif (is_string($ref)) {
  66. $path = explode('/', $ref);
  67. $start = $path[0];
  68. unset($path[0]);
  69. $queryData = array(
  70. 'conditions' => array(
  71. $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft"),
  72. $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght")),
  73. 'fields' => array('id', 'parent_id', 'model', 'foreign_key', 'alias'),
  74. 'joins' => array(array(
  75. 'table' => $table,
  76. 'alias' => "{$type}0",
  77. 'type' => 'LEFT',
  78. 'conditions' => array("{$type}0.alias" => $start)
  79. )),
  80. 'order' => $db->name("{$type}.lft") . ' DESC'
  81. );
  82. foreach ($path as $i => $alias) {
  83. $j = $i - 1;
  84. $queryData['joins'][] = array(
  85. 'table' => $table,
  86. 'alias' => "{$type}{$i}",
  87. 'type' => 'LEFT',
  88. 'conditions' => array(
  89. $db->name("{$type}{$i}.lft") . ' > ' . $db->name("{$type}{$j}.lft"),
  90. $db->name("{$type}{$i}.rght") . ' < ' . $db->name("{$type}{$j}.rght"),
  91. $db->name("{$type}{$i}.alias") . ' = ' . $db->value($alias, 'string'),
  92. $db->name("{$type}{$j}.id") . ' = ' . $db->name("{$type}{$i}.parent_id")
  93. )
  94. );
  95. $queryData['conditions'] = array('or' => array(
  96. $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft") . ' AND ' . $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght"),
  97. $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}{$i}.lft") . ' AND ' . $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}{$i}.rght"))
  98. );
  99. }
  100. $result = $db->read($this, $queryData, -1);
  101. $path = array_values($path);
  102. if (
  103. !isset($result[0][$type]) ||
  104. (!empty($path) && $result[0][$type]['alias'] != $path[count($path) - 1]) ||
  105. (empty($path) && $result[0][$type]['alias'] != $start)
  106. ) {
  107. return false;
  108. }
  109. } elseif (is_object($ref) && is_a($ref, 'Model')) {
  110. $ref = array('model' => $ref->alias, 'foreign_key' => $ref->id);
  111. } elseif (is_array($ref) && !(isset($ref['model']) && isset($ref['foreign_key']))) {
  112. $name = key($ref);
  113. $model = ClassRegistry::init(array('class' => $name, 'alias' => $name));
  114. if (empty($model)) {
  115. trigger_error(__d('cake_dev', "Model class '%s' not found in AclNode::node() when trying to bind %s object", $type, $this->alias), E_USER_WARNING);
  116. return null;
  117. }
  118. $tmpRef = null;
  119. if (method_exists($model, 'bindNode')) {
  120. $tmpRef = $model->bindNode($ref);
  121. }
  122. if (empty($tmpRef)) {
  123. $ref = array('model' => $name, 'foreign_key' => $ref[$name][$model->primaryKey]);
  124. } else {
  125. if (is_string($tmpRef)) {
  126. return $this->node($tmpRef);
  127. }
  128. $ref = $tmpRef;
  129. }
  130. }
  131. if (is_array($ref)) {
  132. if (is_array(current($ref)) && is_string(key($ref))) {
  133. $name = key($ref);
  134. $ref = current($ref);
  135. }
  136. foreach ($ref as $key => $val) {
  137. if (strpos($key, $type) !== 0 && strpos($key, '.') === false) {
  138. unset($ref[$key]);
  139. $ref["{$type}0.{$key}"] = $val;
  140. }
  141. }
  142. $queryData = array(
  143. 'conditions' => $ref,
  144. 'fields' => array('id', 'parent_id', 'model', 'foreign_key', 'alias'),
  145. 'joins' => array(array(
  146. 'table' => $table,
  147. 'alias' => "{$type}0",
  148. 'type' => 'LEFT',
  149. 'conditions' => array(
  150. $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft"),
  151. $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght")
  152. )
  153. )),
  154. 'order' => $db->name("{$type}.lft") . ' DESC'
  155. );
  156. $result = $db->read($this, $queryData, -1);
  157. if (!$result) {
  158. trigger_error(__d('cake_dev', "AclNode::node() - Couldn't find %s node identified by \"%s\"", $type, print_r($ref, true)), E_USER_WARNING);
  159. }
  160. }
  161. return $result;
  162. }
  163. }