PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Acl/Model/AclAco.php

https://github.com/kareypowell/croogo
PHP | 183 lines | 100 code | 19 blank | 64 comment | 7 complexity | 7d989bbe0c8082bdf2f163e2394ff533 MD5 | raw file
  1. <?php
  2. App::uses('AclNode', 'Model');
  3. /**
  4. * AclAco Model
  5. *
  6. * @category Model
  7. * @package Croogo.Acl.Model
  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 AclAco extends AclNode {
  14. /**
  15. * name
  16. *
  17. * @var string
  18. */
  19. public $name = 'AclAco';
  20. /**
  21. * useTable
  22. *
  23. * @var string
  24. */
  25. public $useTable = 'acos';
  26. /**
  27. * actsAs
  28. *
  29. * @var array
  30. */
  31. public $actsAs = array('Tree');
  32. /**
  33. * alias
  34. *
  35. */
  36. public $alias = 'Aco';
  37. /**
  38. * hasAndBelongsToMany
  39. */
  40. public $hasAndBelongsToMany = array(
  41. 'Aro' => array(
  42. 'with' => 'Acl.AclPermission',
  43. ),
  44. );
  45. /**
  46. * getChildren
  47. *
  48. * @param integer aco id
  49. */
  50. public function getChildren($acoId, $fields = array()) {
  51. $fields = Hash::merge(array('id', 'parent_id', 'alias'), $fields);
  52. $acos = $this->children($acoId, true, $fields);
  53. foreach ($acos as &$aco) {
  54. $aco[$this->alias]['children'] = $this->childCount($aco[$this->alias]['id'], true);
  55. }
  56. return $acos;
  57. }
  58. /**
  59. * Create ACO tree
  60. */
  61. public function createFromPath($path) {
  62. $pathE = explode('/', $path);
  63. $parent = $current = null;
  64. foreach ($pathE as $alias) {
  65. $current[] = $alias;
  66. $node = $this->node(join('/', $current));
  67. if ($node) {
  68. $parent = $node[0];
  69. } else {
  70. $aco = $this->create(array(
  71. 'parent_id' => $parent['Aco']['id'],
  72. 'alias' => $alias,
  73. ));
  74. $parent = $this->save($aco);
  75. }
  76. }
  77. return $parent;
  78. }
  79. /**
  80. * ACL: add ACO
  81. *
  82. * Creates ACOs with permissions for roles.
  83. *
  84. * @param string $action possible values: Controller, Controller/action,
  85. * Plugin/Controller/action
  86. * @param array $allowRoles Role aliases
  87. * @return void
  88. */
  89. public function addAco($action, $allowRoles = array()) {
  90. // AROs
  91. $roles = array();
  92. if (count($allowRoles) > 0) {
  93. $roles = ClassRegistry::init('Users.Role')->find('list', array(
  94. 'conditions' => array(
  95. 'Role.alias' => $allowRoles,
  96. ),
  97. 'fields' => array(
  98. 'Role.id',
  99. 'Role.alias',
  100. ),
  101. ));
  102. }
  103. $this->createFromPath($action);
  104. $Permission = ClassRegistry::init('Acl.AclPermission');
  105. foreach ($roles as $roleId => $roleAlias) {
  106. $Permission->allow(array('model' => 'Role', 'foreign_key' => $roleId), $action);
  107. }
  108. }
  109. /**
  110. * ACL: remove ACO
  111. *
  112. * Removes ACOs and their Permissions
  113. *
  114. * @param string $action possible values: ControllerName, ControllerName/method_name
  115. * @return void
  116. */
  117. public function removeAco($action) {
  118. $acoNode = $this->node($action);
  119. if (isset($acoNode['0']['Aco']['id'])) {
  120. $this->delete($acoNode['0']['Aco']['id']);
  121. }
  122. }
  123. /**
  124. * Get valid permission roots
  125. *
  126. * @return array Array of valid permission roots
  127. */
  128. public function getPermissionRoots() {
  129. $roots = $this->find('all', array(
  130. 'recursive' => -1,
  131. 'fields' => array('id', 'alias'),
  132. 'conditions' => array(
  133. 'parent_id' => null,
  134. 'alias' => array('controllers', 'api'),
  135. ),
  136. ));
  137. $apiRoot = -1;
  138. foreach ($roots as $i => &$root) {
  139. if ($root['Aco']['alias'] === 'api') {
  140. $apiRoot = $root['Aco']['id'];
  141. $apiIndex = $i;
  142. }
  143. $root['Aco']['title'] = ucfirst($root['Aco']['alias']);
  144. }
  145. if (isset($apiIndex)) {
  146. unset($roots[$apiIndex]);
  147. }
  148. $versionRoots = $this->find('all', array(
  149. 'recursive' => -1,
  150. 'fields' => array('id', 'alias'),
  151. 'conditions' => array(
  152. 'parent_id' => $apiRoot,
  153. ),
  154. ));
  155. $apiCount = count($versionRoots);
  156. $api = __d('croogo', 'API');
  157. foreach ($versionRoots as &$versionRoot) {
  158. $alias = strtolower(str_replace('_', '.', $versionRoot['Aco']['alias']));
  159. $versionRoot['Aco']['alias'] = $alias;
  160. $versionRoot['Aco']['title'] = $apiCount == 1 ? $api : $api . ' ' . $alias;
  161. }
  162. return array_merge($roots, $versionRoots);
  163. }
  164. }