UserController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. class UserController extends Zend_Controller_Action
  3. {
  4. public function init()
  5. {
  6. $ajaxContext = $this->_helper->getHelper('AjaxContext');
  7. $ajaxContext->addActionContext('get-hosts', 'json')
  8. ->addActionContext('get-user-data-table-info', 'json')
  9. ->addActionContext('get-user-data', 'json')
  10. ->addActionContext('remove-user', 'json')
  11. ->addActionContext('edit-user', 'json')
  12. ->initContext();
  13. }
  14. public function addUserAction()
  15. {
  16. $CC_CONFIG = Config::getConfig();
  17. $request = $this->getRequest();
  18. $baseUrl = Application_Common_OsPath::getBaseDir();
  19. $js_files = array(
  20. 'js/datatables/js/jquery.dataTables.js?',
  21. 'js/datatables/plugin/dataTables.pluginAPI.js?',
  22. 'js/airtime/user/user.js?'
  23. );
  24. foreach ($js_files as $js) {
  25. $this->view->headScript()->appendFile(
  26. $baseUrl.$js.$CC_CONFIG['airtime_version'],'text/javascript');
  27. }
  28. $this->view->headLink()->appendStylesheet($baseUrl.'css/users.css?'.$CC_CONFIG['airtime_version']);
  29. $form = new Application_Form_AddUser();
  30. $this->view->successMessage = "";
  31. if ($request->isPost()) {
  32. $params = $request->getPost();
  33. $postData = explode('&', $params['data']);
  34. $formData = array();
  35. foreach($postData as $k=>$v) {
  36. $v = explode('=', $v);
  37. $formData[$v[0]] = urldecode($v[1]);
  38. }
  39. if ($form->isValid($formData)) {
  40. if ($form->validateLogin($formData)) {
  41. $user = new Application_Model_User($formData['user_id']);
  42. if (empty($formData['user_id'])) {
  43. $user->setLogin($formData['login']);
  44. }
  45. $user->setFirstName($formData['first_name']);
  46. $user->setLastName($formData['last_name']);
  47. // We don't allow 6 x's as a password.
  48. // The reason is because we that as a password placeholder
  49. // on the client side.
  50. if ($formData['password'] != "xxxxxx") {
  51. $user->setPassword($formData['password']);
  52. }
  53. $user->setType($formData['type']);
  54. $user->setEmail($formData['email']);
  55. $user->setCellPhone($formData['cell_phone']);
  56. $user->setSkype($formData['skype']);
  57. $user->setJabber($formData['jabber']);
  58. $user->save();
  59. $form->reset();
  60. $this->view->form = $form;
  61. if (strlen($formData['user_id']) == 0) {
  62. $this->view->successMessage = "<div class='success'>"._("User added successfully!")."</div>";
  63. } else {
  64. $this->view->successMessage = "<div class='success'>"._("User updated successfully!")."</div>";
  65. }
  66. $this->_helper->json->sendJson(array("valid"=>"true", "html"=>$this->view->render('user/add-user.phtml')));
  67. } else {
  68. $this->view->form = $form;
  69. $this->_helper->json->sendJson(array("valid"=>"false", "html"=>$this->view->render('user/add-user.phtml')));
  70. }
  71. } else {
  72. $this->view->form = $form;
  73. $this->_helper->json->sendJson(array("valid"=>"false", "html"=>$this->view->render('user/add-user.phtml')));
  74. }
  75. }
  76. $this->view->form = $form;
  77. }
  78. public function getHostsAction()
  79. {
  80. $search = $this->_getParam('term');
  81. $this->view->hosts = Application_Model_User::getHosts($search);
  82. }
  83. public function getUserDataTableInfoAction()
  84. {
  85. $post = $this->getRequest()->getPost();
  86. $users = Application_Model_User::getUsersDataTablesInfo($post);
  87. $this->_helper->json->sendJson($users);
  88. }
  89. public function getUserDataAction()
  90. {
  91. $id = $this->_getParam('id');
  92. $this->view->entries = Application_Model_User::GetUserData($id);
  93. }
  94. public function editUserAction()
  95. {
  96. $request = $this->getRequest();
  97. $form = new Application_Form_EditUser();
  98. if ($request->isPost()) {
  99. $formData = $request->getPost();
  100. if ($form->isValid($formData) &&
  101. $form->validateLogin($formData['cu_login'], $formData['cu_user_id'])) {
  102. $user = new Application_Model_User($formData['cu_user_id']);
  103. $user->setFirstName($formData['cu_first_name']);
  104. $user->setLastName($formData['cu_last_name']);
  105. // We don't allow 6 x's as a password.
  106. // The reason is because we use that as a password placeholder
  107. // on the client side.
  108. if ($formData['cu_password'] != "xxxxxx") {
  109. $user->setPassword($formData['cu_password']);
  110. }
  111. $user->setEmail($formData['cu_email']);
  112. $user->setCellPhone($formData['cu_cell_phone']);
  113. $user->setSkype($formData['cu_skype']);
  114. $user->setJabber($formData['cu_jabber']);
  115. $user->save();
  116. Application_Model_Preference::SetUserLocale($formData['cu_locale']);
  117. Application_Model_Preference::SetUserTimezone($formData['cu_timezone']);
  118. //configure localization with new locale setting
  119. Application_Model_Locale::configureLocalization($formData['cu_locale']);
  120. //reinitialize form so language gets translated
  121. $form = new Application_Form_EditUser();
  122. $this->view->successMessage = "<div class='success'>"._("Settings updated successfully!")."</div>";
  123. }
  124. $this->view->form = $form;
  125. $this->view->html = $this->view->render('user/edit-user.phtml');
  126. }
  127. $this->view->form = $form;
  128. $this->view->html = $this->view->render('user/edit-user.phtml');
  129. }
  130. public function removeUserAction()
  131. {
  132. // action body
  133. $delId = $this->_getParam('id');
  134. $valid_actions = array("delete_cascade", "reassign_to");
  135. $files_action = $this->_getParam('deleted_files');
  136. # TODO : remove this. we only use default for now not to break the UI.
  137. if (!$files_action) { # set default action
  138. $files_action = "reassign_to";
  139. $new_owner = Application_Model_User::getFirstAdmin();
  140. }
  141. # only delete when valid action is selected for the owned files
  142. if (! in_array($files_action, $valid_actions) ) {
  143. return;
  144. }
  145. $userInfo = Zend_Auth::getInstance()->getStorage()->read();
  146. $userId = $userInfo->id;
  147. # Don't let users delete themselves
  148. if ($delId == $userId) {
  149. return;
  150. }
  151. $user = new Application_Model_User($delId);
  152. # Take care of the user's files by either assigning them to somebody
  153. # or deleting them all
  154. if ($files_action == "delete_cascade") {
  155. $user->deleteAllFiles();
  156. } elseif ($files_action == "reassign_to") {
  157. // TODO : fix code to actually use the line below and pick a
  158. // real owner instead of defaulting to the first found admin
  159. //$new_owner_id = $this->_getParam("new_owner");
  160. //$new_owner = new Application_Model_User($new_owner_id);
  161. $user->donateFilesTo( $new_owner );
  162. Logging::info("Reassign to user {$new_owner->getDbId()}");
  163. }
  164. # Finally delete the user
  165. $this->view->entries = $user->delete();
  166. }
  167. }