TinyAuthorize.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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($settings['cache']) === false) {
  40. throw new CakeException(__('TinyAuth could not find `%s` cache - expects at least a `default` cache', $settings['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. * - User HABTM Roles (Role array in User array)
  48. * - User belongsTo Roles (role_id in User array)
  49. *
  50. * @param array $user The user to authorize
  51. * @param CakeRequest $request The request needing authorization.
  52. * @return boolean
  53. */
  54. public function authorize($user, CakeRequest $request) {
  55. if (isset($user['Role'])) {
  56. $roles = (array)$user['Role'];
  57. } else {
  58. $roles = array($user['role_id']);
  59. }
  60. return $this->validate($roles, $request->params['plugin'], $request->params['controller'], $request->params['action']);
  61. }
  62. /**
  63. * validate the url to the role(s)
  64. * allows single or multi role based authorization
  65. * @return bool $success
  66. */
  67. public function validate($roles, $plugin, $controller, $action) {
  68. $action = Inflector::underscore($action);
  69. $controller = Inflector::underscore($controller);
  70. $plugin = Inflector::underscore($plugin);
  71. if (!empty($this->settings['allowUser'])) {
  72. # all user actions are accessable for logged in users
  73. if (mb_strpos($action, $this->settings['adminPrefix']) !== 0) {
  74. return true;
  75. }
  76. }
  77. if (isset($this->_matchArray[$controller]['*'])) {
  78. $matchArray = $this->_matchArray[$controller]['*'];
  79. if (in_array(-1, $matchArray)) {
  80. return true;
  81. }
  82. foreach ($roles as $role) {
  83. if (in_array($role, $matchArray)) {
  84. return true;
  85. }
  86. }
  87. }
  88. if (!empty($controller) && !empty($action)) {
  89. if (array_key_exists($controller, $this->_matchArray) && !empty($this->_matchArray[$controller][$action])) {
  90. $matchArray = $this->_matchArray[$controller][$action];
  91. # direct access? (even if he has no roles = GUEST)
  92. if (in_array(-1, $matchArray)) {
  93. return true;
  94. }
  95. # normal access (rolebased)
  96. foreach ($roles as $role) {
  97. if (in_array($role, $matchArray)) {
  98. return true;
  99. }
  100. }
  101. }
  102. }
  103. return false;
  104. }
  105. /**
  106. * @return the User model
  107. */
  108. public function getModel() {
  109. return ClassRegistry::init(CLASS_USER);
  110. }
  111. /**
  112. * parse ini files
  113. * @return array $roles
  114. */
  115. protected function _getRoles() {
  116. $res = array();
  117. $cacheKey = 'tiny_acl';
  118. if ($this->settings['autoClearCache'] && Configure::read('debug') > 0) {
  119. Cache::delete($cacheKey, $this->settings['cache']);
  120. }
  121. if (($roles = Cache::read($cacheKey, $this->settings['cache'])) !== false) {
  122. return $roles;
  123. }
  124. if (!file_exists(APP . 'Config' . DS . ACL_FILE)) {
  125. touch(APP . 'Config' . DS . ACL_FILE);
  126. }
  127. $iniArray = parse_ini_file(APP . 'Config' . DS . ACL_FILE, true);
  128. $availableRoles = Configure::read('Role');
  129. if (!is_array($availableRoles)) {
  130. $Model = $this->getModel();
  131. $availableRoles = $Model->Role->find('list', array('fields'=>array('alias', 'id')));
  132. Configure::write('Role', $availableRoles);
  133. }
  134. if (!is_array($availableRoles) || !is_array($iniArray)) {
  135. trigger_error('Invalid Role Setup for TinyAuthorize (no roles found)');
  136. return false;
  137. }
  138. foreach ($iniArray as $key => $array) {
  139. list($plugin, $controllerName) = pluginSplit($key);
  140. $controllerName = Inflector::underscore($controllerName);
  141. foreach ($array as $actions => $roles) {
  142. $actions = explode(',', $actions);
  143. $roles = explode(',', $roles);
  144. foreach ($roles as $key => $role) {
  145. if (!($role = trim($role))) {
  146. continue;
  147. }
  148. if ($role == '*') {
  149. unset($roles[$key]);
  150. $roles = array_merge($roles, array_keys(Configure::read('Role')));
  151. }
  152. }
  153. foreach ($actions as $action) {
  154. if (!($action = trim($action))) {
  155. continue;
  156. }
  157. $actionName = Inflector::underscore($action);
  158. foreach ($roles as $role) {
  159. if (!($role = trim($role)) || $role == '*') {
  160. continue;
  161. }
  162. $newRole = Configure::read('Role.'.strtolower($role));
  163. if (!empty($res[$controllerName][$actionName]) && in_array($newRole, $res[$controllerName][$actionName])) {
  164. continue;
  165. }
  166. $res[$controllerName][$actionName][] = $newRole;
  167. }
  168. }
  169. }
  170. }
  171. Cache::write($cacheKey, $res, $this->settings['cache']);
  172. return $res;
  173. }
  174. }