server_privileges.js 19 KB

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