TinyAuthorize.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. App::uses('Inflector', 'Utility');
  3. if (!defined('AUTH_CACHE')) {
  4. define('AUTH_CACHE', '_cake_core_'); # use the most persistent cache
  5. }
  6. if (!defined('ACL_FILE')) {
  7. define('ACL_FILE', 'acl.ini');
  8. }
  9. /**
  10. * Probably the most simple and fastest Acl out there.
  11. * Only one config file `roles.ini` necessary
  12. * Doesnt even need a Role Model/table
  13. * @link http://www.dereuromark.de/2011/12/18/tinyauth-the-fastest-and-easiest-authorization-for-cake2
  14. *
  15. * Usage:
  16. * Include it in your beforeFilter() method of the AppController
  17. * $this->Auth->authorize = array('Tools.Tiny');
  18. *
  19. * Or with admin prefix protection only
  20. * $this->Auth->authorize = array('Tools.Tiny'=>array('allowUser'=>true));
  21. *
  22. * @version 1.1 - now uses most persistent _cake_core_ cache by default
  23. * @author Mark Scherer
  24. * @cakephp 2.0
  25. * @license MIT
  26. * 2011-12-31 ms
  27. */
  28. class TinyAuthorize extends BaseAuthorize {
  29. protected $_matchArray = array();
  30. protected $_defaults = array(
  31. 'allowUser' => false, # quick way to allow user access to non prefixed urls
  32. 'adminPrefix' => 'admin_',
  33. 'cache' => AUTH_CACHE,
  34. 'autoClearCache' => false # usually done by Cache automatically in debug mode
  35. );
  36. public function __construct(ComponentCollection $Collection, $settings = array()) {
  37. $settings = am($this->_defaults, $settings);
  38. parent::__construct($Collection, $settings);
  39. if (Cache::config('default') === false) {
  40. throw new CakeException(__('TinyAuth expects at least a `default` cache'));
  41. }
  42. $this->_matchArray = $this->_getRoles();
  43. }
  44. /**
  45. * Authorize a user using the AclComponent.
  46. * allows single or multi role based authorization
  47. *
  48. * @param array $user The user to authorize
  49. * @param CakeRequest $request The request needing authorization.
  50. * @return boolean
  51. */
  52. public function authorize($user, CakeRequest $request) {
  53. if (isset($user['Role'])) {
  54. $roles = (array)$user['Role'];
  55. } else {
  56. $roles = array($user['role_id']);
  57. }
  58. return $this->validate($roles, $request->params['plugin'], $request->params['controller'], $request->params['action']);
  59. }
  60. /**
  61. * validate the url to the role(s)
  62. * allows single or multi role based authorization
  63. * @return bool $success
  64. */
  65. public function validate($roles, $plugin, $controller, $action) {
  66. $action = Inflector::underscore($action);
  67. $controller = Inflector::underscore($controller);
  68. $plugin = Inflector::underscore($plugin);
  69. if (!empty($this->settings['allowUser'])) {
  70. # all user actions are accessable for logged in users
  71. if (mb_strpos($action, $this->settings['adminPrefix']) !== 0) {
  72. return true;
  73. }
  74. }
  75. if (isset($this->_matchArray[$controller]['*'])) {
  76. $matchArray = $this->_matchArray[$controller]['*'];
  77. if (in_array(-1, $matchArray)) {
  78. return true;
  79. }
  80. foreach ($roles as $role) {
  81. if (in_array($role, $matchArray)) {
  82. return true;
  83. }
  84. }
  85. }
  86. if (!empty($controller) && !empty($action)) {
  87. if (array_key_exists($controller, $this->_matchArray) && !empty($this->_matchArray[$controller][$action])) {
  88. $matchArray = $this->_matchArray[$controller][$action];
  89. # direct access? (even if he has no roles = GUEST)
  90. if (in_array(-1, $matchArray)) {
  91. return true;
  92. }
  93. # normal access (rolebased)
  94. foreach ($roles as $role) {
  95. if (in_array($role, $matchArray)) {
  96. return true;
  97. }
  98. }
  99. }
  100. }
  101. return false;
  102. }
  103. public function getModel() {
  104. return ClassRegistry::init(CLASS_USER);
  105. }
  106. /**
  107. * parse ini files
  108. */
  109. protected function _getRoles() {
  110. $res = array();
  111. $cacheKey = 'tiny_acl';
  112. if ($this->settings['autoClearCache'] && Configure::read('debug') > 0) {
  113. Cache::delete($cacheKey, $this->settings['cache']);
  114. }
  115. if (($roles = Cache::read($cacheKey, $this->settings['cache'])) !== false) {
  116. return $roles;
  117. }
  118. if (!file_exists(APP . 'Config' . DS . ACL_FILE)) {
  119. touch(APP . 'Config' . DS . ACL_FILE);
  120. }
  121. $iniArray = parse_ini_file(APP . 'Config' . DS . ACL_FILE, true);
  122. $availableRoles = Configure::read('Role');
  123. if (!is_array($availableRoles)) {
  124. $Model = $this->getModel();
  125. $availableRoles = $Model->Role->find('list', array('fields'=>array('alias', 'id')));
  126. Configure::write('Role', $availableRoles);
  127. }
  128. if (!is_array($availableRoles) || !is_array($iniArray)) {
  129. trigger_error('Invalid Role Setup for TinyAuthorize (no roles found)');
  130. return false;
  131. }
  132. foreach ($iniArray as $key => $array) {
  133. list($plugin, $controllerName) = pluginSplit($key);
  134. $controllerName = Inflector::underscore($controllerName);
  135. foreach ($array as $actions => $roles) {
  136. $actions = explode(',', $actions);
  137. $roles = explode(',', $roles);
  138. foreach ($roles as $key => $role) {
  139. if (!($role = trim($role))) {
  140. continue;
  141. }
  142. if ($role == '*') {
  143. unset($roles[$key]);
  144. $roles = array_merge($roles, array_keys(Configure::read('Role')));
  145. }
  146. }
  147. foreach ($actions as $action) {
  148. if (!($action = trim($action))) {
  149. continue;
  150. }
  151. $actionName = Inflector::underscore($action);
  152. foreach ($roles as $role) {
  153. if (!($role = trim($role)) || $role == '*') {
  154. continue;
  155. }
  156. $newRole = Configure::read('Role.'.strtolower($role));
  157. if (!empty($res[$controllerName][$actionName]) && in_array($newRole, $res[$controllerName][$actionName])) {
  158. continue;
  159. }
  160. $res[$controllerName][$actionName][] = $newRole;
  161. }
  162. }
  163. }
  164. }
  165. Cache::write($cacheKey, $res, $this->settings['cache']);
  166. return $res;
  167. }
  168. }