PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/controller/components/acl.php

https://github.com/msadouni/cakephp2x
PHP | 613 lines | 290 code | 64 blank | 259 comment | 79 complexity | 0b6190aa5c0f283a66f080b16c9cf12c MD5 | raw file
  1. <?php
  2. /**
  3. * Access Control List factory class.
  4. *
  5. * Permissions system.
  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.controller.components
  19. * @since CakePHP(tm) v 0.10.0.1076
  20. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  21. */
  22. /**
  23. * Access Control List factory class.
  24. *
  25. * Looks for ACL implementation class in core config, and returns an instance of that class.
  26. *
  27. * @package cake
  28. * @subpackage cake.cake.libs.controller.components
  29. */
  30. class AclComponent extends Object {
  31. /**
  32. * Instance of an ACL class
  33. *
  34. * @var object
  35. * @access protected
  36. */
  37. protected $_Instance = null;
  38. /**
  39. * Constructor. Will return an instance of the correct ACL class.
  40. *
  41. */
  42. public function __construct() {
  43. $name = Inflector::camelize(strtolower(Configure::read('Acl.classname')));
  44. if (!class_exists($name)) {
  45. if (App::import('Component', $name)) {
  46. list($plugin, $name) = pluginSplit($name);
  47. $name .= 'Component';
  48. } else {
  49. trigger_error(sprintf(__('Could not find %s.', true), $name), E_USER_WARNING);
  50. }
  51. }
  52. $this->_Instance = new $name();
  53. $this->_Instance->initialize($this);
  54. }
  55. /**
  56. * Startup is not used
  57. *
  58. * @param object $controller Controller using this component
  59. * @return boolean Proceed with component usage (true), or fail (false)
  60. * @access public
  61. */
  62. public function startup(&$controller) {
  63. return true;
  64. }
  65. /**
  66. * Empty class defintion, to be overridden in subclasses.
  67. *
  68. * @access protected
  69. */
  70. protected function _initACL() {
  71. }
  72. /**
  73. * Pass-thru function for ACL check instance.
  74. *
  75. * @param string $aro ARO
  76. * @param string $aco ACO
  77. * @param string $action Action (defaults to *)
  78. * @return boolean Success
  79. * @access public
  80. */
  81. public function check($aro, $aco, $action = "*") {
  82. return $this->_Instance->check($aro, $aco, $action);
  83. }
  84. /**
  85. * Pass-thru function for ACL allow instance.
  86. *
  87. * @param string $aro ARO
  88. * @param string $aco ACO
  89. * @param string $action Action (defaults to *)
  90. * @return boolean Success
  91. * @access public
  92. */
  93. public function allow($aro, $aco, $action = "*") {
  94. return $this->_Instance->allow($aro, $aco, $action);
  95. }
  96. /**
  97. * Pass-thru function for ACL deny instance.
  98. *
  99. * @param string $aro ARO
  100. * @param string $aco ACO
  101. * @param string $action Action (defaults to *)
  102. * @return boolean Success
  103. * @access public
  104. */
  105. public function deny($aro, $aco, $action = "*") {
  106. return $this->_Instance->deny($aro, $aco, $action);
  107. }
  108. /**
  109. * Pass-thru function for ACL inherit instance.
  110. *
  111. * @param string $aro ARO
  112. * @param string $aco ACO
  113. * @param string $action Action (defaults to *)
  114. * @return boolean Success
  115. * @access public
  116. */
  117. public function inherit($aro, $aco, $action = "*") {
  118. return $this->_Instance->inherit($aro, $aco, $action);
  119. }
  120. /**
  121. * Pass-thru function for ACL grant instance.
  122. *
  123. * @param string $aro ARO
  124. * @param string $aco ACO
  125. * @param string $action Action (defaults to *)
  126. * @return boolean Success
  127. * @access public
  128. */
  129. public function grant($aro, $aco, $action = "*") {
  130. return $this->_Instance->grant($aro, $aco, $action);
  131. }
  132. /**
  133. * Pass-thru function for ACL grant instance.
  134. *
  135. * @param string $aro ARO
  136. * @param string $aco ACO
  137. * @param string $action Action (defaults to *)
  138. * @return boolean Success
  139. * @access public
  140. */
  141. public function revoke($aro, $aco, $action = "*") {
  142. return $this->_Instance->revoke($aro, $aco, $action);
  143. }
  144. }
  145. /**
  146. * Access Control List abstract class. Not to be instantiated.
  147. * Subclasses of this class are used by AclComponent to perform ACL checks in Cake.
  148. *
  149. * @package cake
  150. * @subpackage cake.cake.libs.controller.components
  151. * @abstract
  152. */
  153. class AclBase extends Object {
  154. /**
  155. * This class should never be instantiated, just subclassed.
  156. *
  157. */
  158. public function __construct() {
  159. if (strcasecmp(get_class($this), "AclBase") == 0 || !is_subclass_of($this, "AclBase")) {
  160. trigger_error(__("[acl_base] The AclBase class constructor has been called, or the class was instantiated. This class must remain abstract. Please refer to the Cake docs for ACL configuration.", true), E_USER_ERROR);
  161. return NULL;
  162. }
  163. }
  164. /**
  165. * Empty method to be overridden in subclasses
  166. *
  167. * @param string $aro ARO
  168. * @param string $aco ACO
  169. * @param string $action Action (defaults to *)
  170. * @access public
  171. */
  172. public function check($aro, $aco, $action = "*") {
  173. }
  174. /**
  175. * Empty method to be overridden in subclasses
  176. *
  177. * @param object $component Component
  178. * @access public
  179. */
  180. public function initialize(&$component) {
  181. }
  182. }
  183. /**
  184. * In this file you can extend the AclBase.
  185. *
  186. * @package cake
  187. * @subpackage cake.cake.libs.model
  188. */
  189. class DbAcl extends AclBase {
  190. /**
  191. * Constructor
  192. *
  193. */
  194. public function __construct() {
  195. parent::__construct();
  196. if (!class_exists('AclNode')) {
  197. require LIBS . 'model' . DS . 'db_acl.php';
  198. }
  199. $this->Aro = ClassRegistry::init(array('class' => 'Aro', 'alias' => 'Aro'));
  200. $this->Aco = ClassRegistry::init(array('class' => 'Aco', 'alias' => 'Aco'));
  201. }
  202. /**
  203. * Enter description here...
  204. *
  205. * @param object $component
  206. * @return void
  207. * @access public
  208. */
  209. public function initialize(&$component) {
  210. $component->Aro = $this->Aro;
  211. $component->Aco = $this->Aco;
  212. }
  213. /**
  214. * Checks if the given $aro has access to action $action in $aco
  215. *
  216. * @param string $aro ARO
  217. * @param string $aco ACO
  218. * @param string $action Action (defaults to *)
  219. * @return boolean Success (true if ARO has access to action in ACO, false otherwise)
  220. * @access public
  221. */
  222. public function check($aro, $aco, $action = "*") {
  223. if ($aro == null || $aco == null) {
  224. return false;
  225. }
  226. $permKeys = $this->_getAcoKeys($this->Aro->Permission->schema());
  227. $aroPath = $this->Aro->node($aro);
  228. $acoPath = $this->Aco->node($aco);
  229. if (empty($aroPath) || empty($acoPath)) {
  230. trigger_error("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);
  231. return false;
  232. }
  233. if ($acoPath == null || $acoPath == array()) {
  234. trigger_error("DbAcl::check() - Failed ACO node lookup in permissions check. Node references:\nAro: " . print_r($aro, true) . "\nAco: " . print_r($aco, true), E_USER_WARNING);
  235. return false;
  236. }
  237. $aroNode = $aroPath[0];
  238. $acoNode = $acoPath[0];
  239. if ($action != '*' && !in_array('_' . $action, $permKeys)) {
  240. trigger_error(sprintf(__("ACO permissions key %s does not exist in DbAcl::check()", true), $action), E_USER_NOTICE);
  241. return false;
  242. }
  243. $inherited = array();
  244. $acoIDs = Set::extract($acoPath, '{n}.' . $this->Aco->alias . '.id');
  245. $count = count($aroPath);
  246. for ($i = 0 ; $i < $count; $i++) {
  247. $permAlias = $this->Aro->Permission->alias;
  248. $perms = $this->Aro->Permission->find('all', array(
  249. 'conditions' => array(
  250. "{$permAlias}.aro_id" => $aroPath[$i][$this->Aro->alias]['id'],
  251. "{$permAlias}.aco_id" => $acoIDs
  252. ),
  253. 'order' => array($this->Aco->alias . '.lft' => 'desc'),
  254. 'recursive' => 0
  255. ));
  256. if (empty($perms)) {
  257. continue;
  258. } else {
  259. $perms = Set::extract($perms, '{n}.' . $this->Aro->Permission->alias);
  260. foreach ($perms as $perm) {
  261. if ($action == '*') {
  262. foreach ($permKeys as $key) {
  263. if (!empty($perm)) {
  264. if ($perm[$key] == -1) {
  265. return false;
  266. } elseif ($perm[$key] == 1) {
  267. $inherited[$key] = 1;
  268. }
  269. }
  270. }
  271. if (count($inherited) === count($permKeys)) {
  272. return true;
  273. }
  274. } else {
  275. switch ($perm['_' . $action]) {
  276. case -1:
  277. return false;
  278. case 0:
  279. continue;
  280. break;
  281. case 1:
  282. return true;
  283. break;
  284. }
  285. }
  286. }
  287. }
  288. }
  289. return false;
  290. }
  291. /**
  292. * Allow $aro to have access to action $actions in $aco
  293. *
  294. * @param string $aro ARO
  295. * @param string $aco ACO
  296. * @param string $actions Action (defaults to *)
  297. * @param integer $value Value to indicate access type (1 to give access, -1 to deny, 0 to inherit)
  298. * @return boolean Success
  299. * @access public
  300. */
  301. public function allow($aro, $aco, $actions = "*", $value = 1) {
  302. $perms = $this->getAclLink($aro, $aco);
  303. $permKeys = $this->_getAcoKeys($this->Aro->Permission->schema());
  304. $save = array();
  305. if ($perms == false) {
  306. trigger_error(__('DbAcl::allow() - Invalid node', true), E_USER_WARNING);
  307. return false;
  308. }
  309. if (isset($perms[0])) {
  310. $save = $perms[0][$this->Aro->Permission->alias];
  311. }
  312. if ($actions == "*") {
  313. $permKeys = $this->_getAcoKeys($this->Aro->Permission->schema());
  314. $save = array_combine($permKeys, array_pad(array(), count($permKeys), $value));
  315. } else {
  316. if (!is_array($actions)) {
  317. $actions = array('_' . $actions);
  318. }
  319. if (is_array($actions)) {
  320. foreach ($actions as $action) {
  321. if ($action{0} != '_') {
  322. $action = '_' . $action;
  323. }
  324. if (in_array($action, $permKeys)) {
  325. $save[$action] = $value;
  326. }
  327. }
  328. }
  329. }
  330. list($save['aro_id'], $save['aco_id']) = array($perms['aro'], $perms['aco']);
  331. if ($perms['link'] != null && !empty($perms['link'])) {
  332. $save['id'] = $perms['link'][0][$this->Aro->Permission->alias]['id'];
  333. } else {
  334. unset($save['id']);
  335. $this->Aro->Permission->id = null;
  336. }
  337. return ($this->Aro->Permission->save($save) !== false);
  338. }
  339. /**
  340. * Deny access for $aro to action $action in $aco
  341. *
  342. * @param string $aro ARO
  343. * @param string $aco ACO
  344. * @param string $actions Action (defaults to *)
  345. * @return boolean Success
  346. * @access public
  347. */
  348. public function deny($aro, $aco, $action = "*") {
  349. return $this->allow($aro, $aco, $action, -1);
  350. }
  351. /**
  352. * Let access for $aro to action $action in $aco be inherited
  353. *
  354. * @param string $aro ARO
  355. * @param string $aco ACO
  356. * @param string $actions Action (defaults to *)
  357. * @return boolean Success
  358. * @access public
  359. */
  360. public function inherit($aro, $aco, $action = "*") {
  361. return $this->allow($aro, $aco, $action, 0);
  362. }
  363. /**
  364. * Allow $aro to have access to action $actions in $aco
  365. *
  366. * @param string $aro ARO
  367. * @param string $aco ACO
  368. * @param string $actions Action (defaults to *)
  369. * @return boolean Success
  370. * @see allow()
  371. * @access public
  372. */
  373. public function grant($aro, $aco, $action = "*") {
  374. return $this->allow($aro, $aco, $action);
  375. }
  376. /**
  377. * Deny access for $aro to action $action in $aco
  378. *
  379. * @param string $aro ARO
  380. * @param string $aco ACO
  381. * @param string $actions Action (defaults to *)
  382. * @return boolean Success
  383. * @see deny()
  384. * @access public
  385. */
  386. public function revoke($aro, $aco, $action = "*") {
  387. return $this->deny($aro, $aco, $action);
  388. }
  389. /**
  390. * Get an array of access-control links between the given Aro and Aco
  391. *
  392. * @param string $aro ARO
  393. * @param string $aco ACO
  394. * @return array Indexed array with: 'aro', 'aco' and 'link'
  395. * @access public
  396. */
  397. public function getAclLink($aro, $aco) {
  398. $obj = array();
  399. $obj['Aro'] = $this->Aro->node($aro);
  400. $obj['Aco'] = $this->Aco->node($aco);
  401. if (empty($obj['Aro']) || empty($obj['Aco'])) {
  402. return false;
  403. }
  404. return array(
  405. 'aro' => Set::extract($obj, 'Aro.0.'.$this->Aro->alias.'.id'),
  406. 'aco' => Set::extract($obj, 'Aco.0.'.$this->Aco->alias.'.id'),
  407. 'link' => $this->Aro->Permission->find('all', array('conditions' => array(
  408. $this->Aro->Permission->alias . '.aro_id' => Set::extract($obj, 'Aro.0.'.$this->Aro->alias.'.id'),
  409. $this->Aro->Permission->alias . '.aco_id' => Set::extract($obj, 'Aco.0.'.$this->Aco->alias.'.id')
  410. )))
  411. );
  412. }
  413. /**
  414. * Get the keys used in an ACO
  415. *
  416. * @param array $keys Permission model info
  417. * @return array ACO keys
  418. * @access protected
  419. */
  420. protected function _getAcoKeys($keys) {
  421. $newKeys = array();
  422. $keys = array_keys($keys);
  423. foreach ($keys as $key) {
  424. if (!in_array($key, array('id', 'aro_id', 'aco_id'))) {
  425. $newKeys[] = $key;
  426. }
  427. }
  428. return $newKeys;
  429. }
  430. }
  431. /**
  432. * In this file you can extend the AclBase.
  433. *
  434. * @package cake
  435. * @subpackage cake.cake.libs.model.iniacl
  436. */
  437. class IniAcl extends AclBase {
  438. /**
  439. * Array with configuration, parsed from ini file
  440. *
  441. * @var array
  442. * @access public
  443. */
  444. public $config = null;
  445. /**
  446. * The constructor must be overridden, as AclBase is abstract.
  447. *
  448. */
  449. public function __construct() {
  450. }
  451. /**
  452. * Main ACL check function. Checks to see if the ARO (access request object) has access to the ACO (access control object).
  453. * Looks at the acl.ini.php file for permissions (see instructions in /config/acl.ini.php).
  454. *
  455. * @param string $aro ARO
  456. * @param string $aco ACO
  457. * @param string $aco_action Action
  458. * @return boolean Success
  459. * @access public
  460. */
  461. public function check($aro, $aco, $aco_action = null) {
  462. if ($this->config == null) {
  463. $this->config = $this->readConfigFile(CONFIGS . 'acl.ini.php');
  464. }
  465. $aclConfig = $this->config;
  466. if (isset($aclConfig[$aro]['deny'])) {
  467. $userDenies = $this->arrayTrim(explode(",", $aclConfig[$aro]['deny']));
  468. if (array_search($aco, $userDenies)) {
  469. return false;
  470. }
  471. }
  472. if (isset($aclConfig[$aro]['allow'])) {
  473. $userAllows = $this->arrayTrim(explode(",", $aclConfig[$aro]['allow']));
  474. if (array_search($aco, $userAllows)) {
  475. return true;
  476. }
  477. }
  478. if (isset($aclConfig[$aro]['groups'])) {
  479. $userGroups = $this->arrayTrim(explode(",", $aclConfig[$aro]['groups']));
  480. foreach ($userGroups as $group) {
  481. if (array_key_exists($group, $aclConfig)) {
  482. if (isset($aclConfig[$group]['deny'])) {
  483. $groupDenies=$this->arrayTrim(explode(",", $aclConfig[$group]['deny']));
  484. if (array_search($aco, $groupDenies)) {
  485. return false;
  486. }
  487. }
  488. if (isset($aclConfig[$group]['allow'])) {
  489. $groupAllows = $this->arrayTrim(explode(",", $aclConfig[$group]['allow']));
  490. if (array_search($aco, $groupAllows)) {
  491. return true;
  492. }
  493. }
  494. }
  495. }
  496. }
  497. return false;
  498. }
  499. /**
  500. * Parses an INI file and returns an array that reflects the INI file's section structure. Double-quote friendly.
  501. *
  502. * @param string $fileName File
  503. * @return array INI section structure
  504. * @access public
  505. */
  506. public function readConfigFile($fileName) {
  507. $fileLineArray = file($fileName);
  508. foreach ($fileLineArray as $fileLine) {
  509. $dataLine = trim($fileLine);
  510. $firstChar = substr($dataLine, 0, 1);
  511. if ($firstChar != ';' && $dataLine != '') {
  512. if ($firstChar == '[' && substr($dataLine, -1, 1) == ']') {
  513. $sectionName = preg_replace('/[\[\]]/', '', $dataLine);
  514. } else {
  515. $delimiter = strpos($dataLine, '=');
  516. if ($delimiter > 0) {
  517. $key = strtolower(trim(substr($dataLine, 0, $delimiter)));
  518. $value = trim(substr($dataLine, $delimiter + 1));
  519. if (substr($value, 0, 1) == '"' && substr($value, -1) == '"') {
  520. $value = substr($value, 1, -1);
  521. }
  522. $iniSetting[$sectionName][$key]=stripcslashes($value);
  523. } else {
  524. if (!isset($sectionName)) {
  525. $sectionName = '';
  526. }
  527. $iniSetting[$sectionName][strtolower(trim($dataLine))]='';
  528. }
  529. }
  530. }
  531. }
  532. return $iniSetting;
  533. }
  534. /**
  535. * Removes trailing spaces on all array elements (to prepare for searching)
  536. *
  537. * @param array $array Array to trim
  538. * @return array Trimmed array
  539. * @access public
  540. */
  541. public function arrayTrim($array) {
  542. foreach ($array as $key => $value) {
  543. $array[$key] = trim($value);
  544. }
  545. array_unshift($array, "");
  546. return $array;
  547. }
  548. }
  549. ?>