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

/Acl/Controller/AclActionsController.php

https://github.com/kareypowell/croogo
PHP | 188 lines | 114 code | 22 blank | 52 comment | 31 complexity | be0e32ae01749df87d8ad07437119fa1 MD5 | raw file
  1. <?php
  2. App::uses('AclAppController', 'Acl.Controller');
  3. /**
  4. * AclActions 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 AclActionsController extends AclAppController {
  14. /**
  15. * name
  16. *
  17. * @var string
  18. */
  19. public $name = 'AclActions';
  20. /**
  21. * uses
  22. *
  23. * @var array
  24. */
  25. public $uses = array('Acl.AclAco');
  26. /**
  27. * beforeFilter
  28. *
  29. * @return void
  30. */
  31. public function beforeFilter() {
  32. parent::beforeFilter();
  33. if ($this->action == 'admin_generate') {
  34. $this->Security->csrfCheck = false;
  35. }
  36. }
  37. /**
  38. * admin_index
  39. */
  40. public function admin_index($id = null) {
  41. $this->set('title_for_layout', __d('croogo', 'Actions'));
  42. if ($id == null) {
  43. $root = $this->Acl->Aco->node('controllers');
  44. $root = $root[0];
  45. } else {
  46. $root = $this->Acl->Aco->read(null, $id);
  47. }
  48. $acos = $this->AclAco->getChildren($root['Aco']['id']);
  49. $this->set(compact('acos'));
  50. }
  51. /**
  52. * admin_add
  53. */
  54. public function admin_add() {
  55. $this->set('title_for_layout', __d('croogo', 'Add Action'));
  56. if (!empty($this->request->data)) {
  57. $this->Acl->Aco->create();
  58. // if parent_id is null, assign 'controllers' as parent
  59. if ($this->request->data['Aco']['parent_id'] == null) {
  60. $this->request->data['Aco']['parent_id'] = 1;
  61. $acoType = 'Controller';
  62. } else {
  63. $acoType = 'Action';
  64. }
  65. if ($this->Acl->Aco->save($this->request->data['Aco'])) {
  66. $this->Session->setFlash(sprintf(__d('croogo', 'The %s has been saved'), $acoType), 'default', array('class' => 'success'));
  67. return $this->Croogo->redirect(array('action' => 'edit', $this->Acl->Aco->id));
  68. } else {
  69. $this->Session->setFlash(sprintf(__d('croogo', 'The %s could not be saved. Please, try again.'), $acoType), 'default', array('class' => 'error'));
  70. }
  71. }
  72. $acos = $this->Acl->Aco->generateTreeList(null, '{n}.Aco.id', '{n}.Aco.alias');
  73. $this->set(compact('acos'));
  74. }
  75. /**
  76. * admin_edit
  77. *
  78. * @param integer $id
  79. */
  80. public function admin_edit($id = null) {
  81. $this->set('title_for_layout', __d('croogo', 'Edit Action'));
  82. if (!$id && empty($this->request->data)) {
  83. $this->Session->setFlash(__d('croogo', 'Invalid Action'), 'default', array('class' => 'error'));
  84. return $this->redirect(array('action' => 'index'));
  85. }
  86. if (!empty($this->request->data)) {
  87. if ($this->Acl->Aco->save($this->request->data['Aco'])) {
  88. $this->Session->setFlash(__d('croogo', 'The Action has been saved'), 'default', array('class' => 'success'));
  89. return $this->Croogo->redirect(array('action' => 'edit', $this->Acl->Aco->id));
  90. } else {
  91. $this->Session->setFlash(__d('croogo', 'The Action could not be saved. Please, try again.'), 'default', array('class' => 'error'));
  92. }
  93. }
  94. if (empty($this->request->data)) {
  95. $this->request->data = $this->Acl->Aco->read(null, $id);
  96. }
  97. $acos = $this->Acl->Aco->generateTreeList(null, '{n}.Aco.id', '{n}.Aco.alias');
  98. $this->set(compact('acos'));
  99. }
  100. /**
  101. * admin_delete
  102. *
  103. * @param integer $id
  104. */
  105. public function admin_delete($id = null) {
  106. if (!$id) {
  107. $this->Session->setFlash(__d('croogo', 'Invalid id for Action'), 'default', array('class' => 'error'));
  108. return $this->redirect(array('action' => 'index'));
  109. }
  110. if ($this->Acl->Aco->delete($id)) {
  111. $this->Session->setFlash(__d('croogo', 'Action deleted'), 'default', array('class' => 'success'));
  112. return $this->redirect(array('action' => 'index'));
  113. }
  114. }
  115. /**
  116. * admin_move
  117. *
  118. * @param integer $id
  119. * @param string $direction
  120. * @param string $step
  121. */
  122. public function admin_move($id, $direction = 'up', $step = '1') {
  123. if (!$id) {
  124. $this->Session->setFlash(__d('croogo', 'Invalid id for Action'), 'default', array('class' => 'error'));
  125. return $this->redirect(array('action' => 'index'));
  126. }
  127. if ($direction == 'up') {
  128. if ($this->Acl->Aco->moveUp($id)) {
  129. $this->Session->setFlash(__d('croogo', 'Action moved up'), 'default', array('class' => 'success'));
  130. return $this->redirect(array('action' => 'index'));
  131. }
  132. } else {
  133. if ($this->Acl->Aco->moveDown($id)) {
  134. $this->Session->setFlash(__d('croogo', 'Action moved down'), 'default', array('class' => 'success'));
  135. return $this->redirect(array('action' => 'index'));
  136. }
  137. }
  138. }
  139. /**
  140. * admin_generate
  141. */
  142. public function admin_generate() {
  143. App::uses('AclExtras', 'Acl.Lib');
  144. $AclExtras = new AclExtras();
  145. $AclExtras->startup($this);
  146. if (isset($this->request->named['sync'])) {
  147. $result = $AclExtras->aco_sync();
  148. } else {
  149. $result = $AclExtras->aco_update();
  150. }
  151. $output = $AclExtras->output;
  152. $output += $AclExtras->errors;
  153. if ($result) {
  154. $class = 'success';
  155. $output[] = __d('croogo', 'Created %d new permissions', $AclExtras->created);
  156. } else {
  157. $class = 'error';
  158. }
  159. $this->Session->setFlash(join('<br>', $output), 'default', array('class' => $class));
  160. if (isset($this->request->params['named']['permissions'])) {
  161. return $this->redirect(array('plugin' => 'acl', 'controller' => 'acl_permissions', 'action' => 'index'));
  162. } else {
  163. return $this->redirect(array('action' => 'index'));
  164. }
  165. }
  166. }