/controllers/components/build_acl.php

https://github.com/JeremyPlease/acl_tutorial · PHP · 219 lines · 170 code · 23 blank · 26 comment · 36 complexity · 8f5459906d7545247a572109d0805a47 MD5 · raw file

  1. <?php
  2. class BuildAclComponent extends Object {
  3. var $groupModel = null;
  4. var $controller = null;
  5. function initialize(&$controller) {
  6. $this->controller = $controller;
  7. }
  8. function build() {
  9. if (!Configure::read('debug')) {
  10. return $this->_stop();
  11. }
  12. $log = array();
  13. $aco =& $this->controller->Acl->Aco;
  14. $root = $aco->node('controllers');
  15. if (!$root) {
  16. $aco->create(array('parent_id' => null, 'model' => null, 'alias' => 'controllers'));
  17. $root = $aco->save();
  18. $root['Aco']['id'] = $aco->id;
  19. $log[] = 'Created Aco node for controllers';
  20. } else {
  21. $root = $root[0];
  22. }
  23. App::import('Core', 'File');
  24. $Controllers = Configure::listObjects('controller');
  25. $appIndex = array_search('App', $Controllers);
  26. if ($appIndex !== false ) {
  27. unset($Controllers[$appIndex]);
  28. }
  29. $baseMethods = get_class_methods('Controller');
  30. $baseMethods[] = 'buildAcl';
  31. $Plugins = $this->_getPluginControllerNames();
  32. $Controllers = array_merge($Controllers, $Plugins);
  33. // look at each controller in app/controllers
  34. foreach ($Controllers as $ctrlName) {
  35. $methods = $this->_getClassMethods($this->_getPluginControllerPath($ctrlName));
  36. // Do all Plugins First
  37. if ($this->_isPlugin($ctrlName)){
  38. $pluginNode = $aco->node('controllers/'.$this->_getPluginName($ctrlName));
  39. if (!$pluginNode) {
  40. $aco->create(array('parent_id' => $root['Aco']['id'], 'model' => null, 'alias' => $this->_getPluginName($ctrlName)));
  41. $pluginNode = $aco->save();
  42. $pluginNode['Aco']['id'] = $aco->id;
  43. $log[] = 'Created Aco node for ' . $this->_getPluginName($ctrlName) . ' Plugin';
  44. }
  45. }
  46. // find / make controller node
  47. $controllerNode = $aco->node('controllers/'.$ctrlName);
  48. if (!$controllerNode) {
  49. if ($this->_isPlugin($ctrlName)){
  50. $pluginNode = $aco->node('controllers/' . $this->_getPluginName($ctrlName));
  51. $aco->create(array('parent_id' => $pluginNode['0']['Aco']['id'], 'model' => null, 'alias' => $this->_getPluginControllerName($ctrlName)));
  52. $controllerNode = $aco->save();
  53. $controllerNode['Aco']['id'] = $aco->id;
  54. $log[] = 'Created Aco node for ' . $this->_getPluginControllerName($ctrlName) . ' ' . $this->_getPluginName($ctrlName) . ' Plugin Controller';
  55. } else {
  56. $aco->create(array('parent_id' => $root['Aco']['id'], 'model' => null, 'alias' => $ctrlName));
  57. $controllerNode = $aco->save();
  58. $controllerNode['Aco']['id'] = $aco->id;
  59. $log[] = 'Created Aco node for ' . $ctrlName;
  60. }
  61. } else {
  62. $controllerNode = $controllerNode[0];
  63. }
  64. //clean the methods. to remove those in Controller and private actions.
  65. foreach ($methods as $k => $method) {
  66. if (strpos($method, '_', 0) === 0) {
  67. unset($methods[$k]);
  68. continue;
  69. }
  70. if (in_array($method, $baseMethods)) {
  71. unset($methods[$k]);
  72. continue;
  73. }
  74. $methodNode = $aco->node('controllers/'.$ctrlName.'/'.$method);
  75. if (!$methodNode) {
  76. $aco->create(array('parent_id' => $controllerNode['Aco']['id'], 'model' => null, 'alias' => $method));
  77. $methodNode = $aco->save();
  78. $log[] = 'Created Aco node for '. $method;
  79. }
  80. }
  81. }
  82. if(count($log)>0) {
  83. debug($log);
  84. }
  85. }
  86. function updatePermissions($group_id, $path, $type = 'allow', $groupModel = null) {
  87. if (!$this->groupModel) {
  88. if ($groupModel) {
  89. $this->groupModel = $groupModel;
  90. } else {
  91. App::import('Model', 'Group');
  92. $this->groupModel =& new Group;
  93. }
  94. }
  95. $this->groupModel->id = $group_id;
  96. return $this->controller->Acl->$type($this->groupModel, $path);
  97. }
  98. function _getClassMethods($ctrlName = null) {
  99. App::import('Controller', $ctrlName);
  100. if (strlen(strstr($ctrlName, '.')) > 0) {
  101. // plugin's controller
  102. $num = strpos($ctrlName, '.');
  103. $ctrlName = substr($ctrlName, $num+1);
  104. }
  105. $ctrlclass = $ctrlName . 'Controller';
  106. $methods = get_class_methods($ctrlclass);
  107. // Add scaffold defaults if scaffolds are being used
  108. $properties = get_class_vars($ctrlclass);
  109. if (array_key_exists('scaffold',$properties)) {
  110. if($properties['scaffold'] == 'admin') {
  111. $methods = array_merge($methods, array('admin_add', 'admin_edit', 'admin_index', 'admin_view', 'admin_delete'));
  112. } else {
  113. $methods = array_merge($methods, array('add', 'edit', 'index', 'view', 'delete'));
  114. }
  115. }
  116. return $methods;
  117. }
  118. function _isPlugin($ctrlName = null) {
  119. $arr = String::tokenize($ctrlName, '/');
  120. if (count($arr) > 1) {
  121. return true;
  122. } else {
  123. return false;
  124. }
  125. }
  126. function _getPluginControllerPath($ctrlName = null) {
  127. $arr = String::tokenize($ctrlName, '/');
  128. if (count($arr) == 2) {
  129. return $arr[0] . '.' . $arr[1];
  130. } else {
  131. return $arr[0];
  132. }
  133. }
  134. function _getPluginName($ctrlName = null) {
  135. $arr = String::tokenize($ctrlName, '/');
  136. if (count($arr) == 2) {
  137. return $arr[0];
  138. } else {
  139. return false;
  140. }
  141. }
  142. function _getPluginControllerName($ctrlName = null) {
  143. $arr = String::tokenize($ctrlName, '/');
  144. if (count($arr) == 2) {
  145. return $arr[1];
  146. } else {
  147. return false;
  148. }
  149. }
  150. /**
  151. * Get the names of the plugin controllers ...
  152. *
  153. * This function will get an array of the plugin controller names, and
  154. * also makes sure the controllers are available for us to get the
  155. * method names by doing an App::import for each plugin controller.
  156. *
  157. * @return array of plugin names.
  158. *
  159. */
  160. function _getPluginControllerNames() {
  161. App::import('Core', 'File', 'Folder');
  162. $paths = Configure::getInstance();
  163. $folder =& new Folder();
  164. $folder->cd(APP . 'plugins');
  165. // Get the list of plugins
  166. $Plugins = $folder->read();
  167. $Plugins = $Plugins[0];
  168. $arr = array();
  169. // Loop through the plugins
  170. foreach($Plugins as $pluginName) {
  171. // Change directory to the plugin
  172. $didCD = $folder->cd(APP . 'plugins'. DS . $pluginName . DS . 'controllers');
  173. // Get a list of the files that have a file name that ends
  174. // with controller.php
  175. $files = $folder->findRecursive('.*_controller\.php');
  176. // Loop through the controllers we found in the plugins directory
  177. foreach($files as $fileName) {
  178. // Get the base file name
  179. $file = basename($fileName);
  180. // Get the controller name
  181. $file = Inflector::camelize(substr($file, 0, strlen($file)-strlen('_controller.php')));
  182. if (!preg_match('/^'. Inflector::humanize($pluginName). 'App/', $file)) {
  183. if (!App::import('Controller', $pluginName.'.'.$file)) {
  184. debug('Error importing '.$file.' for plugin '.$pluginName);
  185. } else {
  186. /// Now prepend the Plugin name ...
  187. // This is required to allow us to fetch the method names.
  188. $arr[] = Inflector::humanize($pluginName) . "/" . $file;
  189. }
  190. }
  191. }
  192. }
  193. return $arr;
  194. }
  195. }
  196. ?>