Translation update done using Pootle.
[phpmyadmin/dkf.git] / js / server_privileges.js
blob4ecfea00f875f4c3d7d559a6f6917ea1243cb073
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  * @version $Id$
11  */
13 /**
14  * Validates the password field in a form
15  *
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
20  */
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) {
26         return true;
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')) {
30         return true;
31     }
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'];
41     }
43     if (alert_msg) {
44         alert(alert_msg);
45         password.value  = '';
46         password_repeat.value = '';
47         password.focus();
48         return false;
49     }
51     return true;
52 } // end of the 'checkPassword()' function
55 /**
56  * Validates the "add a user" form
57  *
58  * @return  boolean  whether the form is validated or not
59  */
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();
65         return false;
66     }
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();
71         return false;
72     }
74     return checkPassword(the_form);
75 } // end of the 'checkAddUser()' function
78 /**
79  * Generate a new password and copy it to the password input areas
80  *
81  * @param   object   the form that holds the password fields
82  *
83  * @return  boolean  always true
84  */
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;
92     passwd.value = '';
94     for ( i = 0; i < passwordlength; i++ ) {
95         passwd.value += pwchars.charAt( Math.floor( Math.random() * pwchars.length ) )
96     }
97     passwd_form.text_pma_pw.value = passwd.value;
98     passwd_form.text_pma_pw2.value = passwd.value;
99     return true;
103  * When a new user is created and retrieved over Ajax, append the user's row to
104  * the user's table
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
109  */
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
121     $(new_user_string)
122     .insertAfter($(curr_last_row))
123     .find('input:checkbox')
124     .attr('id', new_last_row_id)
125     .val(function() {
126         //the insert messes up the &amp;27; part. let's fix it
127         return $(this).val().replace(/&/,'&amp;');
128     })
129     .end()
130     .find('label')
131     .attr('for', new_last_row_id)
132     .end();
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);
141 /**#@+
142  * @namespace   jQuery
143  */
146  * AJAX scripts for server_privileges page.
148  * Actions ajaxified here:
149  * Add a new user
150  * Revoke a user
151  * Edit privileges
152  * Export privileges
153  * Paginate table of users
154  * Flush privileges
156  * @memberOf    jQuery
157  * @name        document.ready
158  */
160 $(document).ready(function() {
161     /** @lends jQuery */
163     /**
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
167      */
168     $.ajaxSetup({
169         cache: 'false'
170     });
172     /**
173      * AJAX event handler for 'Add a New User'
174      *
175      * @see         PMA_ajaxShowMessage()
176      * @see         appendNewUser()
177      * @memberOf    jQuery
178      * @name        add_user_click
179      *
180      */
181     $("#fieldset_add_user a").live("click", function(event) {
182         /** @lends jQuery */
183         event.preventDefault();
185         PMA_ajaxShowMessage();
187         /**
188          * @var button_options  Object containing options for jQueryUI dialog buttons
189          */
190         var button_options = {};
191         button_options[PMA_messages['strCreateUser']] = function() {
193                                                         /**
194                                                          * @var the_form    stores reference to current form
195                                                          */
196                                                         var the_form = $(this).find("#addUsersForm");
198                                                         if( ! checkAddUser($(the_form).get(0)) ) {
199                                                             PMA_ajaxShowMessage(PMA_messages['strFormEmpty']);
200                                                             return false;
201                                                         }
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")
209                                                                 .next('div')
210                                                                 .remove()
211                                                                 .end()
212                                                                 .after(data.sql_query);
213                                                                 
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();
218                                                                 }
220                                                                 appendNewUser(data.new_user_string, data.new_user_initial, data.new_user_initial_string);
221                                                             }
222                                                             else {
223                                                                 PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : "+data.error, "7000");
224                                                             }
225                                                         })
226                                                     };
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>')
231             .prepend(data)
232             .find("#fieldset_add_user_footer").hide() //showing the "Go" and "Create User" buttons together will confuse the user
233             .end()
234             .find("#addUsersForm").append('<input type="hidden" name="ajax_request" value="true" />')
235             .end()
236             .dialog({
237                 title: top.frame_content.PMA_messages['strAddNewUser'],
238                 width: 800,
239                 modal: true,
240                 buttons: button_options
241             }); //dialog options end
242         }); // end $.get()
244     });//end of Add New User AJAX event handler
247     /**
248      * Ajax event handler for 'Reload Privileges' anchor
249      *
250      * @see         PMA_ajaxShowMessage()
251      * @memberOf    jQuery
252      * @name        reload_privileges_click
253      */
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);
262             }
263             else {
264                 PMA_ajaxShowMessage(data.error);
265             }
266         }); //end $.get()
268     }); //end of Reload Privileges Ajax event handler
270     /**
271      * AJAX handler for 'Revoke User'
272      *
273      * @see         PMA_ajaxShowMessage()
274      * @memberOf    jQuery
275      * @name        revoke_user_click
276      */
277     $("#fieldset_delete_user_footer #buttonGo").live('click', function(event) {
278         event.preventDefault();
280         PMA_ajaxShowMessage(PMA_messages['strRemovingSelectedUsers']);
281         
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();
289                     $(this).remove();
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);
294                     }
296                     //Re-check the classes of each row
297                     $("#usersForm")
298                     .find('tbody').find('tr:odd')
299                     .removeClass('even').addClass('odd')
300                     .end()
301                     .find('tr:even')
302                     .removeClass('odd').addClass('even');
303                 })
304             }
305             else {
306                 PMA_ajaxShowMessage(data.error);
307             }
308         }) // end $.post()
309     }) // end Revoke User
311     /**
312      * AJAX handler for 'Edit User'
313      *
314      * @see         PMA_ajaxShowMessage()
315      *
316      */
318     /**
319      * Step 1: Load Edit User Dialog
320      * @memberOf    jQuery
321      * @name        edit_user_click
322      */
323     $(".edit_user_anchor").live('click', function(event) {
324         /** @lends jQuery */
325         event.preventDefault();
327         PMA_ajaxShowMessage();
329         $(this).parents('tr').addClass('current_row');
331         /**
332          * @var button_options  Object containing options for jQueryUI dialog buttons
333          */
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>')
339             .append(data)
340             .dialog({
341                 width: 900,
342                 buttons: button_options
343             })
344         }) // end $.get()
345     })
347     /**
348      * Step 2: Submit the Edit User Dialog
349      * 
350      * @see         PMA_ajaxShowMessage()
351      * @memberOf    jQuery
352      * @name        edit_user_submit
353      */
354     $("#edit_user_dialog").find("form").live('submit', function(event) {
355         /** @lends jQuery */
356         event.preventDefault();
358         PMA_ajaxShowMessage(PMA_messages['strProcessingRequest']);
360         $(this).append('<input type="hidden" name="ajax_request" value="true" />');
362         /**
363          * @var curr_submit_name    name of the current button being submitted
364          */
365         var curr_submit_name = $(this).find('.tblFooters').find('input:submit').attr('name');
367         /**
368          * @var curr_submit_value    value of the current button being submitted
369          */
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);
376                 
377                 //Close the jQueryUI dialog
378                 $("#edit_user_dialog").dialog("close").remove();
380                 if(data.sql_query) {
381                     $("#topmenucontainer")
382                     .next('div')
383                     .remove()
384                     .end()
385                     .after(data.sql_query);
386                     var notice_class = $("#topmenucontainer").next("div").find('.notice');
387                     if($(notice_class).text() == '') {
388                         $(notice_class).remove();
389                     }
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);
395                 }
397                 //Change privileges if they were edited
398                 if(data.new_privileges) {
399                     $("#usersForm")
400                     .find('.current_row')
401                     .find('tt')
402                     .html(data.new_privileges);
403                 }
405                 $("#usersForm")
406                 .find('.current_row')
407                 .removeClass('current_row');
408             }
409             else {
410                 PMA_ajaxShowMessage(data.error);
411             }
412         });
413     })
414     //end Edit user
416     /**
417      * AJAX handler for 'Export Privileges'
418      *
419      * @see         PMA_ajaxShowMessage()
420      * @memberOf    jQuery
421      * @name        export_user_click
422      */
423     $(".export_user_anchor").live('click', function(event) {
424         /** @lends jQuery */
425         event.preventDefault();
427         PMA_ajaxShowMessage();
429         /**
430          * @var button_options  Object containing options for jQueryUI dialog buttons
431          */
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>')
437             .prepend(data)
438             .dialog({
439                 width : 500,
440                 buttons: button_options
441             });
442         }) //end $.get
443     }) //end export privileges
445     /**
446      * AJAX handler to Paginate the Users Table
447      *
448      * @see         PMA_ajaxShowMessage()
449      * @name        paginate_users_table_click
450      * @memberOf    jQuery
451      */
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) {
458             $("#usersForm")
459             .hide("medium")
460             .siblings("#initials_table")
461             .after(data)
462             .show("medium")
463             .end()
464             .remove();
465             $("#initials_table").siblings("h2").not(":first").remove();
466         }) // end $.get
467     })// end of the paginate users table
469 }, 'top.frame_content'); //end $(document).ready()
471 /**#@- */