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 password field in a form
15 * @see PMA_messages['strPasswordEmpty']
16 * @see PMA_messages['strPasswordNotSame']
17 * @param object the form
18 * @return boolean whether the field value is valid or not
20 function checkPassword(the_form)
22 // Did the user select 'no password'?
23 if (typeof(the_form.elements['nopass']) != 'undefined'
24 && the_form.elements['nopass'][0].checked) {
26 } else if (typeof(the_form.elements['pred_password']) != 'undefined'
27 && (the_form.elements['pred_password'].value == 'none'
28 || the_form.elements['pred_password'].value == 'keep')) {
32 var password = the_form.elements['pma_pw'];
33 var password_repeat = the_form.elements['pma_pw2'];
34 var alert_msg = false;
36 if (password.value == '') {
37 alert_msg = PMA_messages['strPasswordEmpty'];
38 } else if (password.value != password_repeat.value) {
39 alert_msg = PMA_messages['strPasswordNotSame'];
45 password_repeat.value = '';
51 } // end of the 'checkPassword()' function
55 * Validates the "add a user" form
57 * @return boolean whether the form is validated or not
59 function checkAddUser(the_form)
61 if (the_form.elements['pred_hostname'].value == 'userdefined' && the_form.elements['hostname'].value == '') {
62 alert(PMA_messages['strHostEmpty']);
63 the_form.elements['hostname'].focus();
67 if (the_form.elements['pred_username'].value == 'userdefined' && the_form.elements['username'].value == '') {
68 alert(PMA_messages['strUserEmpty']);
69 the_form.elements['username'].focus();
73 return checkPassword(the_form);
74 } // end of the 'checkAddUser()' function
77 * When a new user is created and retrieved over Ajax, append the user's row to
80 * @param new_user_string the html for the new user's row
81 * @param new_user_initial the first alphabet of the user's name
82 * @param new_user_initial_string html to replace the initial for pagination
84 function appendNewUser(new_user_string, new_user_initial, new_user_initial_string) {
85 //Append the newly retrived user to the table now
87 //Calculate the index for the new row
88 var curr_last_row = $("#usersForm").find('tbody').find('tr:last');
89 var curr_last_row_index_string = $(curr_last_row).find('input:checkbox').attr('id').match(/\d+/)[0];
90 var curr_last_row_index = parseFloat(curr_last_row_index_string);
91 var new_last_row_index = curr_last_row_index + 1;
92 var new_last_row_id = 'checkbox_sel_users_' + new_last_row_index;
94 //Append to the table and set the id/names correctly
96 .insertAfter($(curr_last_row))
97 .find('input:checkbox')
98 .attr('id', new_last_row_id)
100 //the insert messes up the &27; part. let's fix it
101 return $(this).val().replace(/&/,'&');
105 .attr('for', new_last_row_id)
108 //Let us sort the table alphabetically
109 $("#usersForm").find('tbody').PMA_sort_table('label');
111 $("#initials_table").find('td:contains('+new_user_initial+')')
112 .html(new_user_initial_string);
120 * AJAX scripts for server_privileges page.
122 * Actions ajaxified here:
127 * Paginate table of users
131 * @name document.ready
134 $(document).ready(function() {
138 * Set a parameter for all Ajax queries made on this page. Some queries
139 * are affected by cache settings on the server side, and hence, show stale
140 * data. Don't let the web server serve cached pages
147 * AJAX event handler for 'Add a New User'
149 * @see PMA_ajaxShowMessage()
150 * @see appendNewUser()
152 * @name add_user_click
155 $("#fieldset_add_user a").live("click", function(event) {
157 event.preventDefault();
159 PMA_ajaxShowMessage();
162 * @var button_options Object containing options for jQueryUI dialog buttons
164 var button_options = {};
165 button_options[PMA_messages['strCreateUser']] = function() {
168 * @var $form stores reference to current form
170 var $form = $(this).find("#addUsersForm");
172 if( ! checkAddUser($form.get(0)) ) {
173 PMA_ajaxShowMessage(PMA_messages['strFormEmpty']);
177 //We also need to post the value of the submit button in order to get this to work correctly
178 $.post($form.attr('action'), $form.serialize() + "&adduser_submit=" + $(this).find("input[name=adduser_submit]").attr('value'), function(data) {
179 if(data.success == true) {
180 $("#add_user_dialog").dialog("close").remove();
181 PMA_ajaxShowMessage(data.message);
182 $("#topmenucontainer")
186 .after(data.sql_query);
188 //Remove the empty notice div generated due to a NULL query passed to PMA_showMessage()
189 var notice_class = $("#topmenucontainer").next("div").find('.notice');
190 if($(notice_class).text() == '') {
191 $(notice_class).remove();
194 appendNewUser(data.new_user_string, data.new_user_initial, data.new_user_initial_string);
196 PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : "+data.error, "7000");
200 button_options[PMA_messages['strCancel']] = function() {$(this).dialog("close").remove();}
202 $.get($(this).attr("href"), {'ajax_request':true}, function(data) {
203 $('<div id="add_user_dialog"></div>')
205 .find("#fieldset_add_user_footer").hide() //showing the "Go" and "Create User" buttons together will confuse the user
207 .find("#addUsersForm").append('<input type="hidden" name="ajax_request" value="true" />')
210 title: PMA_messages['strAddNewUser'],
212 // height is a workaround for this Chrome problem:
213 // http://bugs.jqueryui.com/ticket/4671
216 buttons: button_options
217 }); //dialog options end
218 displayPasswordGenerateButton();
221 });//end of Add New User AJAX event handler
225 * Ajax event handler for 'Reload Privileges' anchor
227 * @see PMA_ajaxShowMessage()
229 * @name reload_privileges_click
231 $("#reload_privileges_anchor").live("click", function(event) {
232 event.preventDefault();
234 PMA_ajaxShowMessage(PMA_messages['strReloadingPrivileges']);
236 $.get($(this).attr("href"), {'ajax_request': true}, function(data) {
237 if(data.success == true) {
238 PMA_ajaxShowMessage(data.message);
241 PMA_ajaxShowMessage(data.error);
245 }); //end of Reload Privileges Ajax event handler
248 * AJAX handler for 'Revoke User'
250 * @see PMA_ajaxShowMessage()
252 * @name revoke_user_click
254 $("#fieldset_delete_user_footer #buttonGo").live('click', function(event) {
255 event.preventDefault();
257 PMA_ajaxShowMessage(PMA_messages['strRemovingSelectedUsers']);
259 $.post($("#usersForm").attr('action'), $("#usersForm").serialize() + "&delete=" + $(this).attr('value') + "&ajax_request=true", function(data) {
260 if(data.success == true) {
261 PMA_ajaxShowMessage(data.message);
263 //Remove the revoked user from the users list
264 $("#usersForm").find("input:checkbox:checked").parents("tr").slideUp("medium", function() {
265 var this_user_initial = $(this).find('input:checkbox').val().charAt(0).toUpperCase();
268 //If this is the last user with this_user_initial, remove the link from #initials_table
269 if($("#tableuserrights").find('input:checkbox[value^=' + this_user_initial + ']').length == 0) {
270 $("#initials_table").find('td > a:contains(' + this_user_initial + ')').parent('td').html(this_user_initial);
273 //Re-check the classes of each row
275 .find('tbody').find('tr:odd')
276 .removeClass('even').addClass('odd')
279 .removeClass('odd').addClass('even');
283 PMA_ajaxShowMessage(data.error);
286 }) // end Revoke User
289 * AJAX handler for 'Edit User'
291 * @see PMA_ajaxShowMessage()
296 * Step 1: Load Edit User Dialog
298 * @name edit_user_click
300 $(".edit_user_anchor").live('click', function(event) {
302 event.preventDefault();
304 PMA_ajaxShowMessage();
306 $(this).parents('tr').addClass('current_row');
309 * @var button_options Object containing options for jQueryUI dialog buttons
311 var button_options = {};
312 button_options[PMA_messages['strCancel']] = function() {$(this).dialog("close").remove();}
314 $.get($(this).attr('href'), {'ajax_request':true, 'edit_user_dialog': true}, function(data) {
315 $('<div id="edit_user_dialog"></div>')
319 buttons: button_options
320 }); //dialog options end
321 displayPasswordGenerateButton();
326 * Step 2: Submit the Edit User Dialog
328 * @see PMA_ajaxShowMessage()
330 * @name edit_user_submit
332 $("#edit_user_dialog").find("form").live('submit', function(event) {
334 event.preventDefault();
336 PMA_ajaxShowMessage(PMA_messages['strProcessingRequest']);
338 $(this).append('<input type="hidden" name="ajax_request" value="true" />');
341 * @var curr_submit_name name of the current button being submitted
343 var curr_submit_name = $(this).find('.tblFooters').find('input:submit').attr('name');
346 * @var curr_submit_value value of the current button being submitted
348 var curr_submit_value = $(this).find('.tblFooters').find('input:submit').val();
350 $.post($(this).attr('action'), $(this).serialize() + '&' + curr_submit_name + '=' + curr_submit_value, function(data) {
351 if(data.success == true) {
353 PMA_ajaxShowMessage(data.message);
355 //Close the jQueryUI dialog
356 $("#edit_user_dialog").dialog("close").remove();
359 $("#topmenucontainer")
363 .after(data.sql_query);
364 var notice_class = $("#topmenucontainer").next("div").find('.notice');
365 if($(notice_class).text() == '') {
366 $(notice_class).remove();
368 } //Show SQL Query that was executed
370 //Append new user if necessary
371 if(data.new_user_string) {
372 appendNewUser(data.new_user_string, data.new_user_initial, data.new_user_initial_string);
375 //Change privileges if they were edited
376 if(data.new_privileges) {
378 .find('.current_row')
380 .html(data.new_privileges);
384 .find('.current_row')
385 .removeClass('current_row');
388 PMA_ajaxShowMessage(data.error);
395 * AJAX handler for 'Export Privileges'
397 * @see PMA_ajaxShowMessage()
399 * @name export_user_click
401 $(".export_user_anchor").live('click', function(event) {
403 event.preventDefault();
405 PMA_ajaxShowMessage();
408 * @var button_options Object containing options for jQueryUI dialog buttons
410 var button_options = {};
411 button_options[PMA_messages['strClose']] = function() {$(this).dialog("close").remove();}
413 $.get($(this).attr('href'), {'ajax_request': true}, function(data) {
414 $('<div id="export_dialog"></div>')
418 buttons: button_options
421 }) //end export privileges
424 * AJAX handler to Paginate the Users Table
426 * @see PMA_ajaxShowMessage()
427 * @name paginate_users_table_click
430 $("#initials_table").find("a").live('click', function(event) {
431 event.preventDefault();
433 PMA_ajaxShowMessage();
435 $.get($(this).attr('href'), {'ajax_request' : true}, function(data) {
436 // This form is not on screen when first entering Privileges
437 // if there are more than 50 users
438 $("#usersForm").hide("medium").remove();
439 $("#fieldset_add_user").hide("medium").remove();
441 .after(data).show("medium")
442 .siblings("h2").not(":first").remove();
444 })// end of the paginate users table
447 * Additional confirmation dialog after clicking
448 * 'Drop the databases...'
450 $('#checkbox_drop_users_db').click(function() {
451 $this_checkbox = $(this);
452 if ($this_checkbox.is(':checked')) {
453 var is_confirmed = confirm(PMA_messages['strDropDatabaseStrongWarning'] + '\n' + PMA_messages['strDoYouReally'] + ' :\nDROP DATABASE');
454 if (! is_confirmed) {
455 $this_checkbox.attr('checked', false);
460 }, 'top.frame_content'); //end $(document).ready()