code.gitea.io/gitea@v1.21.7/web_src/js/features/admin/common.js (about)

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