PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/model/db_acl.php

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