Translated using Weblate (Slovenian)
[phpmyadmin.git] / js / server / privileges.js
blobf3888b5a8049543b21dc3f6999e4f14b70269951
1 /**
2  * @fileoverview    functions used in server privilege pages
3  * @name            Server Privileges
4  *
5  * @requires    jQuery
6  * @requires    jQueryUI
7  * @requires    js/functions.js
8  *
9  */
11 /**
12  * Validates the "add a user" form
13  *
14  * @return boolean  whether the form is validated or not
15  */
16 function checkAddUser (theForm) {
17     if (theForm.elements.pred_hostname.value === 'userdefined' && theForm.elements.hostname.value === '') {
18         alert(Messages.strHostEmpty);
19         theForm.elements.hostname.focus();
20         return false;
21     }
23     if (theForm.elements.pred_username.value === 'userdefined' && theForm.elements.username.value === '') {
24         alert(Messages.strUserEmpty);
25         theForm.elements.username.focus();
26         return false;
27     }
29     return Functions.checkPassword($(theForm));
32 /**
33  * AJAX scripts for /server/privileges page.
34  *
35  * Actions ajaxified here:
36  * Add user
37  * Revoke a user
38  * Edit privileges
39  * Export privileges
40  * Paginate table of users
41  * Flush privileges
42  *
43  * @memberOf    jQuery
44  * @name        document.ready
45  */
48 /**
49  * Unbind all event handlers before tearing down a page
50  */
51 AJAX.registerTeardown('server/privileges.js', function () {
52     $('#fieldset_add_user_login').off('change', 'input[name=\'username\']');
53     $(document).off('click', '#deleteUserCard .btn.ajax');
54     $(document).off('click', 'a.edit_user_group_anchor.ajax');
55     $(document).off('click', 'button.mult_submit[value=export]');
56     $(document).off('click', 'a.export_user_anchor.ajax');
57     $(document).off('click',  '#initials_table a.ajax');
58     $('#dropUsersDbCheckbox').off('click');
59     $(document).off('click', '.checkall_box');
60     $(document).off('change', '#checkbox_SSL_priv');
61     $(document).off('change', 'input[name="ssl_type"]');
62     $(document).off('change', '#select_authentication_plugin');
63 });
65 AJAX.registerOnload('server/privileges.js', function () {
66     /**
67      * Display a warning if there is already a user by the name entered as the username.
68      */
69     $('#fieldset_add_user_login').on('change', 'input[name=\'username\']', function () {
70         var username = $(this).val();
71         var $warning = $('#user_exists_warning');
72         if ($('#select_pred_username').val() === 'userdefined' && username !== '') {
73             var href = $('form[name=\'usersForm\']').attr('action');
74             var params = {
75                 'ajax_request' : true,
76                 'server' : CommonParams.get('server'),
77                 'validate_username' : true,
78                 'username' : username
79             };
80             $.get(href, params, function (data) {
81                 if (data.user_exists) {
82                     $warning.show();
83                 } else {
84                     $warning.hide();
85                 }
86             });
87         } else {
88             $warning.hide();
89         }
90     });
92     /**
93      * Indicating password strength
94      */
95     $('#text_pma_pw').on('keyup', function () {
96         var meterObj = $('#password_strength_meter');
97         var meterObjLabel = $('#password_strength');
98         var username = $('input[name="username"]');
99         username = username.val();
100         Functions.checkPasswordStrength($(this).val(), meterObj, meterObjLabel, username);
101     });
103     /**
104      * Automatically switching to 'Use Text field' from 'No password' once start writing in text area
105      */
106     $('#text_pma_pw').on('input', function () {
107         if ($('#text_pma_pw').val() !== '') {
108             $('#select_pred_password').val('userdefined');
109         }
110     });
112     $('#text_pma_change_pw').on('keyup', function () {
113         var meterObj = $('#change_password_strength_meter');
114         var meterObjLabel = $('#change_password_strength');
115         Functions.checkPasswordStrength($(this).val(), meterObj, meterObjLabel, CommonParams.get('user'));
116     });
118     /**
119      * Display a notice if sha256_password is selected
120      */
121     $(document).on('change', '#select_authentication_plugin', function () {
122         var selectedPlugin = $(this).val();
123         if (selectedPlugin === 'sha256_password') {
124             $('#ssl_reqd_warning').show();
125         } else {
126             $('#ssl_reqd_warning').hide();
127         }
128     });
130     /**
131      * AJAX handler for 'Revoke User'
132      *
133      * @see         Functions.ajaxShowMessage()
134      * @memberOf    jQuery
135      * @name        revoke_user_click
136      */
137     $(document).on('click', '#deleteUserCard .btn.ajax', function (event) {
138         event.preventDefault();
140         var $thisButton = $(this);
141         var $form = $('#usersForm');
143         $thisButton.confirm(Messages.strDropUserWarning, $form.attr('action'), function (url) {
144             var $dropUsersDbCheckbox = $('#dropUsersDbCheckbox');
145             if ($dropUsersDbCheckbox.is(':checked')) {
146                 var isConfirmed = confirm(Messages.strDropDatabaseStrongWarning + '\n' + Functions.sprintf(Messages.strDoYouReally, 'DROP DATABASE'));
147                 if (! isConfirmed) {
148                     // Uncheck the drop users database checkbox
149                     $dropUsersDbCheckbox.prop('checked', false);
150                 }
151             }
153             Functions.ajaxShowMessage(Messages.strRemovingSelectedUsers);
155             var argsep = CommonParams.get('arg_separator');
156             $.post(url, $form.serialize() + argsep + 'delete=' + $thisButton.val() + argsep + 'ajax_request=true', function (data) {
157                 if (typeof data !== 'undefined' && data.success === true) {
158                     Functions.ajaxShowMessage(data.message);
159                     // Refresh navigation, if we droppped some databases with the name
160                     // that is the same as the username of the deleted user
161                     if ($('#dropUsersDbCheckbox:checked').length) {
162                         Navigation.reload();
163                     }
164                     // Remove the revoked user from the users list
165                     $form.find('input:checkbox:checked').parents('tr').slideUp('medium', function () {
166                         var thisUserInitial = $(this).find('input:checkbox').val().charAt(0).toUpperCase();
167                         $(this).remove();
169                         // If this is the last user with this_user_initial, remove the link from #initials_table
170                         if ($('#tableuserrights').find('input:checkbox[value^="' + thisUserInitial + '"], input:checkbox[value^="' + thisUserInitial.toLowerCase() + '"]').length === 0) {
171                             $('#initials_table').find('td > a:contains(' + thisUserInitial + ')').parent('td').html(thisUserInitial);
172                         }
174                         // Re-check the classes of each row
175                         $form
176                             .find('tbody').find('tr').each(function (index) {
177                                 if (index >= 0 && index % 2 === 0) {
178                                     $(this).removeClass('odd').addClass('even');
179                                 } else if (index >= 0 && index % 2 !== 0) {
180                                     $(this).removeClass('even').addClass('odd');
181                                 }
182                             });
183                         // update the checkall checkbox
184                         $(Functions.checkboxesSel).trigger('change');
185                     });
186                 } else {
187                     Functions.ajaxShowMessage(data.error, false);
188                 }
189             }); // end $.post()
190         });
191     }); // end Revoke User
193     $(document).on('click', 'a.edit_user_group_anchor.ajax', function (event) {
194         event.preventDefault();
195         $(this).parents('tr').addClass('current_row');
196         var $msg = Functions.ajaxShowMessage();
197         $.get(
198             $(this).attr('href'),
199             {
200                 'ajax_request': true,
201                 'edit_user_group_dialog': true
202             },
203             function (data) {
204                 if (typeof data !== 'undefined' && data.success === true) {
205                     Functions.ajaxRemoveMessage($msg);
206                     var buttonOptions = {};
207                     buttonOptions[Messages.strGo] = function () {
208                         var usrGroup = $('#changeUserGroupDialog')
209                             .find('select[name="userGroup"]')
210                             .val();
211                         var $message = Functions.ajaxShowMessage();
212                         var argsep = CommonParams.get('arg_separator');
213                         $.post(
214                             'index.php?route=/server/privileges',
215                             $('#changeUserGroupDialog').find('form').serialize() + argsep + 'ajax_request=1',
216                             function (data) {
217                                 Functions.ajaxRemoveMessage($message);
218                                 if (typeof data !== 'undefined' && data.success === true) {
219                                     $('#usersForm')
220                                         .find('.current_row')
221                                         .removeClass('current_row')
222                                         .find('.usrGroup')
223                                         .text(usrGroup);
224                                 } else {
225                                     Functions.ajaxShowMessage(data.error, false);
226                                     $('#usersForm')
227                                         .find('.current_row')
228                                         .removeClass('current_row');
229                                 }
230                             }
231                         );
232                         $(this).dialog('close');
233                     };
234                     buttonOptions[Messages.strClose] = function () {
235                         $(this).dialog('close');
236                     };
237                     var $dialog = $('<div></div>')
238                         .attr('id', 'changeUserGroupDialog')
239                         .append(data.message)
240                         .dialog({
241                             width: 500,
242                             minWidth: 300,
243                             modal: true,
244                             buttons: buttonOptions,
245                             title: $('legend', $(data.message)).text(),
246                             close: function () {
247                                 $(this).remove();
248                             }
249                         });
250                     $dialog.find('legend').remove();
251                 } else {
252                     Functions.ajaxShowMessage(data.error, false);
253                     $('#usersForm')
254                         .find('.current_row')
255                         .removeClass('current_row');
256                 }
257             }
258         );
259     });
261     /**
262      * AJAX handler for 'Export Privileges'
263      *
264      * @see         Functions.ajaxShowMessage()
265      * @memberOf    jQuery
266      * @name        export_user_click
267      */
268     $(document).on('click', 'button.mult_submit[value=export]', function (event) {
269         event.preventDefault();
270         // can't export if no users checked
271         if ($(this.form).find('input:checked').length === 0) {
272             Functions.ajaxShowMessage(Messages.strNoAccountSelected, 2000, 'success');
273             return;
274         }
275         var $msgbox = Functions.ajaxShowMessage();
276         var buttonOptions = {};
277         buttonOptions[Messages.strClose] = function () {
278             $(this).dialog('close');
279         };
280         var argsep = CommonParams.get('arg_separator');
281         var serverId = CommonParams.get('server');
282         var selectedUsers = $('#usersForm input[name*=\'selected_usr\']:checkbox').serialize();
283         var postStr = selectedUsers + '&submit_mult=export' + argsep + 'ajax_request=true&server=' + serverId;
284         $.post(
285             $(this.form).prop('action'),
286             postStr,
287             function (data) {
288                 if (typeof data !== 'undefined' && data.success === true) {
289                     var $ajaxDialog = $('<div></div>')
290                         .append(data.message)
291                         .dialog({
292                             title: data.title,
293                             width: 500,
294                             buttons: buttonOptions,
295                             close: function () {
296                                 $(this).remove();
297                             }
298                         });
299                     Functions.ajaxRemoveMessage($msgbox);
300                     // Attach syntax highlighted editor to export dialog
301                     Functions.getSqlEditor($ajaxDialog.find('textarea'));
302                 } else {
303                     Functions.ajaxShowMessage(data.error, false);
304                 }
305             }
306         ); // end $.post
307     });
308     // if exporting non-ajax, highlight anyways
309     Functions.getSqlEditor($('textarea.export'));
311     $(document).on('click', 'a.export_user_anchor.ajax', function (event) {
312         event.preventDefault();
313         var $msgbox = Functions.ajaxShowMessage();
314         /**
315          * @var button_options  Object containing options for jQueryUI dialog buttons
316          */
317         var buttonOptions = {};
318         buttonOptions[Messages.strClose] = function () {
319             $(this).dialog('close');
320         };
321         $.get($(this).attr('href'), { 'ajax_request': true }, function (data) {
322             if (typeof data !== 'undefined' && data.success === true) {
323                 var $ajaxDialog = $('<div></div>')
324                     .append(data.message)
325                     .dialog({
326                         title: data.title,
327                         width: 500,
328                         buttons: buttonOptions,
329                         close: function () {
330                             $(this).remove();
331                         }
332                     });
333                 Functions.ajaxRemoveMessage($msgbox);
334                 // Attach syntax highlighted editor to export dialog
335                 Functions.getSqlEditor($ajaxDialog.find('textarea'));
336             } else {
337                 Functions.ajaxShowMessage(data.error, false);
338             }
339         }); // end $.get
340     }); // end export privileges
342     /**
343      * AJAX handler to Paginate the Users Table
344      *
345      * @see         Functions.ajaxShowMessage()
346      * @name        paginate_users_table_click
347      * @memberOf    jQuery
348      */
349     $(document).on('click', '#initials_table a.ajax', function (event) {
350         event.preventDefault();
351         var $msgbox = Functions.ajaxShowMessage();
352         $.get($(this).attr('href'), { 'ajax_request' : true }, function (data) {
353             if (typeof data !== 'undefined' && data.success === true) {
354                 Functions.ajaxRemoveMessage($msgbox);
355                 // This form is not on screen when first entering Privileges
356                 // if there are more than 50 users
357                 $('.alert-primary').remove();
358                 $('#usersForm').hide('medium').remove();
359                 $('#fieldset_add_user').hide('medium').remove();
360                 $('#initials_table')
361                     .prop('id', 'initials_table_old')
362                     .after(data.message).show('medium')
363                     .siblings('h2').not($('#initials_table')
364                         .prop('id', 'initials_table_old')
365                         .after(data.message).show('medium')
366                         .siblings('h2').first())
367                     .remove();
368                 // prevent double initials table
369                 $('#initials_table_old').remove();
370             } else {
371                 Functions.ajaxShowMessage(data.error, false);
372             }
373         }); // end $.get
374     }); // end of the paginate users table
376     $(document).on('change', 'input[name="ssl_type"]', function () {
377         var $div = $('#specified_div');
378         if ($('#ssl_type_SPECIFIED').is(':checked')) {
379             $div.find('input').prop('disabled', false);
380         } else {
381             $div.find('input').prop('disabled', true);
382         }
383     });
385     $(document).on('change', '#checkbox_SSL_priv', function () {
386         var $div = $('#require_ssl_div');
387         if ($(this).is(':checked')) {
388             $div.find('input').prop('disabled', false);
389             $('#ssl_type_SPECIFIED').trigger('change');
390         } else {
391             $div.find('input').prop('disabled', true);
392         }
393     });
395     $('#checkbox_SSL_priv').trigger('change');
397     /*
398      * Create submenu for simpler interface
399      */
400     var addOrUpdateSubmenu = function () {
401         var $subNav = $('.nav-pills');
402         var $editUserDialog = $('#edit_user_dialog');
403         var submenuLabel;
404         var submenuLink;
405         var linkNumber;
407         // if submenu exists yet, remove it first
408         if ($subNav.length > 0) {
409             $subNav.remove();
410         }
412         // construct a submenu from the existing fieldsets
413         $subNav = $('<ul></ul>').prop('class', 'nav nav-pills m-2');
415         $('#edit_user_dialog .submenu-item').each(function () {
416             submenuLabel = $(this).find('legend[data-submenu-label]').data('submenu-label');
418             submenuLink = $('<a></a>')
419                 .prop('class', 'nav-link')
420                 .prop('href', '#')
421                 .html(submenuLabel);
423             $('<li></li>')
424                 .prop('class', 'nav-item')
425                 .append(submenuLink)
426                 .appendTo($subNav);
427         });
429         // click handlers for submenu
430         $subNav.find('a').on('click', function (e) {
431             e.preventDefault();
432             // if already active, ignore click
433             if ($(this).hasClass('active')) {
434                 return;
435             }
436             $subNav.find('a').removeClass('active');
437             $(this).addClass('active');
439             // which section to show now?
440             linkNumber = $subNav.find('a').index($(this));
441             // hide all sections but the one to show
442             $('#edit_user_dialog .submenu-item').hide().eq(linkNumber).show();
443         });
445         // make first menu item active
446         // TODO: support URL hash history
447         $subNav.find('> :first-child a').addClass('active');
448         $editUserDialog.prepend($subNav);
450         // hide all sections but the first
451         $('#edit_user_dialog .submenu-item').hide().eq(0).show();
453         // scroll to the top
454         $('html, body').animate({ scrollTop: 0 }, 'fast');
455     };
457     $('input.autofocus').trigger('focus');
458     $(Functions.checkboxesSel).trigger('change');
459     Functions.displayPasswordGenerateButton();
460     if ($('#edit_user_dialog').length > 0) {
461         addOrUpdateSubmenu();
462     }
464     var windowWidth = $(window).width();
465     $('.jsresponsive').css('max-width', (windowWidth - 35) + 'px');
467     $('#addUsersForm').on('submit', function () {
468         return checkAddUser(this);
469     });
471     $('#copyUserForm').on('submit', function () {
472         return checkAddUser(this);
473     });