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

/Acl/Controller/Component/AclFilterComponent.php

https://github.com/kareypowell/croogo
PHP | 255 lines | 181 code | 20 blank | 54 comment | 28 complexity | 895c0d56909a65201fc23fc1a0c0efaa MD5 | raw file
  1. <?php
  2. App::uses('Component', 'Controller');
  3. /**
  4. * AclFilter Component
  5. *
  6. * @category Component
  7. * @package Croogo.Acl.Controller.Component
  8. * @version 1.0
  9. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  10. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  11. * @link http://www.croogo.org
  12. */
  13. class AclFilterComponent extends Component {
  14. /**
  15. * _controller
  16. *
  17. * @var Controller
  18. */
  19. protected $_controller = null;
  20. /**
  21. * initialize
  22. *
  23. * @param Controller $controller instance of controller
  24. * @return void
  25. */
  26. public function initialize(Controller $controller) {
  27. $this->_controller = $controller;
  28. if ($this->_config('multiRole')) {
  29. Croogo::hookAdminTab('Users/admin_add', 'Roles', 'Acl.admin/roles');
  30. Croogo::hookAdminTab('Users/admin_edit', 'Roles', 'Acl.admin/roles');
  31. }
  32. }
  33. /**
  34. * Helper function to retrieve value from `Access Control` settings
  35. *
  36. * @return mixed null when config key is not found
  37. */
  38. protected function _config($key) {
  39. static $config = null;
  40. if (empty($config)) {
  41. $config = Configure::read('Access Control');
  42. }
  43. if (array_key_exists($key, $config)) {
  44. return $config[$key];
  45. }
  46. return null;
  47. }
  48. /**
  49. * configure component settings
  50. *
  51. * @return void
  52. */
  53. protected function _configure() {
  54. if (!$this->_Collection->loaded('Acl.AclAutoLogin')) {
  55. $this->_Collection->load('Acl.AclAutoLogin');
  56. }
  57. if (!$this->_Collection->loaded('Cookie')) {
  58. $this->_Collection->load('Cookie');
  59. }
  60. //Configure AuthComponent
  61. $this->_controller->Auth->authenticate = array(
  62. AuthComponent::ALL => array(
  63. 'userModel' => 'Users.User',
  64. 'fields' => array(
  65. 'username' => 'username',
  66. 'password' => 'password',
  67. ),
  68. 'scope' => array(
  69. 'User.status' => 1,
  70. ),
  71. ),
  72. );
  73. if ($this->_config('autoLoginDuration')) {
  74. if (!function_exists('mcrypt_encrypt')) {
  75. $notice = __d('croogo', '"AutoLogin" (Remember Me) disabled since mcrypt_encrypt is not available');
  76. $this->log($notice, LOG_CRIT);
  77. if (isset($this->_controller->request->params['admin'])) {
  78. $this->_controller->Session->setFlash($notice, 'default', null, array('class', 'error'));
  79. }
  80. if (isset($this->_controller->Setting)) {
  81. $Setting = $this->_controller->Setting;
  82. } else {
  83. $Setting = ClassRegistry::init('Settings.Setting');
  84. }
  85. $Setting->write('Access Control.autoLoginDuration', '');
  86. }
  87. $this->_controller->Auth->authenticate[] = 'Acl.Cookie';
  88. }
  89. if ($this->_config('multiColumn')) {
  90. $this->_controller->Auth->authenticate[] = 'Acl.MultiColumn';
  91. } else {
  92. $this->_controller->Auth->authenticate[] = 'Form';
  93. }
  94. $actionPath = $this->_controller->request->is('api') ? 'api' : 'controllers';
  95. $this->_controller->Auth->authorize = array(
  96. AuthComponent::ALL => array(
  97. 'actionPath' => 'controllers',
  98. 'userModel' => 'Users.User',
  99. ),
  100. 'Acl.AclCached' => array(
  101. 'actionPath' => $actionPath,
  102. ),
  103. );
  104. if (isset($this->_controller->request->params['admin']) &&
  105. !$this->_controller->Auth->loggedIn()) {
  106. $this->_controller->Auth->authError = false;
  107. }
  108. $this->configureLoginActions();
  109. }
  110. /**
  111. * Load login actions configurations
  112. *
  113. * @return void
  114. */
  115. public function configureLoginActions() {
  116. $this->_controller->Auth->loginAction = array(
  117. 'plugin' => 'users',
  118. 'controller' => 'users',
  119. 'action' => 'login',
  120. );
  121. $this->_controller->Auth->logoutRedirect = array(
  122. 'plugin' => 'users',
  123. 'controller' => 'users',
  124. 'action' => 'login',
  125. );
  126. $this->_controller->Auth->loginRedirect = array(
  127. 'plugin' => 'settings',
  128. 'controller' => 'settings',
  129. 'action' => 'dashboard',
  130. );
  131. $this->_controller->Auth->unauthorizedRedirect = array(
  132. 'plugin' => 'users',
  133. 'controller' => 'users',
  134. 'action' => 'login',
  135. );
  136. $config = Configure::read('Acl');
  137. if (!empty($config['Auth']) && is_array($config['Auth'])) {
  138. $isAdminRequest = !empty($this->_controller->request->params['admin']);
  139. $authActions = array(
  140. 'loginAction', 'loginRedirect', 'logoutRedirect',
  141. 'unauthorizedRedirect',
  142. );
  143. foreach ($config['Auth'] as $property => $value) {
  144. $isAdminRoute = !empty($value['admin']);
  145. $isAuthAction = in_array($property, $authActions);
  146. if (!is_string($value) && $isAdminRequest !== $isAdminRoute && $isAuthAction) {
  147. continue;
  148. }
  149. $this->_controller->Auth->{$property} = $value;
  150. }
  151. }
  152. }
  153. /**
  154. * acl and auth
  155. *
  156. * @return void
  157. */
  158. public function auth() {
  159. $this->_configure();
  160. $user = $this->_controller->Auth->user();
  161. // authorization for authenticated user is handled by authorize object
  162. if ($user) {
  163. return;
  164. }
  165. // public access authorization
  166. $cacheName = 'permissions_public';
  167. if (($perms = Cache::read($cacheName, 'permissions')) === false) {
  168. $perms = $this->getPermissions('Role', 3);
  169. Cache::write($cacheName, $perms, 'permissions');
  170. }
  171. $actionPath = $this->_controller->request->is('api') ? 'api' : 'controllers';
  172. if (!empty($perms['allowed'][$actionPath][$this->_controller->name])) {
  173. $this->_controller->Auth->allow(
  174. $perms['allowed'][$actionPath][$this->_controller->name]
  175. );
  176. }
  177. }
  178. /**
  179. * getPermissions
  180. * retrieve list of permissions from database
  181. * @param string $model model name
  182. * @param string $id model id
  183. * @return array list of authorized and allowed actions
  184. */
  185. public function getPermissions($model, $id) {
  186. $Acl =& $this->_controller->Acl;
  187. $aro = array('model' => $model, 'foreign_key' => $id);
  188. $node = $Acl->Aro->node($aro);
  189. $nodes = $Acl->Aro->getPath($node[0]['Aro']['id']);
  190. $aros = Hash::extract($node, '{n}.Aro.id');
  191. if (!empty($nodes)) {
  192. $aros = Hash::merge($aros, Hash::extract($nodes, '{n}.Aro.id'));
  193. }
  194. $permissions = $Acl->Aro->Permission->find('all', array(
  195. 'conditions' => array(
  196. 'Permission.aro_id' => $aros,
  197. 'Permission._create' => 1,
  198. 'Permission._read' => 1,
  199. 'Permission._update' => 1,
  200. 'Permission._delete' => 1,
  201. )
  202. ));
  203. $authorized = $allowedActions = array();
  204. foreach ($permissions as $permission) {
  205. $path = $Acl->Aco->getPath($permission['Permission']['aco_id']);
  206. if (empty($path)) {
  207. continue;
  208. }
  209. $acos = count($path);
  210. if ($acos == 5) {
  211. // api controller/action
  212. $controller = $path[3]['Aco']['alias'];
  213. $action = $path[1]['Aco']['alias'] . '_' . $path[4]['Aco']['alias'];
  214. } elseif ($acos == 4) {
  215. // plugin controller/action
  216. $controller = $path[2]['Aco']['alias'];
  217. $action = $path[3]['Aco']['alias'];
  218. } elseif ($acos == 3) {
  219. // app controller/action
  220. $controller = $path[1]['Aco']['alias'];
  221. $action = $path[2]['Aco']['alias'];
  222. } else {
  223. $this->log(sprintf(
  224. 'Incomplete path for aco_id = %s:',
  225. $permission['Permission']['id']
  226. ));
  227. $this->log($path);
  228. }
  229. $actionPath = $path[0]['Aco']['alias'];
  230. $allowedActions[$actionPath][$controller][] = $action;
  231. $authorized[] = implode('/', Hash::extract($path, '{n}.Aco.alias'));
  232. }
  233. return array('authorized' => $authorized, 'allowed' => $allowedActions);
  234. }
  235. }