PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/application/modules/admin/controllers/UsersController.php

https://bitbucket.org/philkershaw/zend-framework-1.11-acl-implementation
PHP | 189 lines | 134 code | 4 blank | 51 comment | 9 complexity | 344492707910df8f5093502e31358b9b MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * Users Controller
  4. * Provisiona the adding, editing, deletion and general modification to users and their full profiles
  5. *
  6. * @author Phil Kershaw
  7. */
  8. class Admin_UsersController extends Zend_Controller_Action
  9. {
  10. protected $_redirector = null;
  11. /**
  12. * User table object
  13. * Set up by the constructor to avoid having to perform instanciation within ever action
  14. *
  15. * @var $userTbl;
  16. */
  17. public $userTbl;
  18. /**
  19. * Init Method
  20. * Creates and instance of the DbTable Users Object and stores it in $userTbl class variable
  21. */
  22. public function init()
  23. {
  24. $this->userTbl = new Auth_Model_DbTable_Users();
  25. $this->_redirector = $this->_helper->getHelper('Redirector');
  26. $this->view->message = $this->_helper->flashMessenger->getMessages();
  27. }
  28. /**
  29. * Index Action
  30. * Fetches all users for displaying in a list
  31. */
  32. public function indexAction()
  33. {
  34. $this->view->users = $this->userTbl->getAll();
  35. }
  36. /**
  37. * Add Action
  38. * Invoked via ajax inserts a new user into the Db and redirects to the view for detail entry
  39. *
  40. * @param array $_POST
  41. */
  42. public function addAction()
  43. {
  44. $this->_helper->layout()->disableLayout();
  45. $this->_helper->viewRenderer->setNoRender(true);
  46. if ($this->_request->isXmlHttpRequest())
  47. {
  48. $post = $this->_request->getPost();
  49. $salt = Zend_Registry::get('salt');
  50. $password = substr(md5(uniqid($salt, true)), 0, 8);
  51. $post['password'] = sha1($password.$salt);
  52. try
  53. {
  54. if ($this->userTbl->add($post))
  55. {
  56. $this->_helper->flashMessenger->addMessage(array('type' => 'success', 'message' => "User created successfully. Please enter more detail for this user below."));
  57. $insertId = $this->userTbl->getAdapter()->lastInsertId();
  58. echo "/admin/user/{$insertId}";
  59. }
  60. else
  61. {
  62. $this->_helper->flashMessenger->addMessage(array('type' => 'error', 'message' => "User already exists."));
  63. echo "/admin/users";
  64. }
  65. }
  66. catch (Exception $e)
  67. {
  68. throw new Exception("Failed to add user: {$e->getMessage()}");
  69. }
  70. }
  71. }
  72. /**
  73. * View Action
  74. * Fetches a single user for displaying
  75. *
  76. * @param int $id
  77. */
  78. public function viewAction()
  79. {
  80. $this->view->user = $this->userTbl->get($this->_request->getParam('id'));
  81. }
  82. /**
  83. * Update
  84. * Updates a users record and is invoked via an ajax callback - thus doesn't require a view
  85. *
  86. * @param int $id
  87. * @param array $_POST
  88. *
  89. * @return mixed
  90. */
  91. public function updateAction()
  92. {
  93. $this->_helper->layout()->disableLayout();
  94. $this->_helper->viewRenderer->setNoRender(true);
  95. if ($this->_request->isXmlHttpRequest())
  96. {
  97. $id = $this->_request->getParam('id');
  98. $post = $this->_request->getPost();
  99. try
  100. {
  101. if ($this->userTbl->updateUser($id, $post))
  102. {
  103. echo 'User updated successfully';
  104. }
  105. else
  106. {
  107. echo 'No changes detected, user was not updated';
  108. }
  109. }
  110. catch (Exception $e)
  111. {
  112. throw new Exception("Failed to update user: {$e->getMessage()}");
  113. }
  114. }
  115. }
  116. /**
  117. * Permissions
  118. * Displays a form for managing user permissions to resources
  119. */
  120. public function permissionsAction()
  121. {
  122. if ($this->_request->isPost())
  123. {
  124. switch ($this->_request->getParam('crud'))
  125. {
  126. case 'update':
  127. $post = $this->_request->getPost();
  128. unset($post['save']);
  129. $permissionsTbl = new Auth_Model_DbTable_Permissions();
  130. try
  131. {
  132. $permissionsTbl->savePermissions($post);
  133. $this->_helper->flashMessenger->addMessage(array('type' => 'success', 'message' => "Permissions updated successfully."));
  134. }
  135. catch (Exception $e)
  136. {
  137. $this->_helper->flashMessenger->addMessage(array('type' => 'error', 'message' => "There was a problem saving the permissions. The following error occurred {$e->getMessage()}"));
  138. }
  139. $this->_redirector->gotoUrl('/admin/permissions');
  140. break;
  141. default:
  142. $user = $_POST['users'];
  143. break;
  144. }
  145. }
  146. $userForm = new Admin_Form_Users(array(
  147. 'action' => '/admin/permissions',
  148. 'method' => 'post',
  149. ));
  150. $userTbl = new Auth_Model_DbTable_Users();
  151. $users = $this->reformatArray($userTbl->getAllUsers());
  152. if (!isset($user))
  153. {
  154. $userID = key($users);
  155. }
  156. else
  157. {
  158. $userID = $user;
  159. }
  160. $userSelector = $userForm->getElement('users');
  161. $userSelector->setValue($userID)
  162. ->setMultiOptions($users);
  163. $permissionsForm = new Admin_Form_Permissions($userID, array(
  164. 'action' => '/admin/permissions/update',
  165. 'method' => 'post',
  166. ));
  167. $this->view->form = $permissionsForm;
  168. $this->view->users = $userForm;
  169. }
  170. /**
  171. * Reformat Array
  172. * Arranges the array in a format ready for use with a Zend Select element
  173. *
  174. * @return array reformatted data
  175. */
  176. private static function reformatArray(Array $users)
  177. {
  178. $newArray = array();
  179. foreach ($users as $user)
  180. {
  181. $newArray[$user['id']] = $user['username'];
  182. }
  183. return $newArray;
  184. }
  185. }