USER.class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. /**
  3. * USERS
  4. *
  5. * @author vesta, http://vestacp.com/
  6. * @author Dmitry Malishev <dima.malishev@gmail.com>
  7. * @author Dmitry Naumov-Socolov <naumov.socolov@gmail.com>
  8. * @copyright vesta 2010-2011
  9. */
  10. class USER extends AjaxHandler
  11. {
  12. /**
  13. * Get USER entries
  14. *
  15. * @param Request $request
  16. * @return string - Ajax Reply
  17. */
  18. public function getListExecute(Request $request)
  19. {
  20. if(!VestaSession::getUserRole()){
  21. return self::getUserExecute($request);
  22. }
  23. $reply = array();
  24. $result = Vesta::execute(Vesta::V_LIST_SYS_USERS, array(Config::get('response_type')));
  25. foreach ($result['data'] as $user => $details) {
  26. $nses = $this->getNS($user, $details);
  27. $user_details = array(
  28. "FNAME" => $details['FNAME'],
  29. "LNAME" => $details['LNAME'],
  30. "LOGIN_NAME" => $user,
  31. "FULLNAME" => $details['FNAME'].' '.$details['LNAME'],
  32. "PACKAGE" => $details['PACKAGE'],
  33. "WEB_DOMAINS" => $details['WEB_DOMAINS'],
  34. "WEB_SSL" => $details['WEB_SSL'],
  35. "WEB_ALIASES" => $details['WEB_ALIASES'],
  36. "DATABASES" => $details['DATABASES'],
  37. "MAIL_DOMAINS" => $details['MAIL_DOMAINS'],
  38. "MAIL_BOXES" => $details['MAIL_BOXES'],
  39. "MAIL_FORWARDERS" => $details['MAIL_FORWARDERS'],
  40. "DNS_DOMAINS" => $details['DNS_DOMAINS'],
  41. "DISK_QUOTA" => $details['DISK_QUOTA'],
  42. "BANDWIDTH" => $details['BANDWIDTH'],
  43. "SHELL" => $details['SHELL'],
  44. "BACKUPS" => $details['BACKUPS'],
  45. "WEB_TPL" => $details['WEB_TPL'],
  46. "SUSPEND" => $details['SUSPENDED'],
  47. "CONTACT" => $details['CONTACT'],
  48. "REPORTS" => $details['REPORTS'],
  49. "REPORTS_ENABLED" => $details['REPORTS'],
  50. "IP_OWNED" => $details['IP_OWNED'],
  51. "U_DIR_DISK" => $details['U_DIR_DISK'],
  52. "U_DISK" => $details['U_DISK'],
  53. "U_BANDWIDTH" => $details['U_BANDWIDTH'],
  54. "U_WEB_DOMAINS" => $details['U_WEB_DOMAINS'],
  55. "U_WEB_SSL" => $details['U_WEB_SSL'],
  56. "U_DNS_DOMAINS" => $details['U_DNS_DOMAINS'],
  57. "U_DATABASES" => $details['U_DATABASES'],
  58. "U_MAIL_DOMAINS" => $details['U_MAIL_DOMAINS'],
  59. "DATE" => $details['DATE'],
  60. "U_MAIL_FORWARDERS" => '0',
  61. "U_MAIL_BOXES" => '0',
  62. "U_CRON_JOBS" => $details['U_CRON_JOBS'],
  63. "IP_OWNED" => $details['IP_OWNED'],
  64. "NGINX_EXT" => $details['"NGINX_EXT']
  65. );
  66. $reply[$user] = array_merge($user_details, $nses);
  67. // $reply[$user] = $user_details;
  68. }
  69. return $this->reply(TRUE, $reply);
  70. }
  71. public function getUserExecute(Request $request)
  72. {
  73. $user = $this->getLoggedUser();
  74. $reply = array();
  75. $result = Vesta::execute(Vesta::V_LIST_SYS_USER, array($user['uid'], Config::get('response_type')));
  76. foreach ($result['data'] as $user => $details) {
  77. $user_details = array(
  78. "FNAME" => $details['FNAME'],
  79. "LNAME" => $details['LNAME'],
  80. "LOGIN_NAME" => $user,
  81. "FULLNAME" => $details['FNAME'].' '.$details['LNAME'],
  82. "PACKAGE" => $details['PACKAGE'],
  83. "WEB_DOMAINS" => $details['WEB_DOMAINS'],
  84. "WEB_SSL" => $details['WEB_SSL'],
  85. "WEB_ALIASES" => $details['WEB_ALIASES'],
  86. "DATABASES" => $details['DATABASES'],
  87. "MAIL_DOMAINS" => $details['MAIL_DOMAINS'],
  88. "MAIL_BOXES" => $details['MAIL_BOXES'],
  89. "MAIL_FORWARDERS" => $details['MAIL_FORWARDERS'],
  90. "DNS_DOMAINS" => $details['DNS_DOMAINS'],
  91. "DISK_QUOTA" => $details['DISK_QUOTA'],
  92. "BANDWIDTH" => $details['BANDWIDTH'],
  93. "NS" => $details['NS'],
  94. "SHELL" => $details['SHELL'],
  95. "BACKUPS" => $details['BACKUPS'],
  96. "WEB_TPL" => $details['WEB_TPL'],
  97. "SUSPEND" => $details['SUSPENDED'],
  98. "CONTACT" => $details['CONTACT'],
  99. "REPORTS" => $details['REPORTS'],
  100. "IP_OWNED" => $details['IP_OWNED'],
  101. "U_DIR_DISK" => $details['U_DIR_DISK'],
  102. "U_DISK" => $details['U_DISK'],
  103. "U_BANDWIDTH" => $details['U_BANDWIDTH'],
  104. "U_WEB_DOMAINS" => $details['U_WEB_DOMAINS'],
  105. "U_WEB_SSL" => $details['U_WEB_SSL'],
  106. "U_DNS_DOMAINS" => $details['U_DNS_DOMAINS'],
  107. "U_DATABASES" => $details['U_DATABASES'],
  108. "U_MAIL_DOMAINS" => $details['U_MAIL_DOMAINS'],
  109. "U_CRON_JOBS" => 'todo',
  110. "IP_OWNED" => $details['IP_OWNED'],
  111. "DATE" => $details['DATE']
  112. );
  113. $reply[$user] = $user_details;
  114. }
  115. return $this->reply(TRUE, $reply);
  116. }
  117. /**
  118. * Add USER entry
  119. *
  120. * @param Request $request
  121. * @return string - Ajax Reply
  122. */
  123. public function addExecute(Request $request)
  124. {
  125. $_s = $request->getParameter('spell');
  126. $user = $this->getLoggedUser();
  127. $params = array(
  128. 'USER' => $_s['LOGIN_NAME'],
  129. 'PASSWORD' => $_s['PASSWORD'],
  130. 'EMAIL' => $_s['CONTACT'],
  131. 'PACKAGE' => $_s['PACKAGE'],
  132. 'FNAME' => $_s['FNAME'],
  133. 'LNAME' => $_s['LNAME']
  134. );
  135. $result = Vesta::execute(Vesta::V_ADD_SYS_USER, $params);
  136. // Reports
  137. // $enable_reports = Utils::getCheckboxBooleanValue($spell['REPORTS_ENABLED']);
  138. // $reports_result = $this->setUserReports($spell['LOGIN_NAME'], $spell['REPORTS_ENABLED']);
  139. // Set SHELL
  140. // $this->setShell($_s['LOGIN_NAME'], $_s['SHELL']);
  141. if (!$result['status']) {
  142. $this->errors[] = array($result['error_code'] => $result['error_message']);
  143. }
  144. if(@Utils::getCheckboxBooleanValue($_s['REPORTS_ENABLED'])){
  145. $result = Vesta::execute(Vesta::V_ADD_SYS_USER_REPORTS, array('USER' => $_USER));
  146. if (!$result['status']) {
  147. $this->status = FALSE;
  148. $this->errors['REPORTS'] = array($result['error_code'] => $result['error_message']);
  149. }
  150. }
  151. if ($_s['SUSPEND'] == 'on') {
  152. if($result['status']){
  153. $result = array();
  154. $result = Vesta::execute(Vesta::V_SUSPEND_SYS_USER, array('USER' => $user['uid'], 'USER' => $_s['LOGIN_NAME']));
  155. if (!$result['status']) {
  156. $this->status = FALSE;
  157. $this->errors['SUSPEND'] = array($result['error_code'] => $result['error_message']);
  158. }
  159. }
  160. }
  161. return $this->reply($result['status'], $result['data']);
  162. }
  163. /**
  164. * Delete USER entry
  165. *
  166. * @param Request $request
  167. * @return string - Ajax Reply
  168. */
  169. public function deleteExecute(Request $request)
  170. {
  171. $user = $this->getLoggedUser();
  172. $spell = $request->getParameter('spell');
  173. $params = array(
  174. 'USER' => $spell['LOGIN_NAME']
  175. );
  176. $result = Vesta::execute(Vesta::V_DEL_SYS_USER, $params);
  177. if (!$result['status']) {
  178. $this->errors[] = array($result['error_code'] => $result['error_message']);
  179. }
  180. return $this->reply($result['status'], $result['data']);
  181. }
  182. /**
  183. * Change USER entry
  184. *
  185. * @param Request $request
  186. * @return string - Ajax Reply
  187. */
  188. public function changeExecute(Request $request)
  189. {
  190. $_new = $request->getParameter('new');
  191. $_old = $request->getParameter('old');
  192. $_USER = $_old['LOGIN_NAME'];
  193. $result = array();
  194. if(@Utils::getCheckboxBooleanValue($_new['SUSPEND'])){
  195. $result = Vesta::execute(Vesta::V_SUSPEND_SYS_USER, array('USER' => $_USER));
  196. return $this->reply($result['status'], $result['error_message']);
  197. }
  198. elseif(@Utils::getCheckboxBooleanValue($_old['SUSPEND'])){
  199. $result = Vesta::execute(Vesta::V_UNSUSPEND_SYS_USER, array('USER' => $_USER));
  200. if (!$result['status']) {
  201. $this->status = FALSE;
  202. $this->errors['UNSUSPEND'] = array($result['error_code'] => $result['error_message']);
  203. return $this->reply($result['status'], $result['error_message']);
  204. }
  205. }
  206. $reports_result = $this->setUserReports($_USER, $_new['REPORTS_ENABLED']);
  207. if (!empty($_new['PASSWORD']) && $_new['PASSWORD'] != Vesta::SAME_PASSWORD) {
  208. $result = array();
  209. $result = Vesta::execute(Vesta::V_CHANGE_SYS_USER_PASSWORD, array('USER' => $_USER, 'PASSWORD' => $_new['PASSWORD']));
  210. if (!$result['status']) {
  211. $this->status = FALSE;
  212. $this->errors['PASSWORD'] = array($result['error_code'] => $result['error_message']);
  213. }
  214. }
  215. if ($_old['PACKAGE'] != $_new['PACKAGE']) {
  216. $result = array();
  217. $result = Vesta::execute(Vesta::V_CHANGE_SYS_USER_PACKAGE, array('USER' => $_USER, 'PACKAGE' => $_new['PACKAGE']));
  218. if (!$result['status']) {
  219. $this->status = FALSE;
  220. $this->errors['PACKAGE'] = array($result['error_code'] => $result['error_message']);
  221. }
  222. }
  223. if ($_old['CONTACT'] != $_new['CONTACT']) {
  224. $result = array();
  225. $result = Vesta::execute(Vesta::V_CHANGE_SYS_USER_CONTACT, array('USER' => $_USER, 'EMAIL' => $_new['CONTACT']));
  226. if (!$result['status']) {
  227. $this->status = FALSE;
  228. $this->errors['EMAIL'] = array($result['error_code'] => $result['error_message']);
  229. }
  230. }
  231. if ($_old['REPORTS_ENABLED'] != $_new['REPORTS_ENABLED']) {
  232. $result = array();
  233. if(@Utils::getCheckboxBooleanValue($_new['REPORTS_ENABLED'])){
  234. $result = Vesta::execute(Vesta::V_ADD_SYS_USER_REPORTS, array('USER' => $_USER));
  235. }
  236. else{
  237. $result = Vesta::execute(Vesta::V_ADD_SYS_USER_REPORTS, array('USER' => $_USER));
  238. }
  239. if (!$result['status']) {
  240. $this->status = FALSE;
  241. $this->errors['REPORTS'] = array($result['error_code'] => $result['error_message']);
  242. }
  243. }
  244. // Set SHELL
  245. $this->setShell($_USER, $_new['SHELL']);
  246. $this->setNSentries($_USER, $_new);
  247. $names = array(
  248. 'USER' => $_USER,
  249. 'FNAME' => $_new['FNAME'],
  250. 'LNAME' => $_new['LNAME']
  251. );
  252. $result = Vesta::execute(Vesta::V_CHANGE_SYS_USER_NAME, $names);
  253. if (!$result['status']) {
  254. $this->status = FALSE;
  255. $this->errors['NAMES'] = array($result['error_code'] => $result['error_message']);
  256. }
  257. if (!$this->status) {
  258. Vesta::execute(Vesta::V_CHANGE_SYS_USER_PASSWORD, array('USER' => $_USER, 'PASSWORD' => $_old['PASSWORD']));
  259. Vesta::execute(Vesta::V_CHANGE_SYS_USER_PACKAGE, array('USER' => $_USER, 'PACKAGE' => $_old['PACKAGE']));
  260. Vesta::execute(Vesta::V_CHANGE_SYS_USER_CONTACT, array('USER' => $_USER, 'EMAIL' => $_old['EMAIL']));
  261. Vesta::execute(Vesta::V_CHANGE_SYS_USER_NS, array('USER' => $_USER, 'NS1' => $_old['NS1'], 'NS2' => $_old['NS2']));
  262. Vesta::execute(Vesta::V_CHANGE_SYS_USER_SHELL, array('USER' => $_USER, 'SHELL' => $_old['SHELL']));
  263. }
  264. return $this->reply($this->status, '');
  265. }
  266. protected function setUserReports($user, $enabled)
  267. {
  268. if ($enabled == 'off') {
  269. $result = Vesta::execute(Vesta::V_DEL_SYS_USER_REPORTS, array('USER' => $user));
  270. }
  271. else {
  272. $result = Vesta::execute(Vesta::V_ADD_SYS_USER_REPORTS, array('USER' => $user));
  273. }
  274. return $result['status'];
  275. }
  276. protected function setNSentries($user, $data)
  277. {
  278. $ns = array();
  279. $ns['USER'] = $user;
  280. $ns['NS1'] = $data['NS1'];
  281. $ns['NS2'] = $data['NS2'];
  282. $ns['NS3'] = isset($data['NS3']) ? $data['NS3'] : '';
  283. $ns['NS4'] = isset($data['NS4']) ? $data['NS4'] : '';
  284. $ns['NS5'] = isset($data['NS5']) ? $data['NS5'] : '';
  285. $ns['NS6'] = isset($data['NS6']) ? $data['NS6'] : '';
  286. $ns['NS7'] = isset($data['NS7']) ? $data['NS7'] : '';
  287. $ns['NS8'] = isset($data['NS8']) ? $data['NS8'] : '';
  288. $result = Vesta::execute(Vesta::V_CHANGE_SYS_USER_NS, $ns);
  289. return $result['status'];
  290. }
  291. protected function getNS($user, $data)
  292. {
  293. $result = array();
  294. $ns_str = $data['NS'];
  295. $ns_list = explode(',', $ns_str);
  296. foreach (range(0, 7) as $index) {
  297. $result['NS'.($index + 1)] = @trim(@$ns_list[$index]);
  298. }
  299. return $result;
  300. }
  301. /**
  302. * TODO: handle result set errors
  303. */
  304. protected function setShell($user, $shell)
  305. {
  306. $result = Vesta::execute(Vesta::V_CHANGE_SYS_USER_SHELL, array('USER' => $user, 'SHELL' => $shell));
  307. }
  308. public function massiveSuspendExecute(Request $request)
  309. {
  310. $_entities = $request->getParameter('entities');
  311. foreach($_entities as $entity){
  312. $result = Vesta::execute(Vesta::V_SUSPEND_SYS_USER, array('USER' => $entity['LOGIN_NAME']));
  313. }
  314. return $this->reply($result['status'], $result['data']);
  315. }
  316. public function massiveUnsuspendExecute(Request $request)
  317. {
  318. $_entities = $request->getParameter('entities');
  319. foreach($_entities as $entity){
  320. $result = Vesta::execute(Vesta::V_UNSUSPEND_SYS_USER, array('USER' => $entity['LOGIN_NAME']));
  321. }
  322. return $this->reply($result['status'], $result['data']);
  323. }
  324. public function massiveDeleteExecute(Request $request)
  325. {
  326. $_entities = $request->getParameter('entities');
  327. foreach($_entities as $entity){
  328. $result = Vesta::execute(Vesta::V_DEL_SYS_USER, array('USER' => $entity['LOGIN_NAME']));
  329. }
  330. return $this->reply($result['status'], $result['data']);
  331. }
  332. public function loginAsExecute(Request $request)
  333. {
  334. $_user = $request->getParameter('user');
  335. if(Vesta::hasRights(VestaSession::getInstance()->getUserRole(), 'login_as'))
  336. {
  337. VestaSession::loginAs($_user);
  338. return $this->reply(TRUE, '');
  339. }
  340. return $this->reply(FALSE, '');
  341. }
  342. public function logoutAsExecute(Request $request)
  343. {
  344. VestaSession::logoutAs();
  345. return $this->reply(TRUE, '');
  346. }
  347. }