server_privileges.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /* vim: set expandtab sw=4 ts=4 sts=4: */
  2. /**
  3. * @fileoverview functions used in server privilege pages
  4. * @name Server Privileges
  5. *
  6. * @requires jQuery
  7. * @requires jQueryUI
  8. * @requires js/functions.js
  9. *
  10. */
  11. /**
  12. * Validates the "add a user" form
  13. *
  14. * @return boolean whether the form is validated or not
  15. */
  16. function checkAddUser(the_form)
  17. {
  18. if (the_form.elements.pred_hostname.value == 'userdefined' && the_form.elements.hostname.value === '') {
  19. alert(PMA_messages.strHostEmpty);
  20. the_form.elements.hostname.focus();
  21. return false;
  22. }
  23. if (the_form.elements.pred_username.value == 'userdefined' && the_form.elements.username.value === '') {
  24. alert(PMA_messages.strUserEmpty);
  25. the_form.elements.username.focus();
  26. return false;
  27. }
  28. return PMA_checkPassword($(the_form));
  29. } // end of the 'checkAddUser()' function
  30. /**
  31. * When a new user is created and retrieved over Ajax, append the user's row to
  32. * the user's table
  33. *
  34. * @param new_user_string the html for the new user's row
  35. * @param new_user_initial the first alphabet of the user's name
  36. * @param new_user_initial_string html to replace the initial for pagination
  37. */
  38. function appendNewUser(new_user_string, new_user_initial, new_user_initial_string)
  39. {
  40. //Append the newly retrieved user to the table now
  41. //Calculate the index for the new row
  42. var $curr_last_row = $("#usersForm").find('tbody').find('tr:last');
  43. if ($curr_last_row.length) {
  44. // at least one tr exists inside the tbody
  45. var $curr_first_row = $("#usersForm").find('tbody').find('tr:first');
  46. var first_row_initial = $curr_first_row.find('label').html().substr(0, 1).toUpperCase();
  47. var curr_shown_initial = $curr_last_row.find('label').html().substr(0, 1).toUpperCase();
  48. var curr_last_row_index_string = $curr_last_row.find('input:checkbox').attr('id').match(/\d+/)[0];
  49. var curr_last_row_index = parseFloat(curr_last_row_index_string);
  50. var new_last_row_index = curr_last_row_index + 1;
  51. var is_show_all = (first_row_initial != curr_shown_initial) ? true : false;
  52. var $insert_position = $curr_last_row;
  53. var dummy_tr_inserted = false;
  54. } else {
  55. // no tr exists inside the tbody
  56. var $tbody = $("#usersForm").find('tbody');
  57. // append a dummy tr
  58. $tbody.append('<tr></tr>');
  59. var dummy_tr_inserted = true;
  60. var $insert_position = $tbody.find('tr:first');
  61. var is_show_all = true;
  62. //todo: the case when the new user's initial does not match
  63. // the currently selected initial
  64. var curr_shown_initial = '';
  65. var new_last_row_index = 0;
  66. }
  67. var new_last_row_id = 'checkbox_sel_users_' + new_last_row_index;
  68. //Append to the table and set the id/names correctly
  69. if ((curr_shown_initial == new_user_initial) || is_show_all) {
  70. $(new_user_string)
  71. .insertAfter($insert_position)
  72. .find('input:checkbox')
  73. .attr('id', new_last_row_id)
  74. .val(function () {
  75. //the insert messes up the &amp;27; part. let's fix it
  76. return $(this).val().replace(/&/, '&amp;');
  77. })
  78. .end()
  79. .find('label')
  80. .attr('for', new_last_row_id)
  81. .end();
  82. }
  83. if (dummy_tr_inserted) {
  84. // remove the dummy tr
  85. $tbody.find('tr:first').remove();
  86. }
  87. //Let us sort the table alphabetically
  88. $("#usersForm").find('tbody').PMA_sort_table('label');
  89. $("#initials_table").find('td:contains(' + new_user_initial + ')')
  90. .html(new_user_initial_string);
  91. //update the checkall checkbox
  92. $(checkboxes_sel).trigger("change");
  93. }
  94. function addUser($form)
  95. {
  96. if (! checkAddUser($form.get(0))) {
  97. return false;
  98. }
  99. //We also need to post the value of the submit button in order to get this to work correctly
  100. $.post($form.attr('action'), $form.serialize() + "&adduser_submit=" + $("input[name=adduser_submit]").val(), function (data) {
  101. if (data.success === true) {
  102. // Refresh navigation, if we created a database with the name
  103. // that is the same as the username of the new user
  104. if ($('#add_user_dialog #createdb-1:checked').length) {
  105. PMA_reloadNavigation();
  106. }
  107. $('#page_content').show();
  108. $("#add_user_dialog").remove();
  109. PMA_ajaxShowMessage(data.message);
  110. $("#result_query").remove();
  111. $('#page_content').prepend(data.sql_query);
  112. PMA_highlightSQL($('#page_content'));
  113. $("#result_query").css({
  114. 'margin-top' : '0.5em'
  115. });
  116. //Remove the empty notice div generated due to a NULL query passed to PMA_getMessage()
  117. var $notice_class = $("#result_query").find('.notice');
  118. if ($notice_class.text() === '') {
  119. $notice_class.remove();
  120. }
  121. if ($('#fieldset_add_user a.ajax').attr('name') == 'db_specific') {
  122. /*process the fieldset_add_user attribute and get the val of privileges*/
  123. var url = $('#fieldset_add_user a.ajax').attr('rel');
  124. if (url.substring(url.length - 23, url.length) == "&goto=db_operations.php") {
  125. url = url.substring(0, url.length - 23);
  126. }
  127. url = url + "&ajax_request=true&db_specific=true";
  128. /* post request for get the updated userForm table */
  129. $.post($form.attr('action'), url, function (priv_data) {
  130. /*Remove the old userForm table*/
  131. if ($('#userFormDiv').length !== 0) {
  132. $('#userFormDiv').remove();
  133. } else {
  134. $("#usersForm").remove();
  135. }
  136. if (priv_data.success === true) {
  137. $('<div id="userFormDiv"></div>')
  138. .html(priv_data.user_form)
  139. .insertAfter('#result_query');
  140. } else {
  141. PMA_ajaxShowMessage(PMA_messages.strErrorProcessingRequest + " : " + priv_data.error, false);
  142. }
  143. });
  144. } else {
  145. appendNewUser(data.new_user_string, data.new_user_initial, data.new_user_initial_string);
  146. }
  147. } else {
  148. PMA_ajaxShowMessage(data.error, false);
  149. }
  150. });
  151. }
  152. /**
  153. * AJAX scripts for server_privileges page.
  154. *
  155. * Actions ajaxified here:
  156. * Add user
  157. * Revoke a user
  158. * Edit privileges
  159. * Export privileges
  160. * Paginate table of users
  161. * Flush privileges
  162. *
  163. * @memberOf jQuery
  164. * @name document.ready
  165. */
  166. /**
  167. * Unbind all event handlers before tearing down a page
  168. */
  169. AJAX.registerTeardown('server_privileges.js', function () {
  170. $("#fieldset_add_user_login input[name='username']").die("focusout");
  171. $("#fieldset_add_user a.ajax").die("click");
  172. $('form[name=usersForm]').unbind('submit');
  173. $("#fieldset_delete_user_footer #buttonGo.ajax").die('click');
  174. $("a.edit_user_anchor.ajax").die('click');
  175. $("a.edit_user_group_anchor.ajax").die('click');
  176. $("#edit_user_dialog").find("form.ajax").die('submit');
  177. $("button.mult_submit[value=export]").die('click');
  178. $("a.export_user_anchor.ajax").die('click');
  179. $("#initials_table").find("a.ajax").die('click');
  180. $('#checkbox_drop_users_db').unbind('click');
  181. $(".checkall_box").die("click");
  182. });
  183. AJAX.registerOnload('server_privileges.js', function () {
  184. /**
  185. * Display a warning if there is already a user by the name entered as the username.
  186. */
  187. $("#fieldset_add_user_login input[name='username']").live("focusout", function () {
  188. var username = $(this).val();
  189. var $warning = $("#user_exists_warning");
  190. if ($("#select_pred_username").val() == 'userdefined' && username !== '') {
  191. var href = $("form[name='usersForm']").attr('action');
  192. var params = {
  193. 'ajax_request' : true,
  194. 'token' : PMA_commonParams.get('token'),
  195. 'server' : PMA_commonParams.get('server'),
  196. 'validate_username' : true,
  197. 'username' : username
  198. };
  199. $.get(href, params, function (data) {
  200. if (data.user_exists) {
  201. $warning.show();
  202. } else {
  203. $warning.hide();
  204. }
  205. });
  206. } else {
  207. $warning.hide();
  208. }
  209. });
  210. /**
  211. * AJAX event handler for 'Add a New User'
  212. *
  213. * @see PMA_ajaxShowMessage()
  214. * @see appendNewUser()
  215. * @memberOf jQuery
  216. * @name add_user_click
  217. *
  218. */
  219. $("#fieldset_add_user a.ajax").live("click", function (event) {
  220. /** @lends jQuery */
  221. event.preventDefault();
  222. var $msgbox = PMA_ajaxShowMessage();
  223. $.get($(this).attr("href"), {'ajax_request': true}, function (data) {
  224. if (data.success === true) {
  225. $('#page_content').hide();
  226. var $div = $('#add_user_dialog');
  227. if ($div.length === 0) {
  228. $div = $('<div id="add_user_dialog" style="margin: 0.5em;"></div>')
  229. .insertBefore('#page_content');
  230. } else {
  231. $div.empty();
  232. }
  233. $div.html(data.message)
  234. .find("form[name=usersForm]")
  235. .append('<input type="hidden" name="ajax_request" value="true" />')
  236. .end();
  237. PMA_highlightSQL($div);
  238. displayPasswordGenerateButton();
  239. PMA_showHints($div);
  240. PMA_ajaxRemoveMessage($msgbox);
  241. $div.find("input.autofocus").focus();
  242. $div.find('form[name=usersForm]').bind('submit', function (event) {
  243. event.preventDefault();
  244. addUser($(this));
  245. });
  246. } else {
  247. PMA_ajaxShowMessage(data.error, false);
  248. }
  249. }); // end $.get()
  250. });//end of Add New User AJAX event handler
  251. /**
  252. * AJAX handler for 'Revoke User'
  253. *
  254. * @see PMA_ajaxShowMessage()
  255. * @memberOf jQuery
  256. * @name revoke_user_click
  257. */
  258. $("#fieldset_delete_user_footer #buttonGo.ajax").live('click', function (event) {
  259. event.preventDefault();
  260. $drop_users_db_checkbox = $("#checkbox_drop_users_db");
  261. if ($drop_users_db_checkbox.is(':checked')) {
  262. var is_confirmed = confirm(PMA_messages.strDropDatabaseStrongWarning + '\n' + $.sprintf(PMA_messages.strDoYouReally, 'DROP DATABASE'));
  263. if (! is_confirmed) {
  264. // Uncheck the drop users database checkbox
  265. $drop_users_db_checkbox.prop('checked', false);
  266. }
  267. }
  268. PMA_ajaxShowMessage(PMA_messages.strRemovingSelectedUsers);
  269. var $form = $("#usersForm");
  270. $.post($form.attr('action'), $form.serialize() + "&delete=" + $(this).val() + "&ajax_request=true", function (data) {
  271. if (data.success === true) {
  272. PMA_ajaxShowMessage(data.message);
  273. // Refresh navigation, if we droppped some databases with the name
  274. // that is the same as the username of the deleted user
  275. if ($('#checkbox_drop_users_db:checked').length) {
  276. PMA_reloadNavigation();
  277. }
  278. //Remove the revoked user from the users list
  279. $form.find("input:checkbox:checked").parents("tr").slideUp("medium", function () {
  280. var this_user_initial = $(this).find('input:checkbox').val().charAt(0).toUpperCase();
  281. $(this).remove();
  282. //If this is the last user with this_user_initial, remove the link from #initials_table
  283. if ($("#tableuserrights").find('input:checkbox[value^="' + this_user_initial + '"]').length === 0) {
  284. $("#initials_table").find('td > a:contains(' + this_user_initial + ')').parent('td').html(this_user_initial);
  285. }
  286. //Re-check the classes of each row
  287. $form
  288. .find('tbody').find('tr:odd')
  289. .removeClass('even').addClass('odd')
  290. .end()
  291. .find('tr:even')
  292. .removeClass('odd').addClass('even');
  293. //update the checkall checkbox
  294. $(checkboxes_sel).trigger("change");
  295. });
  296. } else {
  297. PMA_ajaxShowMessage(data.error, false);
  298. }
  299. }); // end $.post()
  300. }); // end Revoke User
  301. $("a.edit_user_group_anchor.ajax").live('click', function (event) {
  302. event.preventDefault();
  303. $(this).parents('tr').addClass('current_row');
  304. var token = $(this).parents('form').find('input[name="token"]').val();
  305. var $msg = PMA_ajaxShowMessage();
  306. $.get(
  307. $(this).attr('href'),
  308. {
  309. 'ajax_request': true,
  310. 'edit_user_group_dialog': true,
  311. 'token': token
  312. },
  313. function (data) {
  314. if (data.success === true) {
  315. PMA_ajaxRemoveMessage($msg);
  316. var buttonOptions = {};
  317. buttonOptions[PMA_messages.strGo] = function () {
  318. var usrGroup = $('#changeUserGroupDialog')
  319. .find('select[name="userGroup"]')
  320. .val();
  321. var $message = PMA_ajaxShowMessage();
  322. $.get(
  323. 'server_privileges.php',
  324. $('#changeUserGroupDialog').find('form').serialize() + '&ajax_request=1',
  325. function (data) {
  326. PMA_ajaxRemoveMessage($message);
  327. if (data.success === true) {
  328. $("#usersForm")
  329. .find('.current_row')
  330. .removeClass('current_row')
  331. .find('.usrGroup')
  332. .text(usrGroup);
  333. } else {
  334. PMA_ajaxShowMessage(data.error, false);
  335. $("#usersForm")
  336. .find('.current_row')
  337. .removeClass('current_row');
  338. }
  339. }
  340. );
  341. $(this).dialog("close");
  342. };
  343. buttonOptions[PMA_messages.strClose] = function () {
  344. $(this).dialog("close");
  345. };
  346. var $dialog = $('<div/>')
  347. .attr('id', 'changeUserGroupDialog')
  348. .append(data.message)
  349. .dialog({
  350. width: 500,
  351. minWidth: 300,
  352. modal: true,
  353. buttons: buttonOptions,
  354. title: $('legend', $(data.message)).text(),
  355. close: function () {
  356. $(this).remove();
  357. }
  358. });
  359. $dialog.find('legend').remove();
  360. } else {
  361. PMA_ajaxShowMessage(data.error, false);
  362. $("#usersForm")
  363. .find('.current_row')
  364. .removeClass('current_row');
  365. }
  366. }
  367. );
  368. });
  369. /**
  370. * AJAX handler for 'Edit User'
  371. *
  372. * @see PMA_ajaxShowMessage()
  373. *
  374. */
  375. /**
  376. * Step 1: Load Edit User Dialog
  377. * @memberOf jQuery
  378. * @name edit_user_click
  379. */
  380. $("a.edit_user_anchor.ajax").live('click', function (event) {
  381. /** @lends jQuery */
  382. event.preventDefault();
  383. var $msgbox = PMA_ajaxShowMessage();
  384. $(this).parents('tr').addClass('current_row');
  385. var token = $(this).parents('form').find('input[name="token"]').val();
  386. $.get(
  387. $(this).attr('href'),
  388. {
  389. 'ajax_request': true,
  390. 'edit_user_dialog': true,
  391. 'token': token
  392. },
  393. function (data) {
  394. if (data.success === true) {
  395. $('#page_content').hide();
  396. var $div = $('#edit_user_dialog');
  397. if ($div.length === 0) {
  398. $div = $('<div id="edit_user_dialog" style="margin: 0.5em;"></div>')
  399. .insertBefore('#page_content');
  400. } else {
  401. $div.empty();
  402. }
  403. $div.html(data.message);
  404. PMA_highlightSQL($div);
  405. $div = $('#edit_user_dialog');
  406. displayPasswordGenerateButton();
  407. addOrUpdateSubmenu();
  408. $(checkboxes_sel).trigger("change");
  409. PMA_ajaxRemoveMessage($msgbox);
  410. PMA_showHints($div);
  411. } else {
  412. PMA_ajaxShowMessage(data.error, false);
  413. }
  414. }
  415. ); // end $.get()
  416. });
  417. /**
  418. * Step 2: Submit the Edit User Dialog
  419. *
  420. * @see PMA_ajaxShowMessage()
  421. * @memberOf jQuery
  422. * @name edit_user_submit
  423. */
  424. $("#edit_user_dialog").find("form.ajax").live('submit', function (event) {
  425. /** @lends jQuery */
  426. event.preventDefault();
  427. var $t = $(this);
  428. if ($t.is('.copyUserForm') && ! PMA_checkPassword($t)) {
  429. return false;
  430. }
  431. PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
  432. $t.append('<input type="hidden" name="ajax_request" value="true" />');
  433. /**
  434. * @var curr_submit_name name of the current button being submitted
  435. */
  436. var curr_submit_name = $t.find('.tblFooters').find('input:submit').attr('name');
  437. /**
  438. * @var curr_submit_value value of the current button being submitted
  439. */
  440. var curr_submit_value = $t.find('.tblFooters').find('input:submit').val();
  441. // If any option other than 'keep the old one'(option 4) is chosen, we need to remove
  442. // the old one from the table.
  443. var $row_to_remove;
  444. if (curr_submit_name == 'change_copy' &&
  445. $('input[name=mode]:checked', '#fieldset_mode').val() != '4'
  446. ) {
  447. var old_username = $t.find('input[name="old_username"]').val();
  448. var old_hostname = $t.find('input[name="old_hostname"]').val();
  449. $('#usersForm tbody tr').each(function () {
  450. var $tr = $(this);
  451. if ($tr.find('td:nth-child(2) label').text() == old_username &&
  452. $tr.find('td:nth-child(3)').text() == old_hostname
  453. ) {
  454. $row_to_remove = $tr;
  455. return false;
  456. }
  457. });
  458. }
  459. $.post($t.attr('action'), $t.serialize() + '&' + curr_submit_name + '=' + curr_submit_value, function (data) {
  460. if (data.success === true) {
  461. $('#page_content').show();
  462. $("#edit_user_dialog").remove();
  463. PMA_ajaxShowMessage(data.message);
  464. if (data.sql_query) {
  465. $("#result_query").remove();
  466. $('#page_content').prepend(data.sql_query);
  467. PMA_highlightSQL($('#page_content'));
  468. $("#result_query").css({
  469. 'margin-top' : '0.5em'
  470. });
  471. var $notice_class = $("#result_query").find('.notice');
  472. if ($notice_class.text() === '') {
  473. $notice_class.remove();
  474. }
  475. } //Show SQL Query that was executed
  476. // Remove the old row if the old user is deleted
  477. if (typeof $row_to_remove != 'undefined' && $row_to_remove !== null) {
  478. $row_to_remove.remove();
  479. }
  480. //Append new user if necessary
  481. if (data.new_user_string) {
  482. appendNewUser(data.new_user_string, data.new_user_initial, data.new_user_initial_string);
  483. }
  484. //Check if we are on the page of the db-specific privileges
  485. var db_priv_page = !!($('#dbspecificuserrights').length); // the "!!" part is merely there to ensure a value of type boolean
  486. // we always need to reload on the db-specific privilege page
  487. // and on the global page when adjusting global privileges,
  488. // but not on the global page when adjusting db-specific privileges.
  489. var reload_privs = false;
  490. if (data.db_specific_privs === false || (db_priv_page == data.db_specific_privs)) {
  491. reload_privs = true;
  492. }
  493. if (data.db_wildcard_privs) {
  494. reload_privs = false;
  495. }
  496. //Change privileges, if they were edited and need to be reloaded
  497. if (data.new_privileges && reload_privs) {
  498. $("#usersForm")
  499. .find('.current_row')
  500. .find('code')
  501. .html(data.new_privileges);
  502. }
  503. $("#usersForm")
  504. .find('.current_row')
  505. .removeClass('current_row');
  506. } else {
  507. PMA_ajaxShowMessage(data.error, false);
  508. }
  509. });
  510. });
  511. //end Edit user
  512. /**
  513. * AJAX handler for 'Export Privileges'
  514. *
  515. * @see PMA_ajaxShowMessage()
  516. * @memberOf jQuery
  517. * @name export_user_click
  518. */
  519. $("button.mult_submit[value=export]").live('click', function (event) {
  520. event.preventDefault();
  521. // can't export if no users checked
  522. if ($(this.form).find("input:checked").length === 0) {
  523. return;
  524. }
  525. var $msgbox = PMA_ajaxShowMessage();
  526. var button_options = {};
  527. button_options[PMA_messages.strClose] = function () {
  528. $(this).dialog("close");
  529. };
  530. $.post(
  531. $(this.form).prop('action'),
  532. $(this.form).serialize() + '&submit_mult=export&ajax_request=true',
  533. function (data) {
  534. if (data.success === true) {
  535. var $ajaxDialog = $('<div />')
  536. .append(data.message)
  537. .dialog({
  538. title: data.title,
  539. width: 500,
  540. buttons: button_options,
  541. close: function () {
  542. $(this).remove();
  543. }
  544. });
  545. PMA_ajaxRemoveMessage($msgbox);
  546. // Attach syntax highlighted editor to export dialog
  547. if (typeof CodeMirror != 'undefined') {
  548. CodeMirror.fromTextArea(
  549. $ajaxDialog.find('textarea')[0],
  550. {
  551. lineNumbers: true,
  552. matchBrackets: true,
  553. indentUnit: 4,
  554. mode: "text/x-mysql",
  555. lineWrapping: true
  556. }
  557. );
  558. }
  559. } else {
  560. PMA_ajaxShowMessage(data.error, false);
  561. }
  562. }
  563. ); //end $.post
  564. });
  565. // if exporting non-ajax, highlight anyways
  566. if ($("textarea.export").length > 0 && typeof CodeMirror != 'undefined') {
  567. CodeMirror.fromTextArea(
  568. $('textarea.export')[0],
  569. {
  570. lineNumbers: true,
  571. matchBrackets: true,
  572. indentUnit: 4,
  573. mode: "text/x-mysql",
  574. lineWrapping: true
  575. }
  576. );
  577. }
  578. $("a.export_user_anchor.ajax").live('click', function (event) {
  579. event.preventDefault();
  580. var $msgbox = PMA_ajaxShowMessage();
  581. /**
  582. * @var button_options Object containing options for jQueryUI dialog buttons
  583. */
  584. var button_options = {};
  585. button_options[PMA_messages.strClose] = function () {
  586. $(this).dialog("close");
  587. };
  588. $.get($(this).attr('href'), {'ajax_request': true}, function (data) {
  589. if (data.success === true) {
  590. var $ajaxDialog = $('<div />')
  591. .append(data.message)
  592. .dialog({
  593. title: data.title,
  594. width: 500,
  595. buttons: button_options,
  596. close: function () {
  597. $(this).remove();
  598. }
  599. });
  600. PMA_ajaxRemoveMessage($msgbox);
  601. // Attach syntax highlighted editor to export dialog
  602. if (typeof CodeMirror != 'undefined') {
  603. CodeMirror.fromTextArea(
  604. $ajaxDialog.find('textarea')[0],
  605. {
  606. lineNumbers: true,
  607. matchBrackets: true,
  608. indentUnit: 4,
  609. mode: "text/x-mysql",
  610. lineWrapping: true
  611. }
  612. );
  613. }
  614. } else {
  615. PMA_ajaxShowMessage(data.error, false);
  616. }
  617. }); //end $.get
  618. }); //end export privileges
  619. /**
  620. * AJAX handler to Paginate the Users Table
  621. *
  622. * @see PMA_ajaxShowMessage()
  623. * @name paginate_users_table_click
  624. * @memberOf jQuery
  625. */
  626. $("#initials_table").find("a.ajax").live('click', function (event) {
  627. event.preventDefault();
  628. var $msgbox = PMA_ajaxShowMessage();
  629. $.get($(this).attr('href'), {'ajax_request' : true}, function (data) {
  630. if (data.success === true) {
  631. PMA_ajaxRemoveMessage($msgbox);
  632. // This form is not on screen when first entering Privileges
  633. // if there are more than 50 users
  634. $("div.notice").remove();
  635. $("#usersForm").hide("medium").remove();
  636. $("#fieldset_add_user").hide("medium").remove();
  637. $("#initials_table")
  638. .prop("id", "initials_table_old")
  639. .after(data.message).show("medium")
  640. .siblings("h2").not(":first").remove();
  641. // prevent double initials table
  642. $("#initials_table_old").remove();
  643. } else {
  644. PMA_ajaxShowMessage(data.error, false);
  645. }
  646. }); // end $.get
  647. }); // end of the paginate users table
  648. displayPasswordGenerateButton();
  649. /*
  650. * Create submenu for simpler interface
  651. */
  652. var addOrUpdateSubmenu = function () {
  653. var $topmenu2 = $("#topmenu2"),
  654. $edit_user_dialog = $("#edit_user_dialog"),
  655. submenu_label,
  656. submenu_link,
  657. link_number;
  658. // if submenu exists yet, remove it first
  659. if ($topmenu2.length > 0) {
  660. $topmenu2.remove();
  661. }
  662. // construct a submenu from the existing fieldsets
  663. $topmenu2 = $("<ul/>").prop("id", "topmenu2");
  664. $("#edit_user_dialog .submenu-item").each(function () {
  665. submenu_label = $(this).find("legend[data-submenu-label]").data("submenu-label");
  666. submenu_link = $("<a/>")
  667. .prop("href", "#")
  668. .html(submenu_label);
  669. $("<li/>")
  670. .append(submenu_link)
  671. .appendTo($topmenu2);
  672. });
  673. // click handlers for submenu
  674. $topmenu2.find("a").click(function (e) {
  675. e.preventDefault();
  676. // if already active, ignore click
  677. if ($(this).hasClass("tabactive")) {
  678. return;
  679. }
  680. $topmenu2.find("a").removeClass("tabactive");
  681. $(this).addClass("tabactive");
  682. // which section to show now?
  683. link_number = $topmenu2.find("a").index($(this));
  684. // hide all sections but the one to show
  685. $("#edit_user_dialog .submenu-item").hide().eq(link_number).show();
  686. });
  687. // make first menu item active
  688. // TODO: support URL hash history
  689. $topmenu2.find("> :first-child a").addClass("tabactive");
  690. $edit_user_dialog.prepend($topmenu2);
  691. // hide all sections but the first
  692. $("#edit_user_dialog .submenu-item").hide().eq(0).show();
  693. };
  694. });