1 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 * @fileoverview functions used in server privilege pages
4 * @name Server Privileges
8 * @requires js/functions.js
13 * Validates the "add a user" form
15 * @return boolean whether the form is validated or not
17 function checkAddUser(the_form)
19 if (the_form.elements.pred_hostname.value == 'userdefined' && the_form.elements.hostname.value === '') {
20 alert(PMA_messages.strHostEmpty);
21 the_form.elements.hostname.focus();
25 if (the_form.elements.pred_username.value == 'userdefined' && the_form.elements.username.value === '') {
26 alert(PMA_messages.strUserEmpty);
27 the_form.elements.username.focus();
31 return PMA_checkPassword($(the_form));
32 } // end of the 'checkAddUser()' function
35 * When a new user is created and retrieved over Ajax, append the user's row to
38 * @param new_user_string the html for the new user's row
39 * @param new_user_initial the first alphabet of the user's name
40 * @param new_user_initial_string html to replace the initial for pagination
42 function appendNewUser(new_user_string, new_user_initial, new_user_initial_string)
44 //Append the newly retrieved user to the table now
46 //Calculate the index for the new row
47 var $curr_last_row = $("#usersForm").find('tbody').find('tr:last');
48 var $curr_first_row = $("#usersForm").find('tbody').find('tr:first');
49 var first_row_initial = $curr_first_row.find('label').html().substr(0, 1).toUpperCase();
50 var curr_shown_initial = $curr_last_row.find('label').html().substr(0, 1).toUpperCase();
51 var curr_last_row_index_string = $curr_last_row.find('input:checkbox').attr('id').match(/\d+/)[0];
52 var curr_last_row_index = parseFloat(curr_last_row_index_string);
53 var new_last_row_index = curr_last_row_index + 1;
54 var new_last_row_id = 'checkbox_sel_users_' + new_last_row_index;
55 var is_show_all = (first_row_initial != curr_shown_initial) ? true : false;
57 //Append to the table and set the id/names correctly
58 if ((curr_shown_initial == new_user_initial) || is_show_all) {
60 .insertAfter($curr_last_row)
61 .find('input:checkbox')
62 .attr('id', new_last_row_id)
64 //the insert messes up the &27; part. let's fix it
65 return $(this).val().replace(/&/, '&');
69 .attr('for', new_last_row_id)
73 //Let us sort the table alphabetically
74 $("#usersForm").find('tbody').PMA_sort_table('label');
76 $("#initials_table").find('td:contains(' + new_user_initial + ')')
77 .html(new_user_initial_string);
79 //update the checkall checkbox
80 $(checkboxes_sel).trigger("change");
83 function addUser($form)
85 if (! checkAddUser($form.get(0))) {
89 //We also need to post the value of the submit button in order to get this to work correctly
90 $.post($form.attr('action'), $form.serialize() + "&adduser_submit=" + $("input[name=adduser_submit]").val(), function (data) {
91 if (data.success === true) {
92 // Refresh navigation, if we created a database with the name
93 // that is the same as the username of the new user
94 if ($('#add_user_dialog #createdb-1:checked').length) {
95 PMA_reloadNavigation();
98 $('#page_content').show();
99 $("#add_user_dialog").remove();
101 PMA_ajaxShowMessage(data.message);
102 $("#result_query").remove();
103 $('#page_content').prepend(data.sql_query);
104 PMA_highlightSQL($('#page_content'));
105 $("#result_query").css({
106 'margin-top' : '0.5em'
109 //Remove the empty notice div generated due to a NULL query passed to PMA_getMessage()
110 var $notice_class = $("#result_query").find('.notice');
111 if ($notice_class.text() === '') {
112 $notice_class.remove();
114 if ($('#fieldset_add_user a.ajax').attr('name') == 'db_specific') {
116 /*process the fieldset_add_user attribute and get the val of privileges*/
117 var url = $('#fieldset_add_user a.ajax').attr('rel');
119 if (url.substring(url.length - 23, url.length) == "&goto=db_operations.php") {
120 url = url.substring(0, url.length - 23);
122 url = url + "&ajax_request=true&db_specific=true";
124 /* post request for get the updated userForm table */
125 $.post($form.attr('action'), url, function (priv_data) {
127 /*Remove the old userForm table*/
128 if ($('#userFormDiv').length !== 0) {
129 $('#userFormDiv').remove();
131 $("#usersForm").remove();
133 if (priv_data.success === true) {
134 $('<div id="userFormDiv"></div>')
135 .html(priv_data.user_form)
136 .insertAfter('#result_query');
138 PMA_ajaxShowMessage(PMA_messages.strErrorProcessingRequest + " : " + priv_data.error, false);
142 appendNewUser(data.new_user_string, data.new_user_initial, data.new_user_initial_string);
145 PMA_ajaxShowMessage(data.error, false);
151 * AJAX scripts for server_privileges page.
153 * Actions ajaxified here:
158 * Paginate table of users
162 * @name document.ready
167 * Unbind all event handlers before tearing down a page
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");
184 AJAX.registerOnload('server_privileges.js', function () {
186 * Display a warning if there is already a user by the name entered as the username.
188 $("#fieldset_add_user_login input[name='username']").live("focusout", function () {
189 var username = $(this).val();
190 var $warning = $("#user_exists_warning");
191 if ($("#select_pred_username").val() == 'userdefined' && username !== '') {
192 var href = $("form[name='usersForm']").attr('action');
194 'ajax_request' : true,
195 'token' : PMA_commonParams.get('token'),
196 'validate_username' : true,
197 'username' : username
199 $.get(href, params, function (data) {
200 if (data.user_exists) {
211 * AJAX event handler for 'Add a New User'
213 * @see PMA_ajaxShowMessage()
214 * @see appendNewUser()
216 * @name add_user_click
219 $("#fieldset_add_user a.ajax").live("click", function (event) {
221 event.preventDefault();
222 var $msgbox = PMA_ajaxShowMessage();
224 $.get($(this).attr("href"), {'ajax_request': true}, function (data) {
225 if (data.success === true) {
226 $('#page_content').hide();
227 var $div = $('#add_user_dialog');
228 if ($div.length === 0) {
229 $div = $('<div id="add_user_dialog" style="margin: 0.5em;"></div>')
230 .insertBefore('#page_content');
234 $div.html(data.message)
235 .find("form[name=usersForm]")
236 .append('<input type="hidden" name="ajax_request" value="true" />')
238 PMA_highlightSQL($div);
239 displayPasswordGenerateButton();
241 PMA_ajaxRemoveMessage($msgbox);
242 $div.find("input.autofocus").focus();
244 $div.find('form[name=usersForm]').bind('submit', function (event) {
245 event.preventDefault();
249 PMA_ajaxShowMessage(data.error, false);
253 });//end of Add New User AJAX event handler
256 * AJAX handler for 'Revoke User'
258 * @see PMA_ajaxShowMessage()
260 * @name revoke_user_click
262 $("#fieldset_delete_user_footer #buttonGo.ajax").live('click', function (event) {
263 event.preventDefault();
265 $drop_users_db_checkbox = $("#checkbox_drop_users_db");
266 if ($drop_users_db_checkbox.is(':checked')) {
267 var is_confirmed = confirm(PMA_messages.strDropDatabaseStrongWarning + '\n' + $.sprintf(PMA_messages.strDoYouReally, 'DROP DATABASE'));
268 if (! is_confirmed) {
269 // Uncheck the drop users database checkbox
270 $drop_users_db_checkbox.prop('checked', false);
274 PMA_ajaxShowMessage(PMA_messages.strRemovingSelectedUsers);
276 var $form = $("#usersForm");
278 $.post($form.attr('action'), $form.serialize() + "&delete=" + $(this).val() + "&ajax_request=true", function (data) {
279 if (data.success === true) {
280 PMA_ajaxShowMessage(data.message);
281 // Refresh navigation, if we droppped some databases with the name
282 // that is the same as the username of the deleted user
283 if ($('#checkbox_drop_users_db:checked').length) {
284 PMA_reloadNavigation();
286 //Remove the revoked user from the users list
287 $form.find("input:checkbox:checked").parents("tr").slideUp("medium", function () {
288 var this_user_initial = $(this).find('input:checkbox').val().charAt(0).toUpperCase();
291 //If this is the last user with this_user_initial, remove the link from #initials_table
292 if ($("#tableuserrights").find('input:checkbox[value^="' + this_user_initial + '"]').length === 0) {
293 $("#initials_table").find('td > a:contains(' + this_user_initial + ')').parent('td').html(this_user_initial);
296 //Re-check the classes of each row
298 .find('tbody').find('tr:odd')
299 .removeClass('even').addClass('odd')
302 .removeClass('odd').addClass('even');
304 //update the checkall checkbox
305 $(checkboxes_sel).trigger("change");
308 PMA_ajaxShowMessage(data.error, false);
311 }); // end Revoke User
313 $("a.edit_user_group_anchor.ajax").live('click', function (event) {
314 event.preventDefault();
315 $(this).parents('tr').addClass('current_row');
316 var token = $(this).parents('form').find('input[name="token"]').val();
317 var $msg = PMA_ajaxShowMessage();
319 $(this).attr('href'),
321 'ajax_request': true,
322 'edit_user_group_dialog': true,
326 if (data.success === true) {
327 PMA_ajaxRemoveMessage($msg);
328 var buttonOptions = {};
329 buttonOptions[PMA_messages.strGo] = function () {
330 var usrGroup = $('#changeUserGroupDialog')
331 .find('select[name="userGroup"]')
333 var $message = PMA_ajaxShowMessage();
335 'server_privileges.php',
336 $('#changeUserGroupDialog').find('form').serialize() + '&ajax_request=1',
338 PMA_ajaxRemoveMessage($message);
339 if (data.success === true) {
341 .find('.current_row')
342 .removeClass('current_row')
346 PMA_ajaxShowMessage(data.error, false);
348 .find('.current_row')
349 .removeClass('current_row');
353 $(this).dialog("close");
355 buttonOptions[PMA_messages.strClose] = function () {
356 $(this).dialog("close");
358 var $dialog = $('<div/>')
359 .attr('id', 'changeUserGroupDialog')
360 .append(data.message)
365 buttons: buttonOptions,
366 title: $('legend', $(data.message)).text(),
371 $dialog.find('legend').remove();
373 PMA_ajaxShowMessage(data.error, false);
375 .find('.current_row')
376 .removeClass('current_row');
383 * AJAX handler for 'Edit User'
385 * @see PMA_ajaxShowMessage()
390 * Step 1: Load Edit User Dialog
392 * @name edit_user_click
394 $("a.edit_user_anchor.ajax").live('click', function (event) {
396 event.preventDefault();
398 var $msgbox = PMA_ajaxShowMessage();
400 $(this).parents('tr').addClass('current_row');
402 var token = $(this).parents('form').find('input[name="token"]').val();
404 $(this).attr('href'),
406 'ajax_request': true,
407 'edit_user_dialog': true,
411 if (data.success === true) {
412 $('#page_content').hide();
413 var $div = $('#edit_user_dialog');
414 if ($div.length === 0) {
415 $div = $('<div id="edit_user_dialog" style="margin: 0.5em;"></div>')
416 .insertBefore('#page_content');
420 $div.html(data.message);
421 PMA_highlightSQL($div);
422 $div = $('#edit_user_dialog');
423 displayPasswordGenerateButton();
424 addOrUpdateSubmenu();
425 $(checkboxes_sel).trigger("change");
426 PMA_ajaxRemoveMessage($msgbox);
429 PMA_ajaxShowMessage(data.error, false);
436 * Step 2: Submit the Edit User Dialog
438 * @see PMA_ajaxShowMessage()
440 * @name edit_user_submit
442 $("#edit_user_dialog").find("form.ajax").live('submit', function (event) {
444 event.preventDefault();
448 if ($t.is('.copyUserForm') && ! PMA_checkPassword($t)) {
452 PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
454 $t.append('<input type="hidden" name="ajax_request" value="true" />');
457 * @var curr_submit_name name of the current button being submitted
459 var curr_submit_name = $t.find('.tblFooters').find('input:submit').attr('name');
462 * @var curr_submit_value value of the current button being submitted
464 var curr_submit_value = $t.find('.tblFooters').find('input:submit').val();
466 // If any option other than 'keep the old one'(option 4) is chosen, we need to remove
467 // the old one from the table.
469 if (curr_submit_name == 'change_copy' &&
470 $('input[name=mode]:checked', '#fieldset_mode').val() != '4'
472 var old_username = $t.find('input[name="old_username"]').val();
473 var old_hostname = $t.find('input[name="old_hostname"]').val();
474 $('#usersForm tbody tr').each(function () {
476 if ($tr.find('td:nth-child(2) label').text() == old_username &&
477 $tr.find('td:nth-child(3)').text() == old_hostname
479 $row_to_remove = $tr;
485 $.post($t.attr('action'), $t.serialize() + '&' + curr_submit_name + '=' + curr_submit_value, function (data) {
486 if (data.success === true) {
487 $('#page_content').show();
488 $("#edit_user_dialog").remove();
490 PMA_ajaxShowMessage(data.message);
492 if (data.sql_query) {
493 $("#result_query").remove();
494 $('#page_content').prepend(data.sql_query);
495 PMA_highlightSQL($('#page_content'));
496 $("#result_query").css({
497 'margin-top' : '0.5em'
499 var $notice_class = $("#result_query").find('.notice');
500 if ($notice_class.text() === '') {
501 $notice_class.remove();
503 } //Show SQL Query that was executed
505 // Remove the old row if the old user is deleted
506 if (typeof $row_to_remove != 'undefined' && $row_to_remove !== null) {
507 $row_to_remove.remove();
510 //Append new user if necessary
511 if (data.new_user_string) {
512 appendNewUser(data.new_user_string, data.new_user_initial, data.new_user_initial_string);
515 //Check if we are on the page of the db-specific privileges
516 var db_priv_page = !!($('#dbspecificuserrights').length); // the "!!" part is merely there to ensure a value of type boolean
517 // we always need to reload on the db-specific privilege page
518 // and on the global page when adjusting global privileges,
519 // but not on the global page when adjusting db-specific privileges.
520 var reload_privs = false;
521 if (data.db_specific_privs === false || (db_priv_page == data.db_specific_privs)) {
524 if (data.db_wildcard_privs) {
525 reload_privs = false;
528 //Change privileges, if they were edited and need to be reloaded
529 if (data.new_privileges && reload_privs) {
531 .find('.current_row')
533 .html(data.new_privileges);
537 .find('.current_row')
538 .removeClass('current_row');
540 PMA_ajaxShowMessage(data.error, false);
547 * AJAX handler for 'Export Privileges'
549 * @see PMA_ajaxShowMessage()
551 * @name export_user_click
553 $("button.mult_submit[value=export]").live('click', function (event) {
554 event.preventDefault();
555 // can't export if no users checked
556 if ($(this.form).find("input:checked").length === 0) {
559 var $msgbox = PMA_ajaxShowMessage();
560 var button_options = {};
561 button_options[PMA_messages.strClose] = function () {
562 $(this).dialog("close");
565 $(this.form).prop('action'),
566 $(this.form).serialize() + '&submit_mult=export&ajax_request=true',
568 if (data.success === true) {
569 var $ajaxDialog = $('<div />')
570 .append(data.message)
574 buttons: button_options,
579 PMA_ajaxRemoveMessage($msgbox);
580 // Attach syntax highlighted editor to export dialog
581 if (typeof CodeMirror != 'undefined') {
582 CodeMirror.fromTextArea(
583 $ajaxDialog.find('textarea')[0],
588 mode: "text/x-mysql",
594 PMA_ajaxShowMessage(data.error, false);
599 // if exporting non-ajax, highlight anyways
600 if ($("textarea.export").length > 0 && typeof CodeMirror != 'undefined') {
601 CodeMirror.fromTextArea(
602 $('textarea.export')[0],
607 mode: "text/x-mysql",
613 $("a.export_user_anchor.ajax").live('click', function (event) {
614 event.preventDefault();
615 var $msgbox = PMA_ajaxShowMessage();
617 * @var button_options Object containing options for jQueryUI dialog buttons
619 var button_options = {};
620 button_options[PMA_messages.strClose] = function () {
621 $(this).dialog("close");
623 $.get($(this).attr('href'), {'ajax_request': true}, function (data) {
624 if (data.success === true) {
625 var $ajaxDialog = $('<div />')
626 .append(data.message)
630 buttons: button_options,
635 PMA_ajaxRemoveMessage($msgbox);
636 // Attach syntax highlighted editor to export dialog
637 if (typeof CodeMirror != 'undefined') {
638 CodeMirror.fromTextArea(
639 $ajaxDialog.find('textarea')[0],
644 mode: "text/x-mysql",
650 PMA_ajaxShowMessage(data.error, false);
653 }); //end export privileges
656 * AJAX handler to Paginate the Users Table
658 * @see PMA_ajaxShowMessage()
659 * @name paginate_users_table_click
662 $("#initials_table").find("a.ajax").live('click', function (event) {
663 event.preventDefault();
664 var $msgbox = PMA_ajaxShowMessage();
665 $.get($(this).attr('href'), {'ajax_request' : true}, function (data) {
666 if (data.success === true) {
667 PMA_ajaxRemoveMessage($msgbox);
668 // This form is not on screen when first entering Privileges
669 // if there are more than 50 users
670 $("div.notice").remove();
671 $("#usersForm").hide("medium").remove();
672 $("#fieldset_add_user").hide("medium").remove();
674 .prop("id", "initials_table_old")
675 .after(data.message).show("medium")
676 .siblings("h2").not(":first").remove();
677 // prevent double initials table
678 $("#initials_table_old").remove();
680 PMA_ajaxShowMessage(data.error, false);
683 }); // end of the paginate users table
685 displayPasswordGenerateButton();
688 * Create submenu for simpler interface
690 var addOrUpdateSubmenu = function () {
691 var $topmenu2 = $("#topmenu2"),
692 $edit_user_dialog = $("#edit_user_dialog"),
697 // if submenu exists yet, remove it first
698 if ($topmenu2.length > 0) {
702 // construct a submenu from the existing fieldsets
703 $topmenu2 = $("<ul/>").prop("id", "topmenu2");
705 $("#edit_user_dialog .submenu-item").each(function () {
706 submenu_label = $(this).find("legend[data-submenu-label]").data("submenu-label");
708 submenu_link = $("<a/>")
710 .html(submenu_label);
713 .append(submenu_link)
714 .appendTo($topmenu2);
717 // click handlers for submenu
718 $topmenu2.find("a").click(function (e) {
720 // if already active, ignore click
721 if ($(this).hasClass("tabactive")) {
724 $topmenu2.find("a").removeClass("tabactive");
725 $(this).addClass("tabactive");
727 // which section to show now?
728 link_number = $topmenu2.find("a").index($(this));
729 // hide all sections but the one to show
730 $("#edit_user_dialog .submenu-item").hide().eq(link_number).show();
733 // make first menu item active
734 // TODO: support URL hash history
735 $topmenu2.find("> :first-child a").addClass("tabactive");
736 $edit_user_dialog.prepend($topmenu2);
738 // hide all sections but the first
739 $("#edit_user_dialog .submenu-item").hide().eq(0).show();