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_first_row = $("#usersForm").find('tbody').find('tr:first');
90 var first_row_initial = $curr_first_row.find('label').html().substr(0, 1).toUpperCase();
91 var curr_shown_initial = $curr_last_row.find('label').html().substr(0, 1).toUpperCase();
92 var curr_last_row_index_string = $curr_last_row.find('input:checkbox').attr('id').match(/\d+/)[0];
93 var curr_last_row_index = parseFloat(curr_last_row_index_string);
94 var new_last_row_index = curr_last_row_index + 1;
95 var new_last_row_id = 'checkbox_sel_users_' + new_last_row_index;
96 var is_show_all = (first_row_initial != curr_shown_initial) ? true : false;
98 //Append to the table and set the id/names correctly
99 if((curr_shown_initial == new_user_initial) || is_show_all) {
101 .insertAfter($curr_last_row)
102 .find('input:checkbox')
103 .attr('id', new_last_row_id)
105 //the insert messes up the &27; part. let's fix it
106 return $(this).val().replace(/&/,'&');
110 .attr('for', new_last_row_id)
114 //Let us sort the table alphabetically
115 $("#usersForm").find('tbody').PMA_sort_table('label');
117 $("#initials_table").find('td:contains('+new_user_initial+')')
118 .html(new_user_initial_string);
126 * AJAX scripts for server_privileges page.
128 * Actions ajaxified here:
133 * Paginate table of users
137 * @name document.ready
140 $(document).ready(function() {
144 * Set a parameter for all Ajax queries made on this page. Some queries
145 * are affected by cache settings on the server side, and hence, show stale
146 * data. Don't let the web server serve cached pages
153 * AJAX event handler for 'Add a New User'
155 * @see PMA_ajaxShowMessage()
156 * @see appendNewUser()
157 * @see $cfg['AjaxEnable']
159 * @name add_user_click
162 $("#fieldset_add_user a.ajax").live("click", function(event) {
164 event.preventDefault();
166 var $msgbox = PMA_ajaxShowMessage();
169 * @var button_options Object containing options for jQueryUI dialog buttons
171 var button_options = {};
172 button_options[PMA_messages['strCreateUser']] = function() {
175 * @var $form stores reference to current form
177 var $form = $(this).find("form[name=usersForm]").last();
179 if( ! checkAddUser($form.get(0)) ) {
180 PMA_ajaxShowMessage(PMA_messages['strFormEmpty']);
184 //We also need to post the value of the submit button in order to get this to work correctly
185 $.post($form.attr('action'), $form.serialize() + "&adduser_submit=" + $(this).find("input[name=adduser_submit]").attr('value'), function(data) {
186 if(data.success == true) {
187 $("#add_user_dialog").dialog("close").remove();
188 PMA_ajaxShowMessage(data.message);
189 $("#topmenucontainer")
193 .after(data.sql_query);
195 //Remove the empty notice div generated due to a NULL query passed to PMA_showMessage()
196 var $notice_class = $("#topmenucontainer").next("div").find('.notice');
197 if($notice_class.text() == '') {
198 $notice_class.remove();
201 appendNewUser(data.new_user_string, data.new_user_initial, data.new_user_initial_string);
203 PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : "+data.error, "7000");
207 button_options[PMA_messages['strCancel']] = function() {$(this).dialog("close").remove();}
209 $.get($(this).attr("href"), {'ajax_request':true}, function(data) {
210 $('<div id="add_user_dialog"></div>')
212 .find("#fieldset_add_user_footer").hide() //showing the "Go" and "Create User" buttons together will confuse the user
214 .find("form[name=usersForm]").append('<input type="hidden" name="ajax_request" value="true" />')
217 title: PMA_messages['strAddNewUser'],
219 // height is a workaround for this Chrome problem:
220 // http://bugs.jqueryui.com/ticket/4671
221 // also it's interesting to be able to scroll this window
224 buttons: button_options
225 }); //dialog options end
226 displayPasswordGenerateButton();
227 PMA_ajaxRemoveMessage($msgbox);
230 });//end of Add New User AJAX event handler
234 * Ajax event handler for 'Reload Privileges' anchor
236 * @see PMA_ajaxShowMessage()
237 * @see $cfg['AjaxEnable']
239 * @name reload_privileges_click
241 $("#reload_privileges_anchor.ajax").live("click", function(event) {
242 event.preventDefault();
244 PMA_ajaxShowMessage(PMA_messages['strReloadingPrivileges']);
246 $.get($(this).attr("href"), {'ajax_request': true}, function(data) {
247 if(data.success == true) {
248 PMA_ajaxShowMessage(data.message);
251 PMA_ajaxShowMessage(data.error);
255 }); //end of Reload Privileges Ajax event handler
258 * AJAX handler for 'Revoke User'
260 * @see PMA_ajaxShowMessage()
261 * @see $cfg['AjaxEnable']
263 * @name revoke_user_click
265 $("#fieldset_delete_user_footer #buttonGo.ajax").live('click', function(event) {
266 event.preventDefault();
268 PMA_ajaxShowMessage(PMA_messages['strRemovingSelectedUsers']);
270 $form = $("#usersForm");
272 $.post($form.attr('action'), $form.serialize() + "&delete=" + $(this).attr('value') + "&ajax_request=true", function(data) {
273 if(data.success == true) {
274 PMA_ajaxShowMessage(data.message);
276 //Remove the revoked user from the users list
277 $form.find("input:checkbox:checked").parents("tr").slideUp("medium", function() {
278 var this_user_initial = $(this).find('input:checkbox').val().charAt(0).toUpperCase();
281 //If this is the last user with this_user_initial, remove the link from #initials_table
282 if($("#tableuserrights").find('input:checkbox[value^=' + this_user_initial + ']').length == 0) {
283 $("#initials_table").find('td > a:contains(' + this_user_initial + ')').parent('td').html(this_user_initial);
286 //Re-check the classes of each row
288 .find('tbody').find('tr:odd')
289 .removeClass('even').addClass('odd')
292 .removeClass('odd').addClass('even');
296 PMA_ajaxShowMessage(data.error);
299 }) // end Revoke User
302 * AJAX handler for 'Edit User'
304 * @see PMA_ajaxShowMessage()
309 * Step 1: Load Edit User Dialog
311 * @name edit_user_click
312 * @see $cfg['AjaxEnable']
314 $(".edit_user_anchor.ajax").live('click', function(event) {
316 event.preventDefault();
318 var $msgbox = PMA_ajaxShowMessage();
320 $(this).parents('tr').addClass('current_row');
323 * @var button_options Object containing options for jQueryUI dialog buttons
325 var button_options = {};
326 button_options[PMA_messages['strCancel']] = function() {$(this).dialog("close").remove();}
328 $.get($(this).attr('href'), {'ajax_request':true, 'edit_user_dialog': true}, function(data) {
329 $('<div id="edit_user_dialog"></div>')
334 buttons: button_options
335 }); //dialog options end
336 displayPasswordGenerateButton();
337 PMA_ajaxRemoveMessage($msgbox);
342 * Step 2: Submit the Edit User Dialog
344 * @see PMA_ajaxShowMessage()
345 * @see $cfg['AjaxEnable']
347 * @name edit_user_submit
349 $("#edit_user_dialog").find("form").live('submit', function(event) {
351 event.preventDefault();
353 PMA_ajaxShowMessage(PMA_messages['strProcessingRequest']);
355 $(this).append('<input type="hidden" name="ajax_request" value="true" />');
358 * @var curr_submit_name name of the current button being submitted
360 var curr_submit_name = $(this).find('.tblFooters').find('input:submit').attr('name');
363 * @var curr_submit_value value of the current button being submitted
365 var curr_submit_value = $(this).find('.tblFooters').find('input:submit').val();
367 $.post($(this).attr('action'), $(this).serialize() + '&' + curr_submit_name + '=' + curr_submit_value, function(data) {
368 if(data.success == true) {
370 PMA_ajaxShowMessage(data.message);
372 //Close the jQueryUI dialog
373 $("#edit_user_dialog").dialog("close").remove();
376 $("#topmenucontainer")
380 .after(data.sql_query);
381 var notice_class = $("#topmenucontainer").next("div").find('.notice');
382 if($(notice_class).text() == '') {
383 $(notice_class).remove();
385 } //Show SQL Query that was executed
387 //Append new user if necessary
388 if(data.new_user_string) {
389 appendNewUser(data.new_user_string, data.new_user_initial, data.new_user_initial_string);
392 //Check if we are on the page of the db-specific privileges
393 var db_priv_page = !!($('#dbspecificuserrights').length); // the "!!" part is merely there to ensure a value of type boolean
394 // we always need to reload on the db-specific privilege page
395 // and on the global page when adjusting global privileges,
396 // but not on the global page when adjusting db-specific privileges.
397 var reload_privs = false;
398 if (data.db_specific_privs == false || (db_priv_page == data.db_specific_privs)) {
402 //Change privileges, if they were edited and need to be reloaded
403 if(data.new_privileges && reload_privs) {
405 .find('.current_row')
407 .html(data.new_privileges);
411 .find('.current_row')
412 .removeClass('current_row');
415 PMA_ajaxShowMessage(data.error);
422 * AJAX handler for 'Export Privileges'
424 * @see PMA_ajaxShowMessage()
425 * @see $cfg['AjaxEnable']
427 * @name export_user_click
429 $(".export_user_anchor.ajax").live('click', function(event) {
431 event.preventDefault();
433 var $msgbox = PMA_ajaxShowMessage();
436 * @var button_options Object containing options for jQueryUI dialog buttons
438 var button_options = {};
439 button_options[PMA_messages['strClose']] = function() {$(this).dialog("close").remove();}
441 $.get($(this).attr('href'), {'ajax_request': true}, function(data) {
442 $('<div id="export_dialog"></div>')
446 buttons: button_options
448 PMA_ajaxRemoveMessage($msgbox);
450 }) //end export privileges
453 * AJAX handler to Paginate the Users Table
455 * @see PMA_ajaxShowMessage()
456 * @see $cfg['AjaxEnable']
457 * @name paginate_users_table_click
460 $("#initials_table.ajax").find("a").live('click', function(event) {
461 event.preventDefault();
463 var $msgbox = PMA_ajaxShowMessage();
465 $.get($(this).attr('href'), {'ajax_request' : true}, function(data) {
466 // This form is not on screen when first entering Privileges
467 // if there are more than 50 users
468 $("#usersForm").hide("medium").remove();
469 $("#fieldset_add_user").hide("medium").remove();
471 .after(data).show("medium")
472 .siblings("h2").not(":first").remove();
474 PMA_ajaxRemoveMessage($msgbox);
476 })// end of the paginate users table
479 * Additional confirmation dialog after clicking
480 * 'Drop the databases...'
482 $('#checkbox_drop_users_db').click(function() {
483 $this_checkbox = $(this);
484 if ($this_checkbox.is(':checked')) {
485 var is_confirmed = confirm(PMA_messages['strDropDatabaseStrongWarning'] + '\n' + PMA_messages['strDoYouReally'] + ' :\nDROP DATABASE');
486 if (! is_confirmed) {
487 $this_checkbox.attr('checked', false);
492 }, 'top.frame_content'); //end $(document).ready()