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.

common.js 8.0KB

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