Permission.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. *
  4. * PHP 5
  5. *
  6. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  7. * Copyright 2005-2012, 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-2012, 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. * Permissions linking AROs with ACOs
  21. *
  22. * @package Cake.Model
  23. */
  24. class Permission extends AppModel {
  25. /**
  26. * Model name
  27. *
  28. * @var string
  29. */
  30. public $name = 'Permission';
  31. /**
  32. * Explicitly disable in-memory query caching
  33. *
  34. * @var boolean
  35. */
  36. public $cacheQueries = false;
  37. /**
  38. * Override default table name
  39. *
  40. * @var string
  41. */
  42. public $useTable = 'aros_acos';
  43. /**
  44. * Permissions link AROs with ACOs
  45. *
  46. * @var array
  47. */
  48. public $belongsTo = array('Aro', 'Aco');
  49. /**
  50. * No behaviors for this model
  51. *
  52. * @var array
  53. */
  54. public $actsAs = null;
  55. /**
  56. * Constructor, used to tell this model to use the
  57. * database configured for ACL
  58. */
  59. public function __construct() {
  60. $config = Configure::read('Acl.database');
  61. if (!empty($config)) {
  62. $this->useDbConfig = $config;
  63. }
  64. parent::__construct();
  65. }
  66. /**
  67. * Checks if the given $aro has access to action $action in $aco
  68. *
  69. * @param string $aro ARO The requesting object identifier.
  70. * @param string $aco ACO The controlled object identifier.
  71. * @param string $action Action (defaults to *)
  72. * @return boolean Success (true if ARO has access to action in ACO, false otherwise)
  73. */
  74. public function check($aro, $aco, $action = "*") {
  75. if (!$aro || !$aco) {
  76. return false;
  77. }
  78. $permKeys = $this->getAcoKeys($this->schema());
  79. $aroPath = $this->Aro->node($aro);
  80. $acoPath = $this->Aco->node($aco);
  81. if (!$aroPath || !$acoPath) {
  82. trigger_error(__d('cake_dev', "DbAcl::check() - Failed ARO/ACO node lookup in permissions check. Node references:\nAro: ") . print_r($aro, true) . "\nAco: " . print_r($aco, true), E_USER_WARNING);
  83. return false;
  84. }
  85. if (!$acoPath) {
  86. trigger_error(__d('cake_dev', "DbAcl::check() - Failed ACO node lookup in permissions check. Node references:\nAro: ") . print_r($aro, true) . "\nAco: " . print_r($aco, true), E_USER_WARNING);
  87. return false;
  88. }
  89. if ($action != '*' && !in_array('_' . $action, $permKeys)) {
  90. trigger_error(__d('cake_dev', "ACO permissions key %s does not exist in DbAcl::check()", $action), E_USER_NOTICE);
  91. return false;
  92. }
  93. $inherited = array();
  94. $acoIDs = Hash::extract($acoPath, '{n}.' . $this->Aco->alias . '.id');
  95. $count = count($aroPath);
  96. for ($i = 0; $i < $count; $i++) {
  97. $permAlias = $this->alias;
  98. $perms = $this->find('all', array(
  99. 'conditions' => array(
  100. "{$permAlias}.aro_id" => $aroPath[$i][$this->Aro->alias]['id'],
  101. "{$permAlias}.aco_id" => $acoIDs
  102. ),
  103. 'order' => array($this->Aco->alias . '.lft' => 'desc'),
  104. 'recursive' => 0
  105. ));
  106. if (empty($perms)) {
  107. continue;
  108. } else {
  109. $perms = Hash::extract($perms, '{n}.' . $this->alias);
  110. foreach ($perms as $perm) {
  111. if ($action == '*') {
  112. foreach ($permKeys as $key) {
  113. if (!empty($perm)) {
  114. if ($perm[$key] == -1) {
  115. return false;
  116. } elseif ($perm[$key] == 1) {
  117. $inherited[$key] = 1;
  118. }
  119. }
  120. }
  121. if (count($inherited) === count($permKeys)) {
  122. return true;
  123. }
  124. } else {
  125. switch ($perm['_' . $action]) {
  126. case -1:
  127. return false;
  128. case 0:
  129. continue;
  130. case 1:
  131. return true;
  132. }
  133. }
  134. }
  135. }
  136. }
  137. return false;
  138. }
  139. /**
  140. * Allow $aro to have access to action $actions in $aco
  141. *
  142. * @param string $aro ARO The requesting object identifier.
  143. * @param string $aco ACO The controlled object identifier.
  144. * @param string $actions Action (defaults to *)
  145. * @param integer $value Value to indicate access type (1 to give access, -1 to deny, 0 to inherit)
  146. * @return boolean Success
  147. */
  148. public function allow($aro, $aco, $actions = "*", $value = 1) {
  149. $perms = $this->getAclLink($aro, $aco);
  150. $permKeys = $this->getAcoKeys($this->schema());
  151. $save = array();
  152. if (!$perms) {
  153. trigger_error(__d('cake_dev', 'DbAcl::allow() - Invalid node'), E_USER_WARNING);
  154. return false;
  155. }
  156. if (isset($perms[0])) {
  157. $save = $perms[0][$this->alias];
  158. }
  159. if ($actions === "*") {
  160. $save = array_combine($permKeys, array_pad(array(), count($permKeys), $value));
  161. } else {
  162. if (!is_array($actions)) {
  163. $actions = array('_' . $actions);
  164. }
  165. if (is_array($actions)) {
  166. foreach ($actions as $action) {
  167. if ($action{0} !== '_') {
  168. $action = '_' . $action;
  169. }
  170. if (in_array($action, $permKeys)) {
  171. $save[$action] = $value;
  172. }
  173. }
  174. }
  175. }
  176. list($save['aro_id'], $save['aco_id']) = array($perms['aro'], $perms['aco']);
  177. if ($perms['link'] && !empty($perms['link'])) {
  178. $save['id'] = $perms['link'][0][$this->alias]['id'];
  179. } else {
  180. unset($save['id']);
  181. $this->id = null;
  182. }
  183. return ($this->save($save) !== false);
  184. }
  185. /**
  186. * Get an array of access-control links between the given Aro and Aco
  187. *
  188. * @param string $aro ARO The requesting object identifier.
  189. * @param string $aco ACO The controlled object identifier.
  190. * @return array Indexed array with: 'aro', 'aco' and 'link'
  191. */
  192. public function getAclLink($aro, $aco) {
  193. $obj = array();
  194. $obj['Aro'] = $this->Aro->node($aro);
  195. $obj['Aco'] = $this->Aco->node($aco);
  196. if (empty($obj['Aro']) || empty($obj['Aco'])) {
  197. return false;
  198. }
  199. $aro = Hash::extract($obj, 'Aro.0.' . $this->Aro->alias . '.id');
  200. $aco = Hash::extract($obj, 'Aco.0.' . $this->Aco->alias . '.id');
  201. $aro = current($aro);
  202. $aco = current($aco);
  203. return array(
  204. 'aro' => $aro,
  205. 'aco' => $aco,
  206. 'link' => $this->find('all', array('conditions' => array(
  207. $this->alias . '.aro_id' => $aro,
  208. $this->alias . '.aco_id' => $aco
  209. )))
  210. );
  211. }
  212. /**
  213. * Get the crud type keys
  214. *
  215. * @param array $keys Permission schema
  216. * @return array permission keys
  217. */
  218. public function getAcoKeys($keys) {
  219. $newKeys = array();
  220. $keys = array_keys($keys);
  221. foreach ($keys as $key) {
  222. if (!in_array($key, array('id', 'aro_id', 'aco_id'))) {
  223. $newKeys[] = $key;
  224. }
  225. }
  226. return $newKeys;
  227. }
  228. }