PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/model/db_acl.php

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