privileges.js 15 KB

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