PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Controller/Component/Acl/DbAcl.php

https://gitlab.com/digaotinfo/agendaLegislativa
PHP | 293 lines | 157 code | 27 blank | 109 comment | 43 complexity | 9466408523e5560be9cc3d5e2a4aa22f MD5 | raw file
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @package Cake.Controller.Component
  12. * @since CakePHP(tm) v 0.10.0.1076
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. App::uses('AclInterface', 'Controller/Component/Acl');
  16. /**
  17. * DbAcl implements an ACL control system in the database. ARO's and ACO's are
  18. * structured into trees and a linking table is used to define permissions. You
  19. * can install the schema for DbAcl with the Schema Shell.
  20. *
  21. * `$aco` and `$aro` parameters can be slash delimited paths to tree nodes.
  22. *
  23. * eg. `controllers/Users/edit`
  24. *
  25. * Would point to a tree structure like
  26. *
  27. * {{{
  28. * controllers
  29. * Users
  30. * edit
  31. * }}}
  32. *
  33. * @package Cake.Controller.Component
  34. */
  35. class DbAcl extends Object implements AclInterface {
  36. /**
  37. * Constructor
  38. *
  39. */
  40. public function __construct() {
  41. parent::__construct();
  42. App::uses('AclNode', 'Model');
  43. $this->Aro = ClassRegistry::init(array('class' => 'Aro', 'alias' => 'Aro'));
  44. $this->Aco = ClassRegistry::init(array('class' => 'Aco', 'alias' => 'Aco'));
  45. }
  46. /**
  47. * Initializes the containing component and sets the Aro/Aco objects to it.
  48. *
  49. * @param AclComponent $component
  50. * @return void
  51. */
  52. public function initialize(Component $component) {
  53. $component->Aro = $this->Aro;
  54. $component->Aco = $this->Aco;
  55. }
  56. /**
  57. * Checks if the given $aro has access to action $action in $aco
  58. *
  59. * @param string $aro ARO The requesting object identifier.
  60. * @param string $aco ACO The controlled object identifier.
  61. * @param string $action Action (defaults to *)
  62. * @return boolean Success (true if ARO has access to action in ACO, false otherwise)
  63. * @link http://book.cakephp.org/2.0/en/core-libraries/components/access-control-lists.html#checking-permissions-the-acl-component
  64. */
  65. public function check($aro, $aco, $action = "*") {
  66. if ($aro == null || $aco == null) {
  67. return false;
  68. }
  69. $permKeys = $this->_getAcoKeys($this->Aro->Permission->schema());
  70. $aroPath = $this->Aro->node($aro);
  71. $acoPath = $this->Aco->node($aco);
  72. if (empty($aroPath) || empty($acoPath)) {
  73. 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);
  74. return false;
  75. }
  76. if ($acoPath == null || $acoPath == array()) {
  77. 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);
  78. return false;
  79. }
  80. if ($action != '*' && !in_array('_' . $action, $permKeys)) {
  81. trigger_error(__d('cake_dev', "ACO permissions key %s does not exist in DbAcl::check()", $action), E_USER_NOTICE);
  82. return false;
  83. }
  84. $inherited = array();
  85. $acoIDs = Set::extract($acoPath, '{n}.' . $this->Aco->alias . '.id');
  86. $count = count($aroPath);
  87. for ($i = 0; $i < $count; $i++) {
  88. $permAlias = $this->Aro->Permission->alias;
  89. $perms = $this->Aro->Permission->find('all', array(
  90. 'conditions' => array(
  91. "{$permAlias}.aro_id" => $aroPath[$i][$this->Aro->alias]['id'],
  92. "{$permAlias}.aco_id" => $acoIDs
  93. ),
  94. 'order' => array($this->Aco->alias . '.lft' => 'desc'),
  95. 'recursive' => 0
  96. ));
  97. if (empty($perms)) {
  98. continue;
  99. } else {
  100. $perms = Set::extract($perms, '{n}.' . $this->Aro->Permission->alias);
  101. foreach ($perms as $perm) {
  102. if ($action == '*') {
  103. foreach ($permKeys as $key) {
  104. if (!empty($perm)) {
  105. if ($perm[$key] == -1) {
  106. return false;
  107. } elseif ($perm[$key] == 1) {
  108. $inherited[$key] = 1;
  109. }
  110. }
  111. }
  112. if (count($inherited) === count($permKeys)) {
  113. return true;
  114. }
  115. } else {
  116. switch ($perm['_' . $action]) {
  117. case -1:
  118. return false;
  119. case 0:
  120. continue;
  121. break;
  122. case 1:
  123. return true;
  124. break;
  125. }
  126. }
  127. }
  128. }
  129. }
  130. return false;
  131. }
  132. /**
  133. * Allow $aro to have access to action $actions in $aco
  134. *
  135. * @param string $aro ARO The requesting object identifier.
  136. * @param string $aco ACO The controlled object identifier.
  137. * @param string $actions Action (defaults to *)
  138. * @param integer $value Value to indicate access type (1 to give access, -1 to deny, 0 to inherit)
  139. * @return boolean Success
  140. * @link http://book.cakephp.org/2.0/en/core-libraries/components/access-control-lists.html#assigning-permissions
  141. */
  142. public function allow($aro, $aco, $actions = "*", $value = 1) {
  143. $perms = $this->getAclLink($aro, $aco);
  144. $permKeys = $this->_getAcoKeys($this->Aro->Permission->schema());
  145. $save = array();
  146. if ($perms == false) {
  147. trigger_error(__d('cake_dev', 'DbAcl::allow() - Invalid node'), E_USER_WARNING);
  148. return false;
  149. }
  150. if (isset($perms[0])) {
  151. $save = $perms[0][$this->Aro->Permission->alias];
  152. }
  153. if ($actions == "*") {
  154. $permKeys = $this->_getAcoKeys($this->Aro->Permission->schema());
  155. $save = array_combine($permKeys, array_pad(array(), count($permKeys), $value));
  156. } else {
  157. if (!is_array($actions)) {
  158. $actions = array('_' . $actions);
  159. }
  160. if (is_array($actions)) {
  161. foreach ($actions as $action) {
  162. if ($action{0} != '_') {
  163. $action = '_' . $action;
  164. }
  165. if (in_array($action, $permKeys)) {
  166. $save[$action] = $value;
  167. }
  168. }
  169. }
  170. }
  171. list($save['aro_id'], $save['aco_id']) = array($perms['aro'], $perms['aco']);
  172. if ($perms['link'] != null && !empty($perms['link'])) {
  173. $save['id'] = $perms['link'][0][$this->Aro->Permission->alias]['id'];
  174. } else {
  175. unset($save['id']);
  176. $this->Aro->Permission->id = null;
  177. }
  178. return ($this->Aro->Permission->save($save) !== false);
  179. }
  180. /**
  181. * Deny access for $aro to action $action in $aco
  182. *
  183. * @param string $aro ARO The requesting object identifier.
  184. * @param string $aco ACO The controlled object identifier.
  185. * @param string $action Action (defaults to *)
  186. * @return boolean Success
  187. * @link http://book.cakephp.org/2.0/en/core-libraries/components/access-control-lists.html#assigning-permissions
  188. */
  189. public function deny($aro, $aco, $action = "*") {
  190. return $this->allow($aro, $aco, $action, -1);
  191. }
  192. /**
  193. * Let access for $aro to action $action in $aco be inherited
  194. *
  195. * @param string $aro ARO The requesting object identifier.
  196. * @param string $aco ACO The controlled object identifier.
  197. * @param string $action Action (defaults to *)
  198. * @return boolean Success
  199. */
  200. public function inherit($aro, $aco, $action = "*") {
  201. return $this->allow($aro, $aco, $action, 0);
  202. }
  203. /**
  204. * Allow $aro to have access to action $actions in $aco
  205. *
  206. * @param string $aro ARO The requesting object identifier.
  207. * @param string $aco ACO The controlled object identifier.
  208. * @param string $action Action (defaults to *)
  209. * @return boolean Success
  210. * @see allow()
  211. */
  212. public function grant($aro, $aco, $action = "*") {
  213. return $this->allow($aro, $aco, $action);
  214. }
  215. /**
  216. * Deny access for $aro to action $action in $aco
  217. *
  218. * @param string $aro ARO The requesting object identifier.
  219. * @param string $aco ACO The controlled object identifier.
  220. * @param string $action Action (defaults to *)
  221. * @return boolean Success
  222. * @see deny()
  223. */
  224. public function revoke($aro, $aco, $action = "*") {
  225. return $this->deny($aro, $aco, $action);
  226. }
  227. /**
  228. * Get an array of access-control links between the given Aro and Aco
  229. *
  230. * @param string $aro ARO The requesting object identifier.
  231. * @param string $aco ACO The controlled object identifier.
  232. * @return array Indexed array with: 'aro', 'aco' and 'link'
  233. */
  234. public function getAclLink($aro, $aco) {
  235. $obj = array();
  236. $obj['Aro'] = $this->Aro->node($aro);
  237. $obj['Aco'] = $this->Aco->node($aco);
  238. if (empty($obj['Aro']) || empty($obj['Aco'])) {
  239. return false;
  240. }
  241. return array(
  242. 'aro' => Set::extract($obj, 'Aro.0.' . $this->Aro->alias . '.id'),
  243. 'aco' => Set::extract($obj, 'Aco.0.' . $this->Aco->alias . '.id'),
  244. 'link' => $this->Aro->Permission->find('all', array('conditions' => array(
  245. $this->Aro->Permission->alias . '.aro_id' => Set::extract($obj, 'Aro.0.' . $this->Aro->alias . '.id'),
  246. $this->Aro->Permission->alias . '.aco_id' => Set::extract($obj, 'Aco.0.' . $this->Aco->alias . '.id')
  247. )))
  248. );
  249. }
  250. /**
  251. * Get the keys used in an ACO
  252. *
  253. * @param array $keys Permission model info
  254. * @return array ACO keys
  255. */
  256. protected function _getAcoKeys($keys) {
  257. $newKeys = array();
  258. $keys = array_keys($keys);
  259. foreach ($keys as $key) {
  260. if (!in_array($key, array('id', 'aro_id', 'aco_id'))) {
  261. $newKeys[] = $key;
  262. }
  263. }
  264. return $newKeys;
  265. }
  266. }