PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/vendors/olc_baker/skels/default/Plugin/Permissible/Model/PermissibleAco.php

https://github.com/kiang/olc_baker
PHP | 313 lines | 215 code | 18 blank | 80 comment | 22 complexity | 8259c06bddacded2960fe8281e6a536e MD5 | raw file
  1. <?php
  2. /**
  3. * Permissible Plugin PermissibleAco Model class
  4. *
  5. * Models the ACOs and provides various management functions
  6. *
  7. * @package permissible
  8. * @subpackage permissible.models
  9. */
  10. class PermissibleAco extends PermissibleAppModel
  11. {
  12. /**
  13. * Sets the name for the model
  14. *
  15. * @var array
  16. * @access public
  17. */
  18. public $name = 'PermissiableAco';
  19. /**
  20. * Sets the table name for the model
  21. *
  22. * @var array
  23. * @access public
  24. */
  25. public $useTable = 'acos';
  26. /**
  27. * Array containing the names of behaviours this model uses
  28. *
  29. * @var array
  30. * @access public
  31. */
  32. public $actsAs = array('Tree');
  33. /**
  34. * Sets the model to cache queries for optimisation
  35. *
  36. * @var array
  37. * @access public
  38. */
  39. public $cacheQueries = true;
  40. /**
  41. * Recursively saves a complete ACO tree
  42. *
  43. * @return null
  44. * @access public
  45. */
  46. public function completeTreeSave($tree, $parent_id = null)
  47. {
  48. if ($parent_id === null) {
  49. $this->truncate();
  50. }
  51. foreach ($tree as $aro => $tre) {
  52. $this->create();
  53. $this->save(array(
  54. 'parent_id' => $parent_id,
  55. 'alias' => $aro
  56. ));
  57. if (is_array($tre)) {
  58. $this->completeTreeSave($tre, $this->id);
  59. }
  60. }
  61. }
  62. /**
  63. * Recursively updates the ACO tree
  64. *
  65. * @return null
  66. * @access public
  67. */
  68. public function completeTreeUpdate($tree, $parent_id = null)
  69. {
  70. if (!is_array($tree)) {
  71. return;
  72. }
  73. $keys = array_keys($tree);
  74. sort($keys);
  75. $children = $this->find('list', array(
  76. 'conditions' => array(
  77. 'PermissibleAco.parent_id' => $parent_id
  78. ),
  79. 'fields' => array(
  80. 'PermissibleAco.alias'
  81. )
  82. ));
  83. sort($children);
  84. $same = ($children === $keys);
  85. $children = $this->find('list', array(
  86. 'conditions' => array(
  87. 'PermissibleAco.parent_id' => $parent_id
  88. ),
  89. 'fields' => array(
  90. 'PermissibleAco.alias'
  91. )
  92. ));
  93. if ($same) {
  94. foreach ($children as $id => $alias) {
  95. $this->completeTreeUpdate($tree[$alias], $id);
  96. }
  97. } else {
  98. foreach ($children as $id => $child) {
  99. if (!in_array($child, $keys)) {
  100. $this->delete($id);
  101. unset($children[$id]);
  102. }
  103. }
  104. foreach ($keys as $key) {
  105. if (!in_array($key, $children)) {
  106. $this->create();
  107. $this->save(array(
  108. 'PermissibleAco' => array(
  109. 'parent_id' => $parent_id,
  110. 'alias' => $key
  111. )
  112. ));
  113. }
  114. }
  115. }
  116. }
  117. /**
  118. * Automates finding of ARO aliases where possible
  119. *
  120. * @return array Results
  121. * @access public
  122. */
  123. public function afterFind($results, $primary)
  124. {
  125. foreach ($results as $key => $result) {
  126. if (!isset($result[$this->alias]['alias']) && isset($result[$this->alias]['model'])) {
  127. $model = ClassRegistry::init($result[$this->alias]['model']);
  128. $model->id = $result[$this->alias]['foreign_key'];
  129. switch ($result[$this->alias]['model']) {
  130. case Configure::read('Permissible.UserModelAlias'):
  131. $user = ClassRegistry::init(Configure::read('Permissible.UserModel'));
  132. $alias = $model->read($user->displayField);
  133. $alias = $alias[Configure::read('Permissible.UserModelAlias')][$user->displayField];
  134. break;
  135. case Configure::read('Permissible.GroupModelAlias'):
  136. $group = ClassRegistry::init(Configure::read('Permissible.GroupModel'));
  137. $alias = $model->read($group->displayField);
  138. $alias = $alias[Configure::read('Permissible.GroupModelAlias')][$group->displayField];
  139. break;
  140. }
  141. $this->save(array(
  142. $this->alias => array(
  143. 'id' => $result[$this->alias]['id'],
  144. 'alias' => $alias
  145. )
  146. ));
  147. $results[$key][$this->alias]['alias'] = $alias;
  148. }
  149. }
  150. return $results;
  151. }
  152. /**
  153. * Generates a list of ACOs
  154. *
  155. * @return array List
  156. * @access public
  157. */
  158. public function generateList($parent = null)
  159. {
  160. $temp = $this->generateTreeList(array(
  161. 'parent_id' => $parent
  162. ), null, null, null, 1);
  163. $ret = array();
  164. foreach ($temp as $id => $name) {
  165. $this->id = $id;
  166. $item = $this->read();
  167. $next = $this->generateList($id);
  168. if ($next !== array()) {
  169. $item[$this->alias]['sub-menu'] = $next;
  170. }
  171. $ret[] = $item[$this->alias];
  172. }
  173. return $ret;
  174. }
  175. /**
  176. * Generates a list of ACOs and whether an ARO can access them
  177. *
  178. * @return array List
  179. * @access public
  180. */
  181. public function generateListPerms($Acl, $aro_alias, $aco_alias = array(), $parent = null)
  182. {
  183. $temp = $this->generateTreeList(array(
  184. 'parent_id' => $parent
  185. ), null, null, null, 1);
  186. $ret = array();
  187. foreach ($temp as $id => $name) {
  188. $this->id = $id;
  189. $item = $this->read();
  190. $aliases = $aco_alias;
  191. $aliases[] = $item[$this->alias]['alias'];
  192. $alias = implode('/', $aliases);
  193. $item[$this->alias]['allowed'] = $Acl->check($aro_alias, $alias);
  194. $item[$this->alias]['full_alias'] = $alias;
  195. $next = $this->generateListPerms($Acl, $aro_alias, $aliases, $id);
  196. if ($next !== array()) {
  197. $item[$this->alias]['sub-menu'] = $next;
  198. }
  199. $ret[] = $item[$this->alias];
  200. }
  201. return $ret;
  202. }
  203. /**
  204. * Wipes then resets the ACO tree
  205. *
  206. * @return boolean ACO/ARO tree valid
  207. * @access public
  208. */
  209. public function reset()
  210. {
  211. $this->completeTreeSave($this->getCompleteTree());
  212. $Aro = ClassRegistry::init('Permissible.PermissibleAro');
  213. $aro = $Aro->find('first', array(
  214. 'conditions' => array(
  215. 'PermissibleAro.parent_id' => null,
  216. 'PermissibleAro.alias' => 'everyone'
  217. )
  218. ));
  219. return ($aro !== false);
  220. }
  221. /**
  222. * Refreshes the ACO tree
  223. *
  224. * @return null
  225. * @access public
  226. */
  227. public function refresh()
  228. {
  229. $this->completeTreeUpdate($this->getCompleteTree());
  230. }
  231. /**
  232. * Gets a complete list of plugins/controllers/actions
  233. *
  234. * @return array List
  235. * @access public
  236. */
  237. public function getCompleteTree()
  238. {
  239. App::uses('Folder', 'Utility');
  240. $acos = array(
  241. 'app' => array()
  242. );
  243. $cont_fold = new Folder(APP . 'Controller' . DS);
  244. $controllers = $cont_fold->read();
  245. $app_cont = new AppController();
  246. $app_cont = get_class_methods($app_cont);
  247. foreach ($controllers[1] as $file) {
  248. if (substr($file, -14) === 'Controller.php') {
  249. $cont = Inflector::camelize(substr($file, 0, -4));
  250. $controllerName = substr($cont, 0, -10);
  251. if ($controllerName === 'App') {
  252. continue;
  253. } else {
  254. if (App::import('Controller', $controllerName)) {
  255. $cont_clas = new $cont();
  256. $methods = array_diff(get_class_methods($cont_clas), $app_cont);
  257. foreach ($methods as $key => $method) {
  258. if (substr($method, 0, 1) === '_') {
  259. unset($methods[$key]);
  260. }
  261. }
  262. sort($methods);
  263. $acos['app'][Inflector::camelize(substr($cont, 0, -10))] = array_flip($methods);
  264. }
  265. }
  266. }
  267. }
  268. foreach (App::objects('plugin') as $plugin) {
  269. $folder = new Folder(App::pluginPath($plugin) . 'Controller/');
  270. $controllers = $folder->read();
  271. if ($controllers[1] !== array() && App::import('Controller', $plugin . '.' . $plugin . 'App')) {
  272. $plug_cont = $plugin . 'AppController';
  273. $plug_cont = new $plug_cont();
  274. $plug_cont = get_class_methods($plug_cont);
  275. foreach ($controllers[1] as $file) {
  276. if (substr($file, -14) === 'Controller.php') {
  277. $cont = Inflector::camelize(substr($file, 0, -4));
  278. if (App::import('Controller', $plugin . '.' . substr($cont, 0, -10))) {
  279. $cont_clas = new $cont();
  280. $methods = array_diff(get_class_methods($cont_clas), $plug_cont);
  281. foreach ($methods as $key => $method) {
  282. if (substr($method, 0, 1) === '_') {
  283. unset($methods[$key]);
  284. }
  285. }
  286. sort($methods);
  287. $acos['app'][Inflector::camelize($plugin)][Inflector::camelize(substr($cont, 0, -10))] = array_flip($methods);
  288. }
  289. }
  290. }
  291. }
  292. }
  293. return $acos;
  294. }
  295. }