PageRenderTime 24ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/docroot/pgadmin/users.php

https://github.com/mterenzio/FollowThis
PHP | 379 lines | 297 code | 45 blank | 37 comment | 60 complexity | 64b7c62ff0be4c74efbe984bee4bb340 MD5 | raw file
  1. <?php
  2. /**
  3. * Manage users in a database cluster
  4. *
  5. * $Id: users.php,v 1.40 2008/02/25 17:20:44 xzilla Exp $
  6. */
  7. // Include application functions
  8. include_once('./libraries/lib.inc.php');
  9. $action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : '';
  10. if (!isset($msg)) $msg = '';
  11. /**
  12. * If a user is not a superuser, then we have an 'account management' page
  13. * where they can change their password, etc. We don't prevent them from
  14. * messing with the URL to gain access to other user admin stuff, because
  15. * the PostgreSQL permissions will prevent them changing anything anyway.
  16. */
  17. function doAccount($msg = '') {
  18. global $data, $misc;
  19. global $lang;
  20. $server_info = $misc->getServerInfo();
  21. $userdata = $data->getUser($server_info['username']);
  22. $_REQUEST['user'] = $server_info['username'];
  23. $misc->printTrail('user');
  24. $misc->printTabs('server','account');
  25. $misc->printMsg($msg);
  26. if ($userdata->recordCount() > 0) {
  27. $userdata->fields['usesuper'] = $data->phpBool($userdata->fields['usesuper']);
  28. $userdata->fields['usecreatedb'] = $data->phpBool($userdata->fields['usecreatedb']);
  29. echo "<table>\n";
  30. echo "<tr><th class=\"data\">{$lang['strusername']}</th><th class=\"data\">{$lang['strsuper']}</th><th class=\"data\">{$lang['strcreatedb']}</th><th class=\"data\">{$lang['strexpires']}</th>";
  31. echo "<th class=\"data\">{$lang['strsessiondefaults']}</th>";
  32. echo "</tr>\n";
  33. echo "<tr>\n\t<td class=\"data1\">", $misc->printVal($userdata->fields['usename']), "</td>\n";
  34. echo "\t<td class=\"data1\">", $misc->printVal($userdata->fields['usesuper'], 'yesno'), "</td>\n";
  35. echo "\t<td class=\"data1\">", $misc->printVal($userdata->fields['usecreatedb'], 'yesno'), "</td>\n";
  36. echo "\t<td class=\"data1\">", ($userdata->fields['useexpires'] == 'infinity' || is_null($userdata->fields['useexpires']) ? $lang['strnever'] : $misc->printVal($userdata->fields['useexpires'])), "</td>\n";
  37. echo "\t<td class=\"data1\">", $misc->printVal($userdata->fields['useconfig']), "</td>\n";
  38. echo "</tr>\n</table>\n";
  39. }
  40. else echo "<p>{$lang['strnodata']}</p>\n";
  41. echo "<p><a class=\"navlink\" href=\"users.php?action=confchangepassword&amp;{$misc->href}\">{$lang['strchangepassword']}</a></p>\n";
  42. }
  43. /**
  44. * Show confirmation of change password and actually change password
  45. */
  46. function doChangePassword($confirm, $msg = '') {
  47. global $data, $misc;
  48. global $lang, $conf;
  49. $server_info = $misc->getServerInfo();
  50. if ($confirm) {
  51. $_REQUEST['user'] = $server_info['username'];
  52. $misc->printTrail('user');
  53. $misc->printTitle($lang['strchangepassword'],'pg.user.alter');
  54. $misc->printMsg($msg);
  55. if (!isset($_POST['password'])) $_POST['password'] = '';
  56. if (!isset($_POST['confirm'])) $_POST['confirm'] = '';
  57. echo "<form action=\"users.php\" method=\"post\">\n";
  58. echo "<table>\n";
  59. echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strpassword']}</th>\n";
  60. echo "\t\t<td><input type=\"password\" name=\"password\" size=\"32\" value=\"",
  61. htmlspecialchars($_POST['password']), "\" /></td>\n\t</tr>\n";
  62. echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strconfirm']}</th>\n";
  63. echo "\t\t<td><input type=\"password\" name=\"confirm\" size=\"32\" value=\"\" /></td>\n\t</tr>\n";
  64. echo "</table>\n";
  65. echo "<p><input type=\"hidden\" name=\"action\" value=\"changepassword\" />\n";
  66. echo $misc->form;
  67. echo "<input type=\"submit\" name=\"ok\" value=\"{$lang['strok']}\" />\n";
  68. echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
  69. echo "</p></form>\n";
  70. }
  71. else {
  72. // Check that password is minimum length
  73. if (strlen($_POST['password']) < $conf['min_password_length'])
  74. doChangePassword(true, $lang['strpasswordshort']);
  75. // Check that password matches confirmation password
  76. elseif ($_POST['password'] != $_POST['confirm'])
  77. doChangePassword(true, $lang['strpasswordconfirm']);
  78. else {
  79. $status = $data->changePassword($server_info['username'],
  80. $_POST['password']);
  81. if ($status == 0)
  82. doAccount($lang['strpasswordchanged']);
  83. else
  84. doAccount($lang['strpasswordchangedbad']);
  85. }
  86. }
  87. }
  88. /**
  89. * Function to allow editing of a user
  90. */
  91. function doEdit($msg = '') {
  92. global $data, $misc;
  93. global $lang;
  94. $misc->printTrail('user');
  95. $misc->printTitle($lang['stralter'],'pg.user.alter');
  96. $misc->printMsg($msg);
  97. $userdata = $data->getUser($_REQUEST['username']);
  98. if ($userdata->recordCount() > 0) {
  99. $server_info = $misc->getServerInfo();
  100. $canRename = $data->hasUserRename() && ($_REQUEST['username'] != $server_info['username']);
  101. $userdata->fields['usesuper'] = $data->phpBool($userdata->fields['usesuper']);
  102. $userdata->fields['usecreatedb'] = $data->phpBool($userdata->fields['usecreatedb']);
  103. if (!isset($_POST['formExpires'])){
  104. if ($canRename) $_POST['newname'] = $userdata->fields['usename'];
  105. if ($userdata->fields['usesuper']) $_POST['formSuper'] = '';
  106. if ($userdata->fields['usecreatedb']) $_POST['formCreateDB'] = '';
  107. $_POST['formExpires'] = $userdata->fields['useexpires'] == 'infinity' ? '' : $userdata->fields['useexpires'];
  108. $_POST['formPassword'] = '';
  109. }
  110. echo "<form action=\"users.php\" method=\"post\">\n";
  111. echo "<table>\n";
  112. echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strusername']}</th>\n";
  113. echo "\t\t<td class=\"data1\">", ($canRename ? "<input name=\"newname\" size=\"15\" maxlength=\"{$data->_maxNameLen}\" value=\"" . htmlspecialchars($_POST['newname']) . "\" />" : $misc->printVal($userdata->fields['usename'])), "</td>\n\t</tr>\n";
  114. echo "\t<tr>\n\t\t<th class=\"data left\"><label for=\"formSuper\">{$lang['strsuper']}</label></th>\n";
  115. echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formSuper\" name=\"formSuper\"",
  116. (isset($_POST['formSuper'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n";
  117. echo "\t<tr>\n\t\t<th class=\"data left\"><label for=\"formCreateDB\">{$lang['strcreatedb']}</label></th>\n";
  118. echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formCreateDB\" name=\"formCreateDB\"",
  119. (isset($_POST['formCreateDB'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n";
  120. echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strexpires']}</th>\n";
  121. echo "\t\t<td class=\"data1\"><input size=\"16\" name=\"formExpires\" value=\"", htmlspecialchars($_POST['formExpires']), "\" /></td>\n\t</tr>\n";
  122. echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strpassword']}</th>\n";
  123. echo "\t\t<td class=\"data1\"><input type=\"password\" size=\"16\" name=\"formPassword\" value=\"", htmlspecialchars($_POST['formPassword']), "\" /></td>\n\t</tr>\n";
  124. echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strconfirm']}</th>\n";
  125. echo "\t\t<td class=\"data1\"><input type=\"password\" size=\"16\" name=\"formConfirm\" value=\"\" /></td>\n\t</tr>\n";
  126. echo "</table>\n";
  127. echo "<p><input type=\"hidden\" name=\"action\" value=\"save_edit\" />\n";
  128. echo "<input type=\"hidden\" name=\"username\" value=\"", htmlspecialchars($_REQUEST['username']), "\" />\n";
  129. echo $misc->form;
  130. echo "<input type=\"submit\" name=\"alter\" value=\"{$lang['stralter']}\" />\n";
  131. echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
  132. echo "</form>\n";
  133. }
  134. else echo "<p>{$lang['strnodata']}</p>\n";
  135. }
  136. /**
  137. * Function to save after editing a user
  138. */
  139. function doSaveEdit() {
  140. global $data, $lang;
  141. // Check name and password
  142. if (isset($_POST['newname']) && $_POST['newname'] == '')
  143. doEdit($lang['struserneedsname']);
  144. else if ($_POST['formPassword'] != $_POST['formConfirm'])
  145. doEdit($lang['strpasswordconfirm']);
  146. else {
  147. if (isset($_POST['newname'])) $status = $data->setRenameUser($_POST['username'], $_POST['formPassword'], isset($_POST['formCreateDB']), isset($_POST['formSuper']), $_POST['formExpires'], $_POST['newname']);
  148. else $status = $data->setUser($_POST['username'], $_POST['formPassword'], isset($_POST['formCreateDB']), isset($_POST['formSuper']), $_POST['formExpires']);
  149. if ($status == 0)
  150. doDefault($lang['struserupdated']);
  151. else
  152. doEdit($lang['struserupdatedbad']);
  153. }
  154. }
  155. /**
  156. * Show confirmation of drop and perform actual drop
  157. */
  158. function doDrop($confirm) {
  159. global $data, $misc;
  160. global $lang;
  161. if ($confirm) {
  162. $misc->printTrail('user');
  163. $misc->printTitle($lang['strdrop'],'pg.user.drop');
  164. echo "<p>", sprintf($lang['strconfdropuser'], $misc->printVal($_REQUEST['username'])), "</p>\n";
  165. echo "<form action=\"users.php\" method=\"post\">\n";
  166. echo "<p><input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
  167. echo "<input type=\"hidden\" name=\"username\" value=\"", htmlspecialchars($_REQUEST['username']), "\" />\n";
  168. echo $misc->form;
  169. echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n";
  170. echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
  171. echo "</form>\n";
  172. }
  173. else {
  174. $status = $data->dropUser($_REQUEST['username']);
  175. if ($status == 0)
  176. doDefault($lang['struserdropped']);
  177. else
  178. doDefault($lang['struserdroppedbad']);
  179. }
  180. }
  181. /**
  182. * Displays a screen where they can enter a new user
  183. */
  184. function doCreate($msg = '') {
  185. global $data, $misc, $username;
  186. global $lang;
  187. if (!isset($_POST['formUsername'])) $_POST['formUsername'] = '';
  188. if (!isset($_POST['formPassword'])) $_POST['formPassword'] = '';
  189. if (!isset($_POST['formConfirm'])) $_POST['formConfirm'] = '';
  190. if (!isset($_POST['formExpires'])) $_POST['formExpires'] = '';
  191. $misc->printTrail('server');
  192. $misc->printTitle($lang['strcreateuser'],'pg.user.create');
  193. $misc->printMsg($msg);
  194. echo "<form action=\"users.php\" method=\"post\">\n";
  195. echo "<table>\n";
  196. echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strusername']}</th>\n";
  197. echo "\t\t<td class=\"data1\"><input size=\"15\" maxlength=\"{$data->_maxNameLen}\" name=\"formUsername\" value=\"", htmlspecialchars($_POST['formUsername']), "\" /></td>\n\t</tr>\n";
  198. echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strpassword']}</th>\n";
  199. echo "\t\t<td class=\"data1\"><input size=\"15\" type=\"password\" name=\"formPassword\" value=\"", htmlspecialchars($_POST['formPassword']), "\" /></td>\n\t</tr>\n";
  200. echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strconfirm']}</th>\n";
  201. echo "\t\t<td class=\"data1\"><input size=\"15\" type=\"password\" name=\"formConfirm\" value=\"", htmlspecialchars($_POST['formConfirm']), "\" /></td>\n\t</tr>\n";
  202. echo "\t<tr>\n\t\t<th class=\"data left\"><label for=\"formSuper\">{$lang['strsuper']}</label></th>\n";
  203. echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formSuper\" name=\"formSuper\"",
  204. (isset($_POST['formSuper'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n";
  205. echo "\t<tr>\n\t\t<th class=\"data left\"><label for=\"formCreateDB\">{$lang['strcreatedb']}</label></th>\n";
  206. echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formCreateDB\" name=\"formCreateDB\"",
  207. (isset($_POST['formCreateDB'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n";
  208. echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strexpires']}</th>\n";
  209. echo "\t\t<td class=\"data1\"><input size=\"30\" name=\"formExpires\" value=\"", htmlspecialchars($_POST['formExpires']), "\" /></td>\n\t</tr>\n";
  210. echo "</table>\n";
  211. echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
  212. echo $misc->form;
  213. echo "<input type=\"submit\" name=\"create\" value=\"{$lang['strcreate']}\" />\n";
  214. echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
  215. echo "</form>\n";
  216. }
  217. /**
  218. * Actually creates the new user in the database
  219. */
  220. function doSaveCreate() {
  221. global $data;
  222. global $lang;
  223. // Check data
  224. if ($_POST['formUsername'] == '')
  225. doCreate($lang['struserneedsname']);
  226. else if ($_POST['formPassword'] != $_POST['formConfirm'])
  227. doCreate($lang['strpasswordconfirm']);
  228. else {
  229. $status = $data->createUser($_POST['formUsername'], $_POST['formPassword'],
  230. isset($_POST['formCreateDB']), isset($_POST['formSuper']), $_POST['formExpires'], array());
  231. if ($status == 0)
  232. doDefault($lang['strusercreated']);
  233. else
  234. doCreate($lang['strusercreatedbad']);
  235. }
  236. }
  237. /**
  238. * Show default list of users in the database
  239. */
  240. function doDefault($msg = '') {
  241. global $data, $misc;
  242. global $lang;
  243. function renderUseExpires($val) {
  244. global $lang;
  245. return $val == 'infinity' ? $lang['strnever'] : htmlspecialchars($val);
  246. }
  247. $misc->printTrail('server');
  248. $misc->printTabs('server','users');
  249. $misc->printMsg($msg);
  250. $users = $data->getUsers();
  251. $columns = array(
  252. 'user' => array(
  253. 'title' => $lang['strusername'],
  254. 'field' => field('usename'),
  255. ),
  256. 'superuser' => array(
  257. 'title' => $lang['strsuper'],
  258. 'field' => field('usesuper'),
  259. 'type' => 'yesno',
  260. ),
  261. 'createdb' => array(
  262. 'title' => $lang['strcreatedb'],
  263. 'field' => field('usecreatedb'),
  264. 'type' => 'yesno',
  265. ),
  266. 'expires' => array(
  267. 'title' => $lang['strexpires'],
  268. 'field' => field('useexpires'),
  269. 'type' => 'callback',
  270. 'params'=> array('function' => 'renderUseExpires', 'null' => $lang['strnever']),
  271. ),
  272. 'defaults' => array(
  273. 'title' => $lang['strsessiondefaults'],
  274. 'field' => field('useconfig'),
  275. ),
  276. 'actions' => array(
  277. 'title' => $lang['stractions'],
  278. ),
  279. );
  280. $actions = array(
  281. 'alter' => array(
  282. 'title' => $lang['stralter'],
  283. 'url' => "users.php?action=edit&amp;{$misc->href}&amp;",
  284. 'vars' => array('username' => 'usename'),
  285. ),
  286. 'drop' => array(
  287. 'title' => $lang['strdrop'],
  288. 'url' => "users.php?action=confirm_drop&amp;{$misc->href}&amp;",
  289. 'vars' => array('username' => 'usename'),
  290. ),
  291. );
  292. $misc->printTable($users, $columns, $actions, $lang['strnousers']);
  293. echo "<p><a class=\"navlink\" href=\"users.php?action=create&amp;{$misc->href}\">{$lang['strcreateuser']}</a></p>\n";
  294. }
  295. $misc->printHeader($lang['strusers']);
  296. $misc->printBody();
  297. switch ($action) {
  298. case 'changepassword':
  299. if (isset($_REQUEST['ok'])) doChangePassword(false);
  300. else doAccount();
  301. break;
  302. case 'confchangepassword':
  303. doChangePassword(true);
  304. break;
  305. case 'account':
  306. doAccount();
  307. break;
  308. case 'save_create':
  309. if (isset($_REQUEST['cancel'])) doDefault();
  310. else doSaveCreate();
  311. break;
  312. case 'create':
  313. doCreate();
  314. break;
  315. case 'drop':
  316. if (isset($_REQUEST['cancel'])) doDefault();
  317. else doDrop(false);
  318. break;
  319. case 'confirm_drop':
  320. doDrop(true);
  321. break;
  322. case 'save_edit':
  323. if (isset($_REQUEST['cancel'])) doDefault();
  324. else doSaveEdit();
  325. break;
  326. case 'edit':
  327. doEdit();
  328. break;
  329. default:
  330. doDefault();
  331. break;
  332. }
  333. $misc->printFooter();
  334. ?>