Permission.php 6.5 KB

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