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