You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

admin-common.js 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. const {csrfToken} = window.config;
  2. export function initAdminCommon() {
  3. if ($('.admin').length === 0) {
  4. return;
  5. }
  6. // New user
  7. if ($('.admin.new.user').length > 0 || $('.admin.edit.user').length > 0) {
  8. $('#login_type').on('change', function () {
  9. if ($(this).val().substring(0, 1) === '0') {
  10. $('#user_name').removeAttr('disabled');
  11. $('#login_name').removeAttr('required');
  12. $('.non-local').hide();
  13. $('.local').show();
  14. $('#user_name').focus();
  15. if ($(this).data('password') === 'required') {
  16. $('#password').attr('required', 'required');
  17. }
  18. } else {
  19. if ($('.admin.edit.user').length > 0) {
  20. $('#user_name').attr('disabled', 'disabled');
  21. }
  22. $('#login_name').attr('required', 'required');
  23. $('.non-local').show();
  24. $('.local').hide();
  25. $('#login_name').focus();
  26. $('#password').removeAttr('required');
  27. }
  28. });
  29. }
  30. function onSecurityProtocolChange() {
  31. if ($('#security_protocol').val() > 0) {
  32. $('.has-tls').show();
  33. } else {
  34. $('.has-tls').hide();
  35. }
  36. }
  37. function onUsePagedSearchChange() {
  38. if ($('#use_paged_search').prop('checked')) {
  39. $('.search-page-size').show()
  40. .find('input').attr('required', 'required');
  41. } else {
  42. $('.search-page-size').hide()
  43. .find('input').removeAttr('required');
  44. }
  45. }
  46. function onOAuth2Change(applyDefaultValues) {
  47. $('.open_id_connect_auto_discovery_url, .oauth2_use_custom_url').hide();
  48. $('.open_id_connect_auto_discovery_url input[required]').removeAttr('required');
  49. const provider = $('#oauth2_provider').val();
  50. switch (provider) {
  51. case 'openidConnect':
  52. $('.open_id_connect_auto_discovery_url input').attr('required', 'required');
  53. $('.open_id_connect_auto_discovery_url').show();
  54. break;
  55. default:
  56. if ($(`#${provider}_customURLSettings`).data('required')) {
  57. $('#oauth2_use_custom_url').attr('checked', 'checked');
  58. }
  59. if ($(`#${provider}_customURLSettings`).data('available')) {
  60. $('.oauth2_use_custom_url').show();
  61. }
  62. }
  63. onOAuth2UseCustomURLChange(applyDefaultValues);
  64. }
  65. function onOAuth2UseCustomURLChange(applyDefaultValues) {
  66. const provider = $('#oauth2_provider').val();
  67. $('.oauth2_use_custom_url_field').hide();
  68. $('.oauth2_use_custom_url_field input[required]').removeAttr('required');
  69. if ($('#oauth2_use_custom_url').is(':checked')) {
  70. for (const custom of ['token_url', 'auth_url', 'profile_url', 'email_url', 'tenant']) {
  71. if (applyDefaultValues) {
  72. $(`#oauth2_${custom}`).val($(`#${provider}_${custom}`).val());
  73. }
  74. if ($(`#${provider}_${custom}`).data('available')) {
  75. $(`.oauth2_${custom} input`).attr('required', 'required');
  76. $(`.oauth2_${custom}`).show();
  77. }
  78. }
  79. }
  80. }
  81. function onVerifyGroupMembershipChange() {
  82. if ($('#groups_enabled').is(':checked')) {
  83. $('#groups_enabled_change').show();
  84. } else {
  85. $('#groups_enabled_change').hide();
  86. }
  87. }
  88. // New authentication
  89. if ($('.admin.new.authentication').length > 0) {
  90. $('#auth_type').on('change', function () {
  91. $('.ldap, .dldap, .smtp, .pam, .oauth2, .has-tls, .search-page-size, .sspi').hide();
  92. $('.ldap input[required], .binddnrequired input[required], .dldap input[required], .smtp input[required], .pam input[required], .oauth2 input[required], .has-tls input[required], .sspi input[required]').removeAttr('required');
  93. $('.binddnrequired').removeClass('required');
  94. const authType = $(this).val();
  95. switch (authType) {
  96. case '2': // LDAP
  97. $('.ldap').show();
  98. $('.binddnrequired input, .ldap div.required:not(.dldap) input').attr('required', 'required');
  99. $('.binddnrequired').addClass('required');
  100. break;
  101. case '3': // SMTP
  102. $('.smtp').show();
  103. $('.has-tls').show();
  104. $('.smtp div.required input, .has-tls').attr('required', 'required');
  105. break;
  106. case '4': // PAM
  107. $('.pam').show();
  108. $('.pam input').attr('required', 'required');
  109. break;
  110. case '5': // LDAP
  111. $('.dldap').show();
  112. $('.dldap div.required:not(.ldap) input').attr('required', 'required');
  113. break;
  114. case '6': // OAuth2
  115. $('.oauth2').show();
  116. $('.oauth2 div.required:not(.oauth2_use_custom_url,.oauth2_use_custom_url_field,.open_id_connect_auto_discovery_url) input').attr('required', 'required');
  117. onOAuth2Change(true);
  118. break;
  119. case '7': // SSPI
  120. $('.sspi').show();
  121. $('.sspi div.required input').attr('required', 'required');
  122. break;
  123. }
  124. if (authType === '2' || authType === '5') {
  125. onSecurityProtocolChange();
  126. onVerifyGroupMembershipChange();
  127. }
  128. if (authType === '2') {
  129. onUsePagedSearchChange();
  130. }
  131. });
  132. $('#auth_type').trigger('change');
  133. $('#security_protocol').on('change', onSecurityProtocolChange);
  134. $('#use_paged_search').on('change', onUsePagedSearchChange);
  135. $('#oauth2_provider').on('change', () => onOAuth2Change(true));
  136. $('#oauth2_use_custom_url').on('change', () => onOAuth2UseCustomURLChange(true));
  137. $('#groups_enabled').on('change', onVerifyGroupMembershipChange);
  138. }
  139. // Edit authentication
  140. if ($('.admin.edit.authentication').length > 0) {
  141. const authType = $('#auth_type').val();
  142. if (authType === '2' || authType === '5') {
  143. $('#security_protocol').on('change', onSecurityProtocolChange);
  144. $('#groups_enabled').on('change', onVerifyGroupMembershipChange);
  145. onVerifyGroupMembershipChange();
  146. if (authType === '2') {
  147. $('#use_paged_search').on('change', onUsePagedSearchChange);
  148. }
  149. } else if (authType === '6') {
  150. $('#oauth2_provider').on('change', () => onOAuth2Change(true));
  151. $('#oauth2_use_custom_url').on('change', () => onOAuth2UseCustomURLChange(false));
  152. onOAuth2Change(false);
  153. }
  154. }
  155. // Notice
  156. if ($('.admin.notice')) {
  157. const $detailModal = $('#detail-modal');
  158. // Attach view detail modals
  159. $('.view-detail').on('click', function () {
  160. $detailModal.find('.content pre').text($(this).parents('tr').find('.notice-description').text());
  161. $detailModal.find('.sub.header').text($(this).parents('tr').find('.notice-created-time').text());
  162. $detailModal.modal('show');
  163. return false;
  164. });
  165. // Select actions
  166. const $checkboxes = $('.select.table .ui.checkbox');
  167. $('.select.action').on('click', function () {
  168. switch ($(this).data('action')) {
  169. case 'select-all':
  170. $checkboxes.checkbox('check');
  171. break;
  172. case 'deselect-all':
  173. $checkboxes.checkbox('uncheck');
  174. break;
  175. case 'inverse':
  176. $checkboxes.checkbox('toggle');
  177. break;
  178. }
  179. });
  180. $('#delete-selection').on('click', function () {
  181. const $this = $(this);
  182. $this.addClass('loading disabled');
  183. const ids = [];
  184. $checkboxes.each(function () {
  185. if ($(this).checkbox('is checked')) {
  186. ids.push($(this).data('id'));
  187. }
  188. });
  189. $.post($this.data('link'), {
  190. _csrf: csrfToken,
  191. ids
  192. }).done(() => {
  193. window.location.href = $this.data('redirect');
  194. });
  195. });
  196. }
  197. }