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
14 * Validates the password field in a form
16 * @see PMA_messages['strPasswordEmpty']
17 * @see PMA_messages['strPasswordNotSame']
18 * @param object the form
19 * @return boolean whether the field value is valid or not
21 function checkPassword(the_form)
23 // Did the user select 'no password'?
24 if (typeof(the_form.elements['nopass']) != 'undefined'
25 && the_form.elements['nopass'][0].checked) {
27 } else if (typeof(the_form.elements['pred_password']) != 'undefined'
28 && (the_form.elements['pred_password'].value == 'none'
29 || the_form.elements['pred_password'].value == 'keep')) {
33 var password = the_form.elements['pma_pw'];
34 var password_repeat = the_form.elements['pma_pw2'];
35 var alert_msg = false;
37 if (password.value == '') {
38 alert_msg = PMA_messages['strPasswordEmpty'];
39 } else if (password.value != password_repeat.value) {
40 alert_msg = PMA_messages['strPasswordNotSame'];
46 password_repeat.value = '';
52 } // end of the 'checkPassword()' function
56 * Validates the "add a user" form
58 * @return boolean whether the form is validated or not
60 function checkAddUser(the_form)
62 if (the_form.elements['pred_hostname'].value == 'userdefined' && the_form.elements['hostname'].value == '') {
63 alert(PMA_messages['strHostEmpty']);
64 the_form.elements['hostname'].focus();
68 if (the_form.elements['pred_username'].value == 'userdefined' && the_form.elements['username'].value == '') {
69 alert(PMA_messages['strUserEmpty']);
70 the_form.elements['username'].focus();
74 return checkPassword(the_form);
75 } // end of the 'checkAddUser()' function
79 * Generate a new password and copy it to the password input areas
81 * @param object the form that holds the password fields
83 * @return boolean always true
85 function suggestPassword(passwd_form) {
86 // restrict the password to just letters and numbers to avoid problems:
87 // "editors and viewers regard the password as multiple words and
88 // things like double click no longer work"
89 var pwchars = "abcdefhjmnpqrstuvwxyz23456789ABCDEFGHJKLMNPQRSTUVWYXZ";
90 var passwordlength = 16; // do we want that to be dynamic? no, keep it simple :)
91 var passwd = passwd_form.generated_pw;
94 for ( i = 0; i < passwordlength; i++ ) {
95 passwd.value += pwchars.charAt( Math.floor( Math.random() * pwchars.length ) )
97 passwd_form.text_pma_pw.value = passwd.value;
98 passwd_form.text_pma_pw2.value = passwd.value;
103 * When a new user is created and retrieved over Ajax, append the user's row to
106 * @param new_user_string the html for the new user's row
107 * @param new_user_initial the first alphabet of the user's name
108 * @param new_user_initial_string html to replace the initial for pagination
110 function appendNewUser(new_user_string, new_user_initial, new_user_initial_string) {
111 //Append the newly retrived user to the table now
113 //Calculate the index for the new row
114 var curr_last_row = $("#usersForm").find('tbody').find('tr:last');
115 var curr_last_row_index_string = $(curr_last_row).find('input:checkbox').attr('id').match(/\d+/)[0];
116 var curr_last_row_index = parseFloat(curr_last_row_index_string);
117 var new_last_row_index = curr_last_row_index + 1;
118 var new_last_row_id = 'checkbox_sel_users_' + new_last_row_index;
120 //Append to the table and set the id/names correctly
122 .insertAfter($(curr_last_row))
123 .find('input:checkbox')
124 .attr('id', new_last_row_id)
126 //the insert messes up the &27; part. let's fix it
127 return $(this).val().replace(/&/,'&');
131 .attr('for', new_last_row_id)
134 //Let us sort the table alphabetically
135 $("#usersForm").find('tbody').PMA_sort_table('label');
137 $("#initials_table").find('td:contains('+new_user_initial+')')
138 .html(new_user_initial_string);
146 * AJAX scripts for server_privileges page.
148 * Actions ajaxified here:
153 * Paginate table of users
157 * @name document.ready
160 $(document).ready(function() {
164 * Set a parameter for all Ajax queries made on this page. Some queries
165 * are affected by cache settings on the server side, and hence, show stale
166 * data. Don't let the web server serve cached pages
173 * AJAX event handler for 'Add a New User'
175 * @see PMA_ajaxShowMessage()
176 * @see appendNewUser()
178 * @name add_user_click
181 $("#fieldset_add_user a").live("click", function(event) {
183 event.preventDefault();
185 PMA_ajaxShowMessage();
188 * @var button_options Object containing options for jQueryUI dialog buttons
190 var button_options = {};
191 button_options[PMA_messages['strCreateUser']] = function() {
194 * @var the_form stores reference to current form
196 var the_form = $(this).find("#addUsersForm");
198 if( ! checkAddUser($(the_form).get(0)) ) {
199 PMA_ajaxShowMessage(PMA_messages['strFormEmpty']);
203 //We also need to post the value of the submit button in order to get this to work correctly
204 $.post($(the_form).attr('action'), $(the_form).serialize() + "&adduser_submit=" + $(this).find("input[name=adduser_submit]").attr('value'), function(data) {
205 if(data.success == true) {
206 $("#add_user_dialog").dialog("close").remove();
207 PMA_ajaxShowMessage(data.message);
208 $("#topmenucontainer")
212 .after(data.sql_query);
214 //Remove the empty notice div generated due to a NULL query passed to PMA_showMessage()
215 var notice_class = $("#topmenucontainer").next("div").find('.notice');
216 if($(notice_class).text() == '') {
217 $(notice_class).remove();
220 appendNewUser(data.new_user_string, data.new_user_initial, data.new_user_initial_string);
223 PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : "+data.error, "7000");
227 button_options[PMA_messages['strCancel']] = function() {$(this).dialog("close").remove();}
229 $.get($(this).attr("href"), {'ajax_request':true}, function(data) {
230 $('<div id="add_user_dialog"></div>')
232 .find("#fieldset_add_user_footer").hide() //showing the "Go" and "Create User" buttons together will confuse the user
234 .find("#addUsersForm").append('<input type="hidden" name="ajax_request" value="true" />')
237 title: top.frame_content.PMA_messages['strAddNewUser'],
240 buttons: button_options
241 }); //dialog options end
244 });//end of Add New User AJAX event handler
248 * Ajax event handler for 'Reload Privileges' anchor
250 * @see PMA_ajaxShowMessage()
252 * @name reload_privileges_click
254 $("#reload_privileges_anchor").live("click", function(event) {
255 event.preventDefault();
257 PMA_ajaxShowMessage(PMA_messages['strReloadingPrivileges']);
259 $.get($(this).attr("href"), {'ajax_request': true}, function(data) {
260 if(data.success == true) {
261 PMA_ajaxShowMessage(data.message);
264 PMA_ajaxShowMessage(data.error);
268 }); //end of Reload Privileges Ajax event handler
271 * AJAX handler for 'Revoke User'
273 * @see PMA_ajaxShowMessage()
275 * @name revoke_user_click
277 $("#fieldset_delete_user_footer #buttonGo").live('click', function(event) {
278 event.preventDefault();
280 PMA_ajaxShowMessage(PMA_messages['strRemovingSelectedUsers']);
282 $.post($("#usersForm").attr('action'), $("#usersForm").serialize() + "&delete=" + $(this).attr('value') + "&ajax_request=true", function(data) {
283 if(data.success == true) {
284 PMA_ajaxShowMessage(data.message);
286 //Remove the revoked user from the users list
287 $("#usersForm").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');
306 PMA_ajaxShowMessage(data.error);
309 }) // end Revoke User
312 * AJAX handler for 'Edit User'
314 * @see PMA_ajaxShowMessage()
319 * Step 1: Load Edit User Dialog
321 * @name edit_user_click
323 $(".edit_user_anchor").live('click', function(event) {
325 event.preventDefault();
327 PMA_ajaxShowMessage();
329 $(this).parents('tr').addClass('current_row');
332 * @var button_options Object containing options for jQueryUI dialog buttons
334 var button_options = {};
335 button_options[PMA_messages['strCancel']] = function() {$(this).dialog("close").remove();}
337 $.get($(this).attr('href'), {'ajax_request':true, 'edit_user_dialog': true}, function(data) {
338 $('<div id="edit_user_dialog"></div>')
342 buttons: button_options
348 * Step 2: Submit the Edit User Dialog
350 * @see PMA_ajaxShowMessage()
352 * @name edit_user_submit
354 $("#edit_user_dialog").find("form").live('submit', function(event) {
356 event.preventDefault();
358 PMA_ajaxShowMessage(PMA_messages['strProcessingRequest']);
360 $(this).append('<input type="hidden" name="ajax_request" value="true" />');
363 * @var curr_submit_name name of the current button being submitted
365 var curr_submit_name = $(this).find('.tblFooters').find('input:submit').attr('name');
368 * @var curr_submit_value value of the current button being submitted
370 var curr_submit_value = $(this).find('.tblFooters').find('input:submit').val();
372 $.post($(this).attr('action'), $(this).serialize() + '&' + curr_submit_name + '=' + curr_submit_value, function(data) {
373 if(data.success == true) {
375 PMA_ajaxShowMessage(data.message);
377 //Close the jQueryUI dialog
378 $("#edit_user_dialog").dialog("close").remove();
381 $("#topmenucontainer")
385 .after(data.sql_query);
386 var notice_class = $("#topmenucontainer").next("div").find('.notice');
387 if($(notice_class).text() == '') {
388 $(notice_class).remove();
390 } //Show SQL Query that was executed
392 //Append new user if necessary
393 if(data.new_user_string) {
394 appendNewUser(data.new_user_string, data.new_user_initial, data.new_user_initial_string);
397 //Change privileges if they were edited
398 if(data.new_privileges) {
400 .find('.current_row')
402 .html(data.new_privileges);
406 .find('.current_row')
407 .removeClass('current_row');
410 PMA_ajaxShowMessage(data.error);
417 * AJAX handler for 'Export Privileges'
419 * @see PMA_ajaxShowMessage()
421 * @name export_user_click
423 $(".export_user_anchor").live('click', function(event) {
425 event.preventDefault();
427 PMA_ajaxShowMessage();
430 * @var button_options Object containing options for jQueryUI dialog buttons
432 var button_options = {};
433 button_options[PMA_messages['strClose']] = function() {$(this).dialog("close").remove();}
435 $.get($(this).attr('href'), {'ajax_request': true}, function(data) {
436 $('<div id="export_dialog"></div>')
440 buttons: button_options
443 }) //end export privileges
446 * AJAX handler to Paginate the Users Table
448 * @see PMA_ajaxShowMessage()
449 * @name paginate_users_table_click
452 $("#initials_table").find("a").live('click', function(event) {
453 event.preventDefault();
455 PMA_ajaxShowMessage();
457 $.get($(this).attr('href'), {'ajax_request' : true}, function(data) {
460 .siblings("#initials_table")
465 $("#initials_table").siblings("h2").not(":first").remove();
467 })// end of the paginate users table
469 }, 'top.frame_content'); //end $(document).ready()