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
78 * Generate a new password and copy it to the password input areas
80 * @param object the form that holds the password fields
82 * @return boolean always true
84 function suggestPassword(passwd_form) {
85 // restrict the password to just letters and numbers to avoid problems:
86 // "editors and viewers regard the password as multiple words and
87 // things like double click no longer work"
88 var pwchars = "abcdefhjmnpqrstuvwxyz23456789ABCDEFGHJKLMNPQRSTUVWYXZ";
89 var passwordlength = 16; // do we want that to be dynamic? no, keep it simple :)
90 var passwd = passwd_form.generated_pw;
93 for ( i = 0; i < passwordlength; i++ ) {
94 passwd.value += pwchars.charAt( Math.floor( Math.random() * pwchars.length ) )
96 passwd_form.text_pma_pw.value = passwd.value;
97 passwd_form.text_pma_pw2.value = passwd.value;
102 * When a new user is created and retrieved over Ajax, append the user's row to
105 * @param new_user_string the html for the new user's row
106 * @param new_user_initial the first alphabet of the user's name
107 * @param new_user_initial_string html to replace the initial for pagination
109 function appendNewUser(new_user_string, new_user_initial, new_user_initial_string) {
110 //Append the newly retrived user to the table now
112 //Calculate the index for the new row
113 var curr_last_row = $("#usersForm").find('tbody').find('tr:last');
114 var curr_last_row_index_string = $(curr_last_row).find('input:checkbox').attr('id').match(/\d+/)[0];
115 var curr_last_row_index = parseFloat(curr_last_row_index_string);
116 var new_last_row_index = curr_last_row_index + 1;
117 var new_last_row_id = 'checkbox_sel_users_' + new_last_row_index;
119 //Append to the table and set the id/names correctly
121 .insertAfter($(curr_last_row))
122 .find('input:checkbox')
123 .attr('id', new_last_row_id)
125 //the insert messes up the &27; part. let's fix it
126 return $(this).val().replace(/&/,'&');
130 .attr('for', new_last_row_id)
133 //Let us sort the table alphabetically
134 $("#usersForm").find('tbody').PMA_sort_table('label');
136 $("#initials_table").find('td:contains('+new_user_initial+')')
137 .html(new_user_initial_string);
145 * AJAX scripts for server_privileges page.
147 * Actions ajaxified here:
152 * Paginate table of users
156 * @name document.ready
159 $(document).ready(function() {
163 * Set a parameter for all Ajax queries made on this page. Some queries
164 * are affected by cache settings on the server side, and hence, show stale
165 * data. Don't let the web server serve cached pages
172 * AJAX event handler for 'Add a New User'
174 * @see PMA_ajaxShowMessage()
175 * @see appendNewUser()
177 * @name add_user_click
180 $("#fieldset_add_user a").live("click", function(event) {
182 event.preventDefault();
184 PMA_ajaxShowMessage();
187 * @var button_options Object containing options for jQueryUI dialog buttons
189 var button_options = {};
190 button_options[PMA_messages['strCreateUser']] = function() {
193 * @var the_form stores reference to current form
195 var the_form = $(this).find("#addUsersForm");
197 if( ! checkAddUser($(the_form).get(0)) ) {
198 PMA_ajaxShowMessage(PMA_messages['strFormEmpty']);
202 //We also need to post the value of the submit button in order to get this to work correctly
203 $.post($(the_form).attr('action'), $(the_form).serialize() + "&adduser_submit=" + $(this).find("input[name=adduser_submit]").attr('value'), function(data) {
204 if(data.success == true) {
205 $("#add_user_dialog").dialog("close").remove();
206 PMA_ajaxShowMessage(data.message);
207 $("#topmenucontainer")
211 .after(data.sql_query);
213 //Remove the empty notice div generated due to a NULL query passed to PMA_showMessage()
214 var notice_class = $("#topmenucontainer").next("div").find('.notice');
215 if($(notice_class).text() == '') {
216 $(notice_class).remove();
219 appendNewUser(data.new_user_string, data.new_user_initial, data.new_user_initial_string);
222 PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : "+data.error, "7000");
226 button_options[PMA_messages['strCancel']] = function() {$(this).dialog("close").remove();}
228 $.get($(this).attr("href"), {'ajax_request':true}, function(data) {
229 $('<div id="add_user_dialog"></div>')
231 .find("#fieldset_add_user_footer").hide() //showing the "Go" and "Create User" buttons together will confuse the user
233 .find("#addUsersForm").append('<input type="hidden" name="ajax_request" value="true" />')
236 title: top.frame_content.PMA_messages['strAddNewUser'],
239 buttons: button_options
240 }); //dialog options end
243 });//end of Add New User AJAX event handler
247 * Ajax event handler for 'Reload Privileges' anchor
249 * @see PMA_ajaxShowMessage()
251 * @name reload_privileges_click
253 $("#reload_privileges_anchor").live("click", function(event) {
254 event.preventDefault();
256 PMA_ajaxShowMessage(PMA_messages['strReloadingPrivileges']);
258 $.get($(this).attr("href"), {'ajax_request': true}, function(data) {
259 if(data.success == true) {
260 PMA_ajaxShowMessage(data.message);
263 PMA_ajaxShowMessage(data.error);
267 }); //end of Reload Privileges Ajax event handler
270 * AJAX handler for 'Revoke User'
272 * @see PMA_ajaxShowMessage()
274 * @name revoke_user_click
276 $("#fieldset_delete_user_footer #buttonGo").live('click', function(event) {
277 event.preventDefault();
279 PMA_ajaxShowMessage(PMA_messages['strRemovingSelectedUsers']);
281 $.post($("#usersForm").attr('action'), $("#usersForm").serialize() + "&delete=" + $(this).attr('value') + "&ajax_request=true", function(data) {
282 if(data.success == true) {
283 PMA_ajaxShowMessage(data.message);
285 //Remove the revoked user from the users list
286 $("#usersForm").find("input:checkbox:checked").parents("tr").slideUp("medium", function() {
287 var this_user_initial = $(this).find('input:checkbox').val().charAt(0).toUpperCase();
290 //If this is the last user with this_user_initial, remove the link from #initials_table
291 if($("#tableuserrights").find('input:checkbox[value^=' + this_user_initial + ']').length == 0) {
292 $("#initials_table").find('td > a:contains(' + this_user_initial + ')').parent('td').html(this_user_initial);
295 //Re-check the classes of each row
297 .find('tbody').find('tr:odd')
298 .removeClass('even').addClass('odd')
301 .removeClass('odd').addClass('even');
305 PMA_ajaxShowMessage(data.error);
308 }) // end Revoke User
311 * AJAX handler for 'Edit User'
313 * @see PMA_ajaxShowMessage()
318 * Step 1: Load Edit User Dialog
320 * @name edit_user_click
322 $(".edit_user_anchor").live('click', function(event) {
324 event.preventDefault();
326 PMA_ajaxShowMessage();
328 $(this).parents('tr').addClass('current_row');
331 * @var button_options Object containing options for jQueryUI dialog buttons
333 var button_options = {};
334 button_options[PMA_messages['strCancel']] = function() {$(this).dialog("close").remove();}
336 $.get($(this).attr('href'), {'ajax_request':true, 'edit_user_dialog': true}, function(data) {
337 $('<div id="edit_user_dialog"></div>')
341 buttons: button_options
347 * Step 2: Submit the Edit User Dialog
349 * @see PMA_ajaxShowMessage()
351 * @name edit_user_submit
353 $("#edit_user_dialog").find("form").live('submit', function(event) {
355 event.preventDefault();
357 PMA_ajaxShowMessage(PMA_messages['strProcessingRequest']);
359 $(this).append('<input type="hidden" name="ajax_request" value="true" />');
362 * @var curr_submit_name name of the current button being submitted
364 var curr_submit_name = $(this).find('.tblFooters').find('input:submit').attr('name');
367 * @var curr_submit_value value of the current button being submitted
369 var curr_submit_value = $(this).find('.tblFooters').find('input:submit').val();
371 $.post($(this).attr('action'), $(this).serialize() + '&' + curr_submit_name + '=' + curr_submit_value, function(data) {
372 if(data.success == true) {
374 PMA_ajaxShowMessage(data.message);
376 //Close the jQueryUI dialog
377 $("#edit_user_dialog").dialog("close").remove();
380 $("#topmenucontainer")
384 .after(data.sql_query);
385 var notice_class = $("#topmenucontainer").next("div").find('.notice');
386 if($(notice_class).text() == '') {
387 $(notice_class).remove();
389 } //Show SQL Query that was executed
391 //Append new user if necessary
392 if(data.new_user_string) {
393 appendNewUser(data.new_user_string, data.new_user_initial, data.new_user_initial_string);
396 //Change privileges if they were edited
397 if(data.new_privileges) {
399 .find('.current_row')
401 .html(data.new_privileges);
405 .find('.current_row')
406 .removeClass('current_row');
409 PMA_ajaxShowMessage(data.error);
416 * AJAX handler for 'Export Privileges'
418 * @see PMA_ajaxShowMessage()
420 * @name export_user_click
422 $(".export_user_anchor").live('click', function(event) {
424 event.preventDefault();
426 PMA_ajaxShowMessage();
429 * @var button_options Object containing options for jQueryUI dialog buttons
431 var button_options = {};
432 button_options[PMA_messages['strClose']] = function() {$(this).dialog("close").remove();}
434 $.get($(this).attr('href'), {'ajax_request': true}, function(data) {
435 $('<div id="export_dialog"></div>')
439 buttons: button_options
442 }) //end export privileges
445 * AJAX handler to Paginate the Users Table
447 * @see PMA_ajaxShowMessage()
448 * @name paginate_users_table_click
451 $("#initials_table").find("a").live('click', function(event) {
452 event.preventDefault();
454 PMA_ajaxShowMessage();
456 $.get($(this).attr('href'), {'ajax_request' : true}, function(data) {
459 .siblings("#initials_table")
464 $("#initials_table").siblings("h2").not(":first").remove();
466 })// end of the paginate users table
468 }, 'top.frame_content'); //end $(document).ready()