PageRenderTime 27ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/model/db_acl.php

http://github.com/Datawalke/Coordino
PHP | 332 lines | 262 code | 17 blank | 53 comment | 25 complexity | db5abf456c3be3fed34213b29146203e MD5 | raw file
  1. <?php
  2. /**
  3. * This is core configuration file.
  4. *
  5. * Use it to configure core behaviour ofCake.
  6. *
  7. * PHP versions 4 and 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2012, 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-2012, 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. * ACL Node
  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. var $cacheQueries = false;
  41. /**
  42. * ACL models use the Tree behavior
  43. *
  44. * @var array
  45. * @access public
  46. */
  47. var $actsAs = array('Tree' => 'nested');
  48. /**
  49. * Constructor
  50. *
  51. */
  52. 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. 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' => $table,
  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' => $table,
  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. $db->name("{$type}{$j}.id") . ' = ' . $db->name("{$type}{$i}.parent_id")
  105. )
  106. );
  107. $queryData['conditions'] = array('or' => array(
  108. $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft") . ' AND ' . $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght"),
  109. $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}{$i}.lft") . ' AND ' . $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}{$i}.rght"))
  110. );
  111. }
  112. $result = $db->read($this, $queryData, -1);
  113. $path = array_values($path);
  114. if (
  115. !isset($result[0][$type]) ||
  116. (!empty($path) && $result[0][$type]['alias'] != $path[count($path) - 1]) ||
  117. (empty($path) && $result[0][$type]['alias'] != $start)
  118. ) {
  119. return false;
  120. }
  121. } elseif (is_object($ref) && is_a($ref, 'Model')) {
  122. $ref = array('model' => $ref->alias, 'foreign_key' => $ref->id);
  123. } elseif (is_array($ref) && !(isset($ref['model']) && isset($ref['foreign_key']))) {
  124. $name = key($ref);
  125. if (PHP5) {
  126. $model = ClassRegistry::init(array('class' => $name, 'alias' => $name));
  127. } else {
  128. $model =& ClassRegistry::init(array('class' => $name, 'alias' => $name));
  129. }
  130. if (empty($model)) {
  131. trigger_error(sprintf(__("Model class '%s' not found in AclNode::node() when trying to bind %s object", true), $type, $this->alias), E_USER_WARNING);
  132. return null;
  133. }
  134. $tmpRef = null;
  135. if (method_exists($model, 'bindNode')) {
  136. $tmpRef = $model->bindNode($ref);
  137. }
  138. if (empty($tmpRef)) {
  139. $ref = array('model' => $name, 'foreign_key' => $ref[$name][$model->primaryKey]);
  140. } else {
  141. if (is_string($tmpRef)) {
  142. return $this->node($tmpRef);
  143. }
  144. $ref = $tmpRef;
  145. }
  146. }
  147. if (is_array($ref)) {
  148. if (is_array(current($ref)) && is_string(key($ref))) {
  149. $name = key($ref);
  150. $ref = current($ref);
  151. }
  152. foreach ($ref as $key => $val) {
  153. if (strpos($key, $type) !== 0 && strpos($key, '.') === false) {
  154. unset($ref[$key]);
  155. $ref["{$type}0.{$key}"] = $val;
  156. }
  157. }
  158. $queryData = array(
  159. 'conditions' => $ref,
  160. 'fields' => array('id', 'parent_id', 'model', 'foreign_key', 'alias'),
  161. 'joins' => array(array(
  162. 'table' => $table,
  163. 'alias' => "{$type}0",
  164. 'type' => 'LEFT',
  165. 'conditions' => array(
  166. $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft"),
  167. $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght")
  168. )
  169. )),
  170. 'order' => $db->name("{$type}.lft") . ' DESC'
  171. );
  172. $result = $db->read($this, $queryData, -1);
  173. if (!$result) {
  174. trigger_error(sprintf(__("AclNode::node() - Couldn't find %s node identified by \"%s\"", true), $type, print_r($ref, true)), E_USER_WARNING);
  175. }
  176. }
  177. return $result;
  178. }
  179. }
  180. /**
  181. * Access Control Object
  182. *
  183. * @package cake
  184. * @subpackage cake.cake.libs.model
  185. */
  186. class Aco extends AclNode {
  187. /**
  188. * Model name
  189. *
  190. * @var string
  191. * @access public
  192. */
  193. var $name = 'Aco';
  194. /**
  195. * Binds to ARO nodes through permissions settings
  196. *
  197. * @var array
  198. * @access public
  199. */
  200. var $hasAndBelongsToMany = array('Aro' => array('with' => 'Permission'));
  201. }
  202. /**
  203. * Action for Access Control Object
  204. *
  205. * @package cake
  206. * @subpackage cake.cake.libs.model
  207. */
  208. class AcoAction extends AppModel {
  209. /**
  210. * Model name
  211. *
  212. * @var string
  213. * @access public
  214. */
  215. var $name = 'AcoAction';
  216. /**
  217. * ACO Actions belong to ACOs
  218. *
  219. * @var array
  220. * @access public
  221. */
  222. var $belongsTo = array('Aco');
  223. }
  224. /**
  225. * Access Request Object
  226. *
  227. * @package cake
  228. * @subpackage cake.cake.libs.model
  229. */
  230. class Aro extends AclNode {
  231. /**
  232. * Model name
  233. *
  234. * @var string
  235. * @access public
  236. */
  237. var $name = 'Aro';
  238. /**
  239. * AROs are linked to ACOs by means of Permission
  240. *
  241. * @var array
  242. * @access public
  243. */
  244. var $hasAndBelongsToMany = array('Aco' => array('with' => 'Permission'));
  245. }
  246. /**
  247. * Permissions linking AROs with ACOs
  248. *
  249. * @package cake
  250. * @subpackage cake.cake.libs.model
  251. */
  252. class Permission extends AppModel {
  253. /**
  254. * Model name
  255. *
  256. * @var string
  257. * @access public
  258. */
  259. var $name = 'Permission';
  260. /**
  261. * Explicitly disable in-memory query caching
  262. *
  263. * @var boolean
  264. * @access public
  265. */
  266. var $cacheQueries = false;
  267. /**
  268. * Override default table name
  269. *
  270. * @var string
  271. * @access public
  272. */
  273. var $useTable = 'aros_acos';
  274. /**
  275. * Permissions link AROs with ACOs
  276. *
  277. * @var array
  278. * @access public
  279. */
  280. var $belongsTo = array('Aro', 'Aco');
  281. /**
  282. * No behaviors for this model
  283. *
  284. * @var array
  285. * @access public
  286. */
  287. var $actsAs = null;
  288. /**
  289. * Constructor, used to tell this model to use the
  290. * database configured for ACL
  291. */
  292. function __construct() {
  293. $config = Configure::read('Acl.database');
  294. if (!empty($config)) {
  295. $this->useDbConfig = $config;
  296. }
  297. parent::__construct();
  298. }
  299. }