PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Acl/Controller/AclPermissionsController.php

https://github.com/kareypowell/croogo
PHP | 175 lines | 109 code | 19 blank | 47 comment | 20 complexity | 6257a2aad31713a7c2d8e00b4e4126f2 MD5 | raw file
  1. <?php
  2. App::uses('AclAppController', 'Acl.Controller');
  3. /**
  4. * AclPermissions Controller
  5. *
  6. * @category Controller
  7. * @package Croogo.Acl
  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 AclPermissionsController extends AclAppController {
  14. /**
  15. * Controller name
  16. *
  17. * @var string
  18. * @access public
  19. */
  20. public $name = 'AclPermissions';
  21. /**
  22. * Models used by the Controller
  23. *
  24. * @var array
  25. * @access public
  26. */
  27. public $uses = array(
  28. 'Acl.AclPermission',
  29. 'Acl.AclAco',
  30. 'Acl.AclAro',
  31. 'Users.Role',
  32. );
  33. /**
  34. * beforeFilter
  35. *
  36. * @return void
  37. */
  38. public function beforeFilter() {
  39. parent::beforeFilter();
  40. $this->Security->requirePost('admin_toggle');
  41. if ($this->action == 'admin_toggle') {
  42. $this->Security->csrfCheck = false;
  43. }
  44. }
  45. /**
  46. * admin_index
  47. *
  48. * @param id integer aco id, when null, the root ACO is used
  49. * @return void
  50. */
  51. public function admin_index($id = null, $level = null) {
  52. $this->set('title_for_layout', __d('croogo', 'Permissions'));
  53. if (isset($this->request->query['root'])) {
  54. $query = strtolower($this->request->query['root']);
  55. }
  56. if ($id == null) {
  57. $root = isset($query) ? $query : 'controllers';
  58. $root = $this->AclAco->node(str_replace('.', '_', $root));
  59. $root = $root[0];
  60. } else {
  61. $root = $this->AclAco->read(null, $id);
  62. }
  63. if ($level !== null) {
  64. $level++;
  65. }
  66. $acos = array();
  67. $roles = $this->Role->find('list');
  68. if ($root) {
  69. $acos = $this->AclAco->getChildren($root['Aco']['id']);
  70. }
  71. $this->set(compact('acos', 'roles', 'level'));
  72. $aros = $this->AclAro->getRoles($roles);
  73. if ($root && $this->RequestHandler->ext == 'json') {
  74. $options = array_intersect_key(
  75. $this->request->query,
  76. array('perms' => null, 'urls' => null)
  77. );
  78. $cacheName = 'permissions_aco_' . $root['Aco']['id'];
  79. $permissions = Cache::read($cacheName, 'permissions');
  80. if ($permissions === false) {
  81. $permissions = $this->AclPermission->format($acos, $aros, $options);
  82. Cache::write($cacheName, $permissions, 'permissions');
  83. }
  84. } else {
  85. $permissions = array();
  86. }
  87. $this->set(compact('aros', 'permissions'));
  88. if ($this->request->is('ajax') && isset($query)) {
  89. $this->render('Acl.Elements/admin/acl_permissions_table');
  90. } else {
  91. $this->_setPermissionRoots();
  92. }
  93. }
  94. protected function _setPermissionRoots() {
  95. $roots = $this->AclAco->getPermissionRoots();
  96. foreach ($roots as $id => $root) {
  97. Croogo::hookAdminTab(
  98. 'AclPermissions/admin_index',
  99. __d('croogo', $root['Aco']['title']),
  100. 'Croogo.blank',
  101. array(
  102. 'linkOptions' => array(
  103. 'data-alias' => $root['Aco']['alias'],
  104. ),
  105. )
  106. );
  107. }
  108. $this->set(compact('roots'));
  109. }
  110. /**
  111. * admin_toggle
  112. *
  113. * @param integer $acoId
  114. * @param integer $aroId
  115. * @return void
  116. */
  117. public function admin_toggle($acoId, $aroId) {
  118. if (!$this->RequestHandler->isAjax()) {
  119. return $this->redirect(array('action' => 'index'));
  120. }
  121. // see if acoId and aroId combination exists
  122. $this->AclPermission->Aro->id = $aroId;
  123. $aro = $this->AclPermission->Aro->read();
  124. $aro = $aro['Aro'];
  125. $path = $this->AclPermission->Aco->getPath($acoId);
  126. $path = join('/', Hash::extract($path, '{n}.Aco.alias'));
  127. $permitted = !$this->AclPermission->check($aro, $path);
  128. $success = $this->AclPermission->allow($aro, $path, '*', $permitted ? 1 : -1);
  129. if ($success) {
  130. $this->AclPermission->Aco->id = $acoId;
  131. $parentAcoId = $this->AclPermission->Aco->field('parent_id');
  132. $cacheName = 'permissions_aco_' . $parentAcoId;
  133. Cache::delete($cacheName, 'permissions');
  134. Cache::delete('permissions_public', 'permissions');
  135. }
  136. $this->set(compact('acoId', 'aroId', 'data', 'success', 'permitted'));
  137. }
  138. /**
  139. * admin_upgrade
  140. *
  141. * upgrades ACL database
  142. * @return void
  143. */
  144. public function admin_upgrade() {
  145. App::uses('AclUpgrade', 'Acl.Lib');
  146. $AclUpgrade = new AclUpgrade();
  147. $result = $AclUpgrade->upgrade();
  148. if ($result === true) {
  149. $this->Session->delete(AuthComponent::$sessionKey . '.aclUpgrade');
  150. $this->Session->setFlash(__d('croogo', 'ACL database has been upgraded successfully'), 'default', array('class' => 'success'));
  151. } else {
  152. $this->Session->setFlash(join('<br>', $result), 'default', array('class' => 'error'));
  153. }
  154. return $this->redirect($this->referer());
  155. }
  156. }