patch for Copy set of tables to new name pattern.
[phpmyadmin/dennischen.git] / js / server_privileges.js
blob332ad6b856bd13024a188e67b3272575bb1ecd5b
1 /* vim: set expandtab sw=4 ts=4 sts=4: */
2 /**
3  * @fileoverview    functions used in server privilege pages
4  * @name            Server Privileges
5  *
6  * @requires    jQuery
7  * @requires    jQueryUI
8  * @requires    js/functions.js
9  *
10  */
12 /**
13  * Validates the password field in a form
14  *
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
19  */
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) {
25         return true;
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')) {
29         return true;
30     }
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'];
40     }
42     if (alert_msg) {
43         alert(alert_msg);
44         password.value  = '';
45         password_repeat.value = '';
46         password.focus();
47         return false;
48     }
50     return true;
51 } // end of the 'checkPassword()' function
54 /**
55  * Validates the "add a user" form
56  *
57  * @return  boolean  whether the form is validated or not
58  */
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();
64         return false;
65     }
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();
70         return false;
71     }
73     return checkPassword(the_form);
74 } // end of the 'checkAddUser()' function
76 /**
77  * When a new user is created and retrieved over Ajax, append the user's row to
78  * the user's table
79  *
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
83  */
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) {
100         $(new_user_string)
101         .insertAfter($curr_last_row)
102         .find('input:checkbox')
103         .attr('id', new_last_row_id)
104         .val(function() {
105             //the insert messes up the &27; part. let's fix it
106             return $(this).val().replace(/&/,'&');
107         })
108         .end()
109         .find('label')
110         .attr('for', new_last_row_id)
111         .end();
112     }
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);
121 /**#@+
122  * @namespace   jQuery
123  */
126  * AJAX scripts for server_privileges page.
128  * Actions ajaxified here:
129  * Add a new user
130  * Revoke a user
131  * Edit privileges
132  * Export privileges
133  * Paginate table of users
134  * Flush privileges
136  * @memberOf    jQuery
137  * @name        document.ready
138  */
140 $(document).ready(function() {
141     /** @lends jQuery */
143     /**
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
147      */
148     $.ajaxSetup({
149         cache: 'false'
150     });
152     /**
153      * AJAX event handler for 'Add a New User'
154      *
155      * @see         PMA_ajaxShowMessage()
156      * @see         appendNewUser()
157      * @see         $cfg['AjaxEnable'] 
158      * @memberOf    jQuery
159      * @name        add_user_click
160      *
161      */
162     $("#fieldset_add_user a.ajax").live("click", function(event) {
163         /** @lends jQuery */
164         event.preventDefault();
166         PMA_ajaxShowMessage();
168         /**
169          * @var button_options  Object containing options for jQueryUI dialog buttons
170          */
171         var button_options = {};
172         button_options[PMA_messages['strCreateUser']] = function() {
174             /**
175              * @var $form    stores reference to current form
176              */
177             var $form = $(this).find("#addUsersForm");
179             if( ! checkAddUser($form.get(0)) ) {
180                 PMA_ajaxShowMessage(PMA_messages['strFormEmpty']);
181                 return false;
182             }
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")
190                      .next('div')
191                      .remove()
192                      .end()
193                      .after(data.sql_query);
194                                                                 
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();
199                      }
201                      appendNewUser(data.new_user_string, data.new_user_initial, data.new_user_initial_string);
202                 } else {
203                      PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : "+data.error, "7000");
204                 }
205             })
206         };
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>')
211             .prepend(data)
212             .find("#fieldset_add_user_footer").hide() //showing the "Go" and "Create User" buttons together will confuse the user
213             .end()
214             .find("#addUsersForm").append('<input type="hidden" name="ajax_request" value="true" />')
215             .end()
216             .dialog({
217                 title: PMA_messages['strAddNewUser'],
218                 width: 800,
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
222                 height: 600,
223                 modal: true,
224                 buttons: button_options
225             }); //dialog options end
226             displayPasswordGenerateButton();
227         }); // end $.get()
229     });//end of Add New User AJAX event handler
232     /**
233      * Ajax event handler for 'Reload Privileges' anchor
234      *
235      * @see         PMA_ajaxShowMessage()
236      * @see         $cfg['AjaxEnable'] 
237      * @memberOf    jQuery
238      * @name        reload_privileges_click
239      */
240     $("#reload_privileges_anchor.ajax").live("click", function(event) {
241         event.preventDefault();
243         PMA_ajaxShowMessage(PMA_messages['strReloadingPrivileges']);
245         $.get($(this).attr("href"), {'ajax_request': true}, function(data) {
246             if(data.success == true) {
247                 PMA_ajaxShowMessage(data.message);
248             }
249             else {
250                 PMA_ajaxShowMessage(data.error);
251             }
252         }); //end $.get()
254     }); //end of Reload Privileges Ajax event handler
256     /**
257      * AJAX handler for 'Revoke User'
258      *
259      * @see         PMA_ajaxShowMessage()
260      * @see         $cfg['AjaxEnable'] 
261      * @memberOf    jQuery
262      * @name        revoke_user_click
263      */
264     $("#fieldset_delete_user_footer #buttonGo.ajax").live('click', function(event) {
265         event.preventDefault();
267         PMA_ajaxShowMessage(PMA_messages['strRemovingSelectedUsers']);
269         $form = $("#usersForm");
270         
271         $.post($form.attr('action'), $form.serialize() + "&delete=" + $(this).attr('value') + "&ajax_request=true", function(data) {
272             if(data.success == true) {
273                 PMA_ajaxShowMessage(data.message);
275                 //Remove the revoked user from the users list
276                 $form.find("input:checkbox:checked").parents("tr").slideUp("medium", function() {
277                     var this_user_initial = $(this).find('input:checkbox').val().charAt(0).toUpperCase();
278                     $(this).remove();
280                     //If this is the last user with this_user_initial, remove the link from #initials_table
281                     if($("#tableuserrights").find('input:checkbox[value^=' + this_user_initial + ']').length == 0) {
282                         $("#initials_table").find('td > a:contains(' + this_user_initial + ')').parent('td').html(this_user_initial);
283                     }
285                     //Re-check the classes of each row
286                     $form
287                     .find('tbody').find('tr:odd')
288                     .removeClass('even').addClass('odd')
289                     .end()
290                     .find('tr:even')
291                     .removeClass('odd').addClass('even');
292                 })
293             }
294             else {
295                 PMA_ajaxShowMessage(data.error);
296             }
297         }) // end $.post()
298     }) // end Revoke User
300     /**
301      * AJAX handler for 'Edit User'
302      *
303      * @see         PMA_ajaxShowMessage()
304      *
305      */
307     /**
308      * Step 1: Load Edit User Dialog
309      * @memberOf    jQuery
310      * @name        edit_user_click
311      * @see         $cfg['AjaxEnable'] 
312      */
313     $(".edit_user_anchor.ajax").live('click', function(event) {
314         /** @lends jQuery */
315         event.preventDefault();
317         PMA_ajaxShowMessage();
319         $(this).parents('tr').addClass('current_row');
321         /**
322          * @var button_options  Object containing options for jQueryUI dialog buttons
323          */
324         var button_options = {};
325         button_options[PMA_messages['strCancel']] = function() {$(this).dialog("close").remove();}
327         $.get($(this).attr('href'), {'ajax_request':true, 'edit_user_dialog': true}, function(data) {
328             $('<div id="edit_user_dialog"></div>')
329             .append(data)
330             .dialog({
331                 width: 900,
332                 height: 600,
333                 buttons: button_options
334             }); //dialog options end
335             displayPasswordGenerateButton();
336         }) // end $.get()
337     })
339     /**
340      * Step 2: Submit the Edit User Dialog
341      * 
342      * @see         PMA_ajaxShowMessage()
343      * @see         $cfg['AjaxEnable'] 
344      * @memberOf    jQuery
345      * @name        edit_user_submit
346      */
347     $("#edit_user_dialog").find("form").live('submit', function(event) {
348         /** @lends jQuery */
349         event.preventDefault();
351         PMA_ajaxShowMessage(PMA_messages['strProcessingRequest']);
353         $(this).append('<input type="hidden" name="ajax_request" value="true" />');
355         /**
356          * @var curr_submit_name    name of the current button being submitted
357          */
358         var curr_submit_name = $(this).find('.tblFooters').find('input:submit').attr('name');
360         /**
361          * @var curr_submit_value    value of the current button being submitted
362          */
363         var curr_submit_value = $(this).find('.tblFooters').find('input:submit').val();
365         $.post($(this).attr('action'), $(this).serialize() + '&' + curr_submit_name + '=' + curr_submit_value, function(data) {
366             if(data.success == true) {
368                 PMA_ajaxShowMessage(data.message);
369                 
370                 //Close the jQueryUI dialog
371                 $("#edit_user_dialog").dialog("close").remove();
373                 if(data.sql_query) {
374                     $("#topmenucontainer")
375                     .next('div')
376                     .remove()
377                     .end()
378                     .after(data.sql_query);
379                     var notice_class = $("#topmenucontainer").next("div").find('.notice');
380                     if($(notice_class).text() == '') {
381                         $(notice_class).remove();
382                     }
383                 } //Show SQL Query that was executed
385                 //Append new user if necessary
386                 if(data.new_user_string) {
387                     appendNewUser(data.new_user_string, data.new_user_initial, data.new_user_initial_string);
388                 }
390                 //Change privileges if they were edited
391                 if(data.new_privileges) {
392                     $("#usersForm")
393                     .find('.current_row')
394                     .find('tt')
395                     .html(data.new_privileges);
396                 }
398                 $("#usersForm")
399                 .find('.current_row')
400                 .removeClass('current_row');
401             }
402             else {
403                 PMA_ajaxShowMessage(data.error);
404             }
405         });
406     })
407     //end Edit user
409     /**
410      * AJAX handler for 'Export Privileges'
411      *
412      * @see         PMA_ajaxShowMessage()
413      * @see         $cfg['AjaxEnable'] 
414      * @memberOf    jQuery
415      * @name        export_user_click
416      */
417     $(".export_user_anchor.ajax").live('click', function(event) {
418         /** @lends jQuery */
419         event.preventDefault();
421         PMA_ajaxShowMessage();
423         /**
424          * @var button_options  Object containing options for jQueryUI dialog buttons
425          */
426         var button_options = {};
427         button_options[PMA_messages['strClose']] = function() {$(this).dialog("close").remove();}
429         $.get($(this).attr('href'), {'ajax_request': true}, function(data) {
430             $('<div id="export_dialog"></div>')
431             .prepend(data)
432             .dialog({
433                 width : 500,
434                 buttons: button_options
435             });
436         }) //end $.get
437     }) //end export privileges
439     /**
440      * AJAX handler to Paginate the Users Table
441      *
442      * @see         PMA_ajaxShowMessage()
443      * @see         $cfg['AjaxEnable'] 
444      * @name        paginate_users_table_click
445      * @memberOf    jQuery
446      */
447     $("#initials_table.ajax").find("a").live('click', function(event) {
448         event.preventDefault();
450         PMA_ajaxShowMessage();
452         $.get($(this).attr('href'), {'ajax_request' : true}, function(data) {
453             // This form is not on screen when first entering Privileges
454             // if there are more than 50 users
455             $("#usersForm").hide("medium").remove();
456             $("#fieldset_add_user").hide("medium").remove();
457             $("#initials_table")
458              .after(data).show("medium")
459              .siblings("h2").not(":first").remove();
460         }) // end $.get
461     })// end of the paginate users table
463     /*
464      * Additional confirmation dialog after clicking 
465      * 'Drop the databases...'
466      */
467     $('#checkbox_drop_users_db').click(function() {
468         $this_checkbox = $(this);
469         if ($this_checkbox.is(':checked')) {
470             var is_confirmed = confirm(PMA_messages['strDropDatabaseStrongWarning'] + '\n' + PMA_messages['strDoYouReally'] + ' :\nDROP DATABASE');
471             if (! is_confirmed) {
472                 $this_checkbox.attr('checked', false);
473             }
474         }
475     });
477 }, 'top.frame_content'); //end $(document).ready()
479 /**#@- */