PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/ManiAdil/jardinorient
PHP | 172 lines | 67 code | 21 blank | 84 comment | 12 complexity | a8175fb93d24b8364f13708df4fe1acc 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.Acl
  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. * IniAcl implements an access control system using an INI file. An example
  18. * of the ini file used can be found in /config/acl.ini.php.
  19. *
  20. * @package Cake.Controller.Component.Acl
  21. */
  22. class IniAcl extends Object implements AclInterface {
  23. /**
  24. * Array with configuration, parsed from ini file
  25. *
  26. * @var array
  27. */
  28. public $config = null;
  29. /**
  30. * The Hash::extract() path to the user/aro identifier in the
  31. * acl.ini file. This path will be used to extract the string
  32. * representation of a user used in the ini file.
  33. *
  34. * @var string
  35. */
  36. public $userPath = 'User.username';
  37. /**
  38. * Initialize method
  39. *
  40. * @param AclBase $component
  41. * @return void
  42. */
  43. public function initialize(Component $component) {
  44. }
  45. /**
  46. * No op method, allow cannot be done with IniAcl
  47. *
  48. * @param string $aro ARO The requesting object identifier.
  49. * @param string $aco ACO The controlled object identifier.
  50. * @param string $action Action (defaults to *)
  51. * @return boolean Success
  52. */
  53. public function allow($aro, $aco, $action = "*") {
  54. }
  55. /**
  56. * No op method, deny cannot be done with IniAcl
  57. *
  58. * @param string $aro ARO The requesting object identifier.
  59. * @param string $aco ACO The controlled object identifier.
  60. * @param string $action Action (defaults to *)
  61. * @return boolean Success
  62. */
  63. public function deny($aro, $aco, $action = "*") {
  64. }
  65. /**
  66. * No op method, inherit cannot be done with IniAcl
  67. *
  68. * @param string $aro ARO The requesting object identifier.
  69. * @param string $aco ACO The controlled object identifier.
  70. * @param string $action Action (defaults to *)
  71. * @return boolean Success
  72. */
  73. public function inherit($aro, $aco, $action = "*") {
  74. }
  75. /**
  76. * Main ACL check function. Checks to see if the ARO (access request object) has access to the
  77. * ACO (access control object).Looks at the acl.ini.php file for permissions
  78. * (see instructions in /config/acl.ini.php).
  79. *
  80. * @param string $aro ARO
  81. * @param string $aco ACO
  82. * @param string $action Action
  83. * @return boolean Success
  84. */
  85. public function check($aro, $aco, $action = null) {
  86. if (!$this->config) {
  87. $this->config = $this->readConfigFile(APP . 'Config' . DS . 'acl.ini.php');
  88. }
  89. $aclConfig = $this->config;
  90. if (is_array($aro)) {
  91. $aro = Hash::get($aro, $this->userPath);
  92. }
  93. if (isset($aclConfig[$aro]['deny'])) {
  94. $userDenies = $this->arrayTrim(explode(",", $aclConfig[$aro]['deny']));
  95. if (array_search($aco, $userDenies)) {
  96. return false;
  97. }
  98. }
  99. if (isset($aclConfig[$aro]['allow'])) {
  100. $userAllows = $this->arrayTrim(explode(",", $aclConfig[$aro]['allow']));
  101. if (array_search($aco, $userAllows)) {
  102. return true;
  103. }
  104. }
  105. if (isset($aclConfig[$aro]['groups'])) {
  106. $userGroups = $this->arrayTrim(explode(",", $aclConfig[$aro]['groups']));
  107. foreach ($userGroups as $group) {
  108. if (array_key_exists($group, $aclConfig)) {
  109. if (isset($aclConfig[$group]['deny'])) {
  110. $groupDenies = $this->arrayTrim(explode(",", $aclConfig[$group]['deny']));
  111. if (array_search($aco, $groupDenies)) {
  112. return false;
  113. }
  114. }
  115. if (isset($aclConfig[$group]['allow'])) {
  116. $groupAllows = $this->arrayTrim(explode(",", $aclConfig[$group]['allow']));
  117. if (array_search($aco, $groupAllows)) {
  118. return true;
  119. }
  120. }
  121. }
  122. }
  123. }
  124. return false;
  125. }
  126. /**
  127. * Parses an INI file and returns an array that reflects the
  128. * INI file's section structure. Double-quote friendly.
  129. *
  130. * @param string $filename File
  131. * @return array INI section structure
  132. */
  133. public function readConfigFile($filename) {
  134. App::uses('IniReader', 'Configure');
  135. $iniFile = new IniReader(dirname($filename) . DS);
  136. return $iniFile->read(basename($filename));
  137. }
  138. /**
  139. * Removes trailing spaces on all array elements (to prepare for searching)
  140. *
  141. * @param array $array Array to trim
  142. * @return array Trimmed array
  143. */
  144. public function arrayTrim($array) {
  145. foreach ($array as $key => $value) {
  146. $array[$key] = trim($value);
  147. }
  148. array_unshift($array, "");
  149. return $array;
  150. }
  151. }