PageRenderTime 39ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/cake/libs/model/db_acl.php

https://github.com/bb-dev/cakephp2x
PHP | 328 lines | 258 code | 17 blank | 53 comment | 23 complexity | 85aee4bd53261fdcdf8e8f811a9c673e MD5 | raw file
  1. <?php
  2. /**
  3. * This is core configuration file.
  4. *
  5. * Use it to configure core behaviour ofCake.
  6. *
  7. * PHP Version 5.x
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package cake
  18. * @subpackage cake.cake.libs.model
  19. * @since CakePHP(tm) v 0.2.9
  20. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  21. */
  22. /**
  23. * Load Model and AppModel
  24. */
  25. App::import('Model', 'App');
  26. /**
  27. * Short description for file.
  28. *
  29. *
  30. * @package cake
  31. * @subpackage cake.cake.libs.model
  32. */
  33. class AclNode extends AppModel {
  34. /**
  35. * Explicitly disable in-memory query caching for ACL models
  36. *
  37. * @var boolean
  38. * @access public
  39. */
  40. public $cacheQueries = false;
  41. /**
  42. * ACL models use the Tree behavior
  43. *
  44. * @var array
  45. * @access public
  46. */
  47. public $actsAs = array('Tree' => 'nested');
  48. /**
  49. * Constructor
  50. *
  51. */
  52. public function __construct() {
  53. $config = Configure::read('Acl.database');
  54. if (isset($config)) {
  55. $this->useDbConfig = $config;
  56. }
  57. parent::__construct();
  58. }
  59. /**
  60. * Retrieves the Aro/Aco node for this model
  61. *
  62. * @param mixed $ref Array with 'model' and 'foreign_key', model object, or string value
  63. * @return array Node found in database
  64. * @access public
  65. */
  66. public function node($ref = null) {
  67. $db = ConnectionManager::getDataSource($this->useDbConfig);
  68. $type = $this->alias;
  69. $result = null;
  70. if (!empty($this->useTable)) {
  71. $table = $this->useTable;
  72. } else {
  73. $table = Inflector::pluralize(Inflector::underscore($type));
  74. }
  75. if (empty($ref)) {
  76. return null;
  77. } elseif (is_string($ref)) {
  78. $path = explode('/', $ref);
  79. $start = $path[0];
  80. unset($path[0]);
  81. $queryData = array(
  82. 'conditions' => array(
  83. $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft"),
  84. $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght")),
  85. 'fields' => array('id', 'parent_id', 'model', 'foreign_key', 'alias'),
  86. 'joins' => array(array(
  87. 'table' => $db->fullTableName($this),
  88. 'alias' => "{$type}0",
  89. 'type' => 'LEFT',
  90. 'conditions' => array("{$type}0.alias" => $start)
  91. )),
  92. 'order' => $db->name("{$type}.lft") . ' DESC'
  93. );
  94. foreach ($path as $i => $alias) {
  95. $j = $i - 1;
  96. $queryData['joins'][] = array(
  97. 'table' => $db->fullTableName($this),
  98. 'alias' => "{$type}{$i}",
  99. 'type' => 'LEFT',
  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. )
  105. );
  106. $queryData['conditions'] = array('or' => array(
  107. $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft") . ' AND ' . $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght"),
  108. $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}{$i}.lft") . ' AND ' . $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}{$i}.rght"))
  109. );
  110. }
  111. $result = $db->read($this, $queryData, -1);
  112. $path = array_values($path);
  113. if (
  114. !isset($result[0][$type]) ||
  115. (!empty($path) && $result[0][$type]['alias'] != $path[count($path) - 1]) ||
  116. (empty($path) && $result[0][$type]['alias'] != $start)
  117. ) {
  118. return false;
  119. }
  120. } elseif (is_object($ref) && is_a($ref, 'Model')) {
  121. $ref = array('model' => $ref->alias, 'foreign_key' => $ref->id);
  122. } elseif (is_array($ref) && !(isset($ref['model']) && isset($ref['foreign_key']))) {
  123. $name = key($ref);
  124. $model = ClassRegistry::init(array('class' => $name, 'alias' => $name));
  125. if (empty($model)) {
  126. trigger_error("Model class '$name' not found in AclNode::node() when trying to bind {$this->alias} object", E_USER_WARNING);
  127. return null;
  128. }
  129. $tmpRef = null;
  130. if (method_exists($model, 'bindNode')) {
  131. $tmpRef = $model->bindNode($ref);
  132. }
  133. if (empty($tmpRef)) {
  134. $ref = array('model' => $name, 'foreign_key' => $ref[$name][$model->primaryKey]);
  135. } else {
  136. if (is_string($tmpRef)) {
  137. return $this->node($tmpRef);
  138. }
  139. $ref = $tmpRef;
  140. }
  141. }
  142. if (is_array($ref)) {
  143. if (is_array(current($ref)) && is_string(key($ref))) {
  144. $name = key($ref);
  145. $ref = current($ref);
  146. }
  147. foreach ($ref as $key => $val) {
  148. if (strpos($key, $type) !== 0 && strpos($key, '.') === false) {
  149. unset($ref[$key]);
  150. $ref["{$type}0.{$key}"] = $val;
  151. }
  152. }
  153. $queryData = array(
  154. 'conditions' => $ref,
  155. 'fields' => array('id', 'parent_id', 'model', 'foreign_key', 'alias'),
  156. 'joins' => array(array(
  157. 'table' => $db->fullTableName($this),
  158. 'alias' => "{$type}0",
  159. 'type' => 'LEFT',
  160. 'conditions' => array(
  161. $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft"),
  162. $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght")
  163. )
  164. )),
  165. 'order' => $db->name("{$type}.lft") . ' DESC'
  166. );
  167. $result = $db->read($this, $queryData, -1);
  168. if (!$result) {
  169. trigger_error("AclNode::node() - Couldn't find {$type} node identified by \"" . print_r($ref, true) . "\"", E_USER_WARNING);
  170. }
  171. }
  172. return $result;
  173. }
  174. }
  175. /**
  176. * Access Control Object
  177. *
  178. * @package cake
  179. * @subpackage cake.cake.libs.model
  180. */
  181. class Aco extends AclNode {
  182. /**
  183. * Model name
  184. *
  185. * @var string
  186. * @access public
  187. */
  188. public $name = 'Aco';
  189. /**
  190. * Binds to ARO nodes through permissions settings
  191. *
  192. * @var array
  193. * @access public
  194. */
  195. public $hasAndBelongsToMany = array('Aro' => array('with' => 'Permission'));
  196. }
  197. /**
  198. * Action for Access Control Object
  199. *
  200. * @package cake
  201. * @subpackage cake.cake.libs.model
  202. */
  203. class AcoAction extends AppModel {
  204. /**
  205. * Model name
  206. *
  207. * @var string
  208. * @access public
  209. */
  210. public $name = 'AcoAction';
  211. /**
  212. * ACO Actions belong to ACOs
  213. *
  214. * @var array
  215. * @access public
  216. */
  217. public $belongsTo = array('Aco');
  218. }
  219. /**
  220. * Access Request Object
  221. *
  222. * @package cake
  223. * @subpackage cake.cake.libs.model
  224. */
  225. class Aro extends AclNode {
  226. /**
  227. * Model name
  228. *
  229. * @var string
  230. * @access public
  231. */
  232. public $name = 'Aro';
  233. /**
  234. * AROs are linked to ACOs by means of Permission
  235. *
  236. * @var array
  237. * @access public
  238. */
  239. public $hasAndBelongsToMany = array('Aco' => array('with' => 'Permission'));
  240. }
  241. /**
  242. * Permissions linking AROs with ACOs
  243. *
  244. * @package cake
  245. * @subpackage cake.cake.libs.model
  246. */
  247. class Permission extends AppModel {
  248. /**
  249. * Model name
  250. *
  251. * @var string
  252. * @access public
  253. */
  254. public $name = 'Permission';
  255. /**
  256. * Explicitly disable in-memory query caching
  257. *
  258. * @var boolean
  259. * @access public
  260. */
  261. public $cacheQueries = false;
  262. /**
  263. * Override default table name
  264. *
  265. * @var string
  266. * @access public
  267. */
  268. public $useTable = 'aros_acos';
  269. /**
  270. * Permissions link AROs with ACOs
  271. *
  272. * @var array
  273. * @access public
  274. */
  275. public $belongsTo = array('Aro', 'Aco');
  276. /**
  277. * No behaviors for this model
  278. *
  279. * @var array
  280. * @access public
  281. */
  282. public $actsAs = null;
  283. /**
  284. * Constructor, used to tell this model to use the
  285. * database configured for ACL
  286. */
  287. public function __construct() {
  288. $config = Configure::read('Acl.database');
  289. if (!empty($config)) {
  290. $this->useDbConfig = $config;
  291. }
  292. parent::__construct();
  293. }
  294. }
  295. ?>