2022-01-29 00:00:11 +03:00
|
|
|
import $ from 'jquery';
|
2023-02-09 19:14:45 +03:00
|
|
|
import {checkAppUrl} from '../common-global.js';
|
2023-02-19 07:06:14 +03:00
|
|
|
import {hideElem, showElem, toggleElem} from '../../utils/dom.js';
|
2022-01-29 00:00:11 +03:00
|
|
|
|
2021-10-21 10:37:43 +03:00
|
|
|
const {csrfToken} = window.config;
|
2021-10-16 20:28:04 +03:00
|
|
|
|
|
|
|
export function initAdminCommon() {
|
2023-02-09 19:14:45 +03:00
|
|
|
if ($('.page-content.admin').length === 0) {
|
2021-10-16 20:28:04 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-09 19:14:45 +03:00
|
|
|
// check whether appUrl(ROOT_URL) is correct, if not, show an error message
|
|
|
|
checkAppUrl();
|
|
|
|
|
2021-10-16 20:28:04 +03:00
|
|
|
// New user
|
|
|
|
if ($('.admin.new.user').length > 0 || $('.admin.edit.user').length > 0) {
|
|
|
|
$('#login_type').on('change', function () {
|
|
|
|
if ($(this).val().substring(0, 1) === '0') {
|
|
|
|
$('#user_name').removeAttr('disabled');
|
|
|
|
$('#login_name').removeAttr('required');
|
2023-02-19 07:06:14 +03:00
|
|
|
hideElem($('.non-local'));
|
|
|
|
showElem($('.local'));
|
2023-04-20 12:28:27 +03:00
|
|
|
$('#user_name').trigger('focus');
|
2021-10-16 20:28:04 +03:00
|
|
|
|
|
|
|
if ($(this).data('password') === 'required') {
|
|
|
|
$('#password').attr('required', 'required');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($('.admin.edit.user').length > 0) {
|
|
|
|
$('#user_name').attr('disabled', 'disabled');
|
|
|
|
}
|
|
|
|
$('#login_name').attr('required', 'required');
|
2023-02-19 07:06:14 +03:00
|
|
|
showElem($('.non-local'));
|
|
|
|
hideElem($('.local'));
|
2023-04-20 12:28:27 +03:00
|
|
|
$('#login_name').trigger('focus');
|
2021-10-16 20:28:04 +03:00
|
|
|
|
|
|
|
$('#password').removeAttr('required');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function onSecurityProtocolChange() {
|
|
|
|
if ($('#security_protocol').val() > 0) {
|
2023-02-19 07:06:14 +03:00
|
|
|
showElem($('.has-tls'));
|
2021-10-16 20:28:04 +03:00
|
|
|
} else {
|
2023-02-19 07:06:14 +03:00
|
|
|
hideElem($('.has-tls'));
|
2021-10-16 20:28:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onUsePagedSearchChange() {
|
|
|
|
if ($('#use_paged_search').prop('checked')) {
|
2023-02-22 18:26:02 +03:00
|
|
|
showElem('.search-page-size');
|
|
|
|
$('.search-page-size').find('input').attr('required', 'required');
|
2021-10-16 20:28:04 +03:00
|
|
|
} else {
|
2023-02-22 18:26:02 +03:00
|
|
|
hideElem('.search-page-size');
|
|
|
|
$('.search-page-size').find('input').removeAttr('required');
|
2021-10-16 20:28:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onOAuth2Change(applyDefaultValues) {
|
2023-02-19 07:06:14 +03:00
|
|
|
hideElem($('.open_id_connect_auto_discovery_url, .oauth2_use_custom_url'));
|
2021-10-16 20:28:04 +03:00
|
|
|
$('.open_id_connect_auto_discovery_url input[required]').removeAttr('required');
|
|
|
|
|
|
|
|
const provider = $('#oauth2_provider').val();
|
|
|
|
switch (provider) {
|
|
|
|
case 'openidConnect':
|
|
|
|
$('.open_id_connect_auto_discovery_url input').attr('required', 'required');
|
2023-02-19 07:06:14 +03:00
|
|
|
showElem($('.open_id_connect_auto_discovery_url'));
|
2021-10-16 20:28:04 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if ($(`#${provider}_customURLSettings`).data('required')) {
|
|
|
|
$('#oauth2_use_custom_url').attr('checked', 'checked');
|
|
|
|
}
|
|
|
|
if ($(`#${provider}_customURLSettings`).data('available')) {
|
2023-02-19 07:06:14 +03:00
|
|
|
showElem($('.oauth2_use_custom_url'));
|
2021-10-16 20:28:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
onOAuth2UseCustomURLChange(applyDefaultValues);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onOAuth2UseCustomURLChange(applyDefaultValues) {
|
|
|
|
const provider = $('#oauth2_provider').val();
|
2023-02-19 07:06:14 +03:00
|
|
|
hideElem($('.oauth2_use_custom_url_field'));
|
2021-10-16 20:28:04 +03:00
|
|
|
$('.oauth2_use_custom_url_field input[required]').removeAttr('required');
|
|
|
|
|
|
|
|
if ($('#oauth2_use_custom_url').is(':checked')) {
|
|
|
|
for (const custom of ['token_url', 'auth_url', 'profile_url', 'email_url', 'tenant']) {
|
|
|
|
if (applyDefaultValues) {
|
|
|
|
$(`#oauth2_${custom}`).val($(`#${provider}_${custom}`).val());
|
|
|
|
}
|
|
|
|
if ($(`#${provider}_${custom}`).data('available')) {
|
|
|
|
$(`.oauth2_${custom} input`).attr('required', 'required');
|
2023-02-19 07:06:14 +03:00
|
|
|
showElem($(`.oauth2_${custom}`));
|
2021-10-16 20:28:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 17:24:58 +03:00
|
|
|
function onEnableLdapGroupsChange() {
|
2023-02-19 07:06:14 +03:00
|
|
|
toggleElem($('#ldap-group-options'), $('.js-ldap-group-toggle').is(':checked'));
|
2021-10-16 20:28:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// New authentication
|
|
|
|
if ($('.admin.new.authentication').length > 0) {
|
|
|
|
$('#auth_type').on('change', function () {
|
2023-02-19 07:06:14 +03:00
|
|
|
hideElem($('.ldap, .dldap, .smtp, .pam, .oauth2, .has-tls, .search-page-size, .sspi'));
|
2021-10-16 20:28:04 +03:00
|
|
|
|
|
|
|
$('.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');
|
|
|
|
$('.binddnrequired').removeClass('required');
|
|
|
|
|
|
|
|
const authType = $(this).val();
|
|
|
|
switch (authType) {
|
|
|
|
case '2': // LDAP
|
2023-02-19 07:06:14 +03:00
|
|
|
showElem($('.ldap'));
|
2021-10-16 20:28:04 +03:00
|
|
|
$('.binddnrequired input, .ldap div.required:not(.dldap) input').attr('required', 'required');
|
|
|
|
$('.binddnrequired').addClass('required');
|
|
|
|
break;
|
|
|
|
case '3': // SMTP
|
2023-02-19 07:06:14 +03:00
|
|
|
showElem($('.smtp'));
|
|
|
|
showElem($('.has-tls'));
|
2021-10-16 20:28:04 +03:00
|
|
|
$('.smtp div.required input, .has-tls').attr('required', 'required');
|
|
|
|
break;
|
|
|
|
case '4': // PAM
|
2023-02-19 07:06:14 +03:00
|
|
|
showElem($('.pam'));
|
2021-10-16 20:28:04 +03:00
|
|
|
$('.pam input').attr('required', 'required');
|
|
|
|
break;
|
|
|
|
case '5': // LDAP
|
2023-02-19 07:06:14 +03:00
|
|
|
showElem($('.dldap'));
|
2021-10-16 20:28:04 +03:00
|
|
|
$('.dldap div.required:not(.ldap) input').attr('required', 'required');
|
|
|
|
break;
|
|
|
|
case '6': // OAuth2
|
2023-02-19 07:06:14 +03:00
|
|
|
showElem($('.oauth2'));
|
2021-10-16 20:28:04 +03:00
|
|
|
$('.oauth2 div.required:not(.oauth2_use_custom_url,.oauth2_use_custom_url_field,.open_id_connect_auto_discovery_url) input').attr('required', 'required');
|
|
|
|
onOAuth2Change(true);
|
|
|
|
break;
|
|
|
|
case '7': // SSPI
|
2023-02-19 07:06:14 +03:00
|
|
|
showElem($('.sspi'));
|
2021-10-16 20:28:04 +03:00
|
|
|
$('.sspi div.required input').attr('required', 'required');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (authType === '2' || authType === '5') {
|
|
|
|
onSecurityProtocolChange();
|
2022-02-11 17:24:58 +03:00
|
|
|
onEnableLdapGroupsChange();
|
2021-10-16 20:28:04 +03:00
|
|
|
}
|
|
|
|
if (authType === '2') {
|
|
|
|
onUsePagedSearchChange();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$('#auth_type').trigger('change');
|
|
|
|
$('#security_protocol').on('change', onSecurityProtocolChange);
|
|
|
|
$('#use_paged_search').on('change', onUsePagedSearchChange);
|
|
|
|
$('#oauth2_provider').on('change', () => onOAuth2Change(true));
|
|
|
|
$('#oauth2_use_custom_url').on('change', () => onOAuth2UseCustomURLChange(true));
|
2022-02-11 17:24:58 +03:00
|
|
|
$('.js-ldap-group-toggle').on('change', onEnableLdapGroupsChange);
|
2021-10-16 20:28:04 +03:00
|
|
|
}
|
|
|
|
// Edit authentication
|
|
|
|
if ($('.admin.edit.authentication').length > 0) {
|
|
|
|
const authType = $('#auth_type').val();
|
|
|
|
if (authType === '2' || authType === '5') {
|
|
|
|
$('#security_protocol').on('change', onSecurityProtocolChange);
|
2022-02-11 17:24:58 +03:00
|
|
|
$('.js-ldap-group-toggle').on('change', onEnableLdapGroupsChange);
|
|
|
|
onEnableLdapGroupsChange();
|
2021-10-16 20:28:04 +03:00
|
|
|
if (authType === '2') {
|
|
|
|
$('#use_paged_search').on('change', onUsePagedSearchChange);
|
|
|
|
}
|
|
|
|
} else if (authType === '6') {
|
|
|
|
$('#oauth2_provider').on('change', () => onOAuth2Change(true));
|
|
|
|
$('#oauth2_use_custom_url').on('change', () => onOAuth2UseCustomURLChange(false));
|
|
|
|
onOAuth2Change(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-15 05:48:36 +03:00
|
|
|
if ($('.admin.authentication').length > 0) {
|
|
|
|
$('#auth_name').on('input', function () {
|
|
|
|
$('#oauth2-callback-url').text(`${window.location.origin}/user/oauth2/${encodeURIComponent($(this).val())}/callback`);
|
|
|
|
}).trigger('input');
|
|
|
|
}
|
|
|
|
|
2021-10-16 20:28:04 +03:00
|
|
|
// Notice
|
|
|
|
if ($('.admin.notice')) {
|
|
|
|
const $detailModal = $('#detail-modal');
|
|
|
|
|
|
|
|
// Attach view detail modals
|
|
|
|
$('.view-detail').on('click', function () {
|
|
|
|
$detailModal.find('.content pre').text($(this).parents('tr').find('.notice-description').text());
|
2023-04-11 02:01:20 +03:00
|
|
|
$detailModal.find('.sub.header').text($(this).parents('tr').find('relative-time').attr('title'));
|
2021-10-16 20:28:04 +03:00
|
|
|
$detailModal.modal('show');
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Select actions
|
|
|
|
const $checkboxes = $('.select.table .ui.checkbox');
|
|
|
|
$('.select.action').on('click', function () {
|
|
|
|
switch ($(this).data('action')) {
|
|
|
|
case 'select-all':
|
|
|
|
$checkboxes.checkbox('check');
|
|
|
|
break;
|
|
|
|
case 'deselect-all':
|
|
|
|
$checkboxes.checkbox('uncheck');
|
|
|
|
break;
|
|
|
|
case 'inverse':
|
|
|
|
$checkboxes.checkbox('toggle');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
2023-03-14 06:34:09 +03:00
|
|
|
$('#delete-selection').on('click', function (e) {
|
|
|
|
e.preventDefault();
|
2021-10-16 20:28:04 +03:00
|
|
|
const $this = $(this);
|
|
|
|
$this.addClass('loading disabled');
|
|
|
|
const ids = [];
|
|
|
|
$checkboxes.each(function () {
|
|
|
|
if ($(this).checkbox('is checked')) {
|
|
|
|
ids.push($(this).data('id'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$.post($this.data('link'), {
|
2021-10-21 10:37:43 +03:00
|
|
|
_csrf: csrfToken,
|
2021-10-16 20:28:04 +03:00
|
|
|
ids
|
|
|
|
}).done(() => {
|
|
|
|
window.location.href = $this.data('redirect');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|