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 10KB

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