application_settings_helper.rb 11 KB
Newer Older
1 2
# frozen_string_literal: true

3
module ApplicationSettingsHelper
4
  extend self
5

6 7 8 9 10
  delegate :allow_signup?,
           :gravatar_enabled?,
           :password_authentication_enabled_for_web?,
           :akismet_enabled?,
           to: :'Gitlab::CurrentSettings.current_application_settings'
11

12
  def user_oauth_applications?
13
    Gitlab::CurrentSettings.user_oauth_applications
14 15
  end

16
  def allowed_protocols_present?
17
    Gitlab::CurrentSettings.enabled_git_access_protocol.present?
18 19 20
  end

  def enabled_protocol
21
    case Gitlab::CurrentSettings.enabled_git_access_protocol
22
    when 'http'
23
      Gitlab.config.gitlab.protocol
24 25 26 27 28
    when 'ssh'
      'ssh'
    end
  end

29 30 31 32 33 34 35 36 37
  def all_protocols_enabled?
    Gitlab::CurrentSettings.enabled_git_access_protocol.blank?
  end

  def ssh_enabled?
    all_protocols_enabled? || enabled_protocol == 'ssh'
  end

  def http_enabled?
38
    all_protocols_enabled? || Gitlab::CurrentSettings.enabled_git_access_protocol == 'http'
39 40
  end

41
  def enabled_project_button(project, protocol)
42 43
    case protocol
    when 'ssh'
44
      ssh_clone_button(project, append_link: false)
45
    else
46
      http_clone_button(project, append_link: false)
47 48 49
    end
  end

50 51
  # Return a group of checkboxes that use Bootstrap's button plugin for a
  # toggle button effect.
52
  def restricted_level_checkboxes(help_block_id, checkbox_name, options = {})
53
    Gitlab::VisibilityLevel.values.map do |level|
54
      checked = restricted_visibility_levels(true).include?(level)
55
      css_class = checked ? 'active' : ''
56
      tag_name = "application_setting_visibility_level_#{level}"
57

58
      label_tag(tag_name, class: css_class) do
59 60
        check_box_tag(checkbox_name, level, checked,
                      autocomplete: 'off',
61
                      'aria-describedby' => help_block_id,
62
                      'class' => options[:class],
63
                      id: tag_name) + visibility_level_icon(level) + visibility_level_label(level)
64 65 66
      end
    end
  end
67 68 69

  # Return a group of checkboxes that use Bootstrap's button plugin for a
  # toggle button effect.
70
  def import_sources_checkboxes(help_block_id, options = {})
71
    Gitlab::ImportSources.options.map do |name, source|
72
      checked = @application_setting.import_sources.include?(source)
73
      css_class = checked ? 'active' : ''
74 75
      checkbox_name = 'application_setting[import_sources][]'

76
      label_tag(name, class: css_class) do
77 78
        check_box_tag(checkbox_name, source, checked,
                      autocomplete: 'off',
79
                      'aria-describedby' => help_block_id,
80
                      'class' => options[:class],
81
                      id: name.tr(' ', '_')) + name
82 83 84
      end
    end
  end
85

86
  def oauth_providers_checkboxes
87
    button_based_providers.map do |source|
88
      disabled = @application_setting.disabled_oauth_sign_in_sources.include?(source.to_s)
89 90
      css_class = ['btn']
      css_class << 'active' unless disabled
91
      checkbox_name = 'application_setting[enabled_oauth_sign_in_sources][]'
92
      name = Gitlab::Auth::OAuth::Provider.label_for(source)
93

94
      label_tag(checkbox_name, class: css_class.join(' ')) do
95
        check_box_tag(checkbox_name, source, !disabled,
96 97
                      autocomplete: 'off',
                      id: name.tr(' ', '_')) + name
98 99 100
      end
    end
  end
101

102 103 104
  def key_restriction_options_for_select(type)
    bit_size_options = Gitlab::SSHPublicKey.supported_sizes(type).map do |bits|
      ["Must be at least #{bits} bits", bits]
105
    end
106 107 108 109 110 111

    [
      ['Are allowed', 0],
      *bit_size_options,
      ['Are forbidden', ApplicationSetting::FORBIDDEN_KEY_VALUE]
    ]
112 113
  end

114
  def repository_storages_options_for_select(selected)
115
    options = Gitlab.config.repositories.storages.map do |name, storage|
116
      ["#{name} - #{storage['gitaly_address']}", name]
117 118
    end

119
    options_for_select(options, selected)
120
  end
121

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
  def external_authorization_description
    _("If enabled, access to projects will be validated on an external service"\
        " using their classification label.")
  end

  def external_authorization_timeout_help_text
    _("Time in seconds GitLab will wait for a response from the external "\
        "service. When the service does not respond in time, access will be "\
        "denied.")
  end

  def external_authorization_url_help_text
    _("When leaving the URL blank, classification labels can still be "\
        "specified without disabling cross project features or performing "\
        "external authorization checks.")
  end

  def external_authorization_client_certificate_help_text
    _("The X509 Certificate to use when mutual TLS is required to communicate "\
        "with the external authorization service. If left blank, the server "\
        "certificate is still validated when accessing over HTTPS.")
  end

  def external_authorization_client_key_help_text
    _("The private key to use when a client certificate is provided. This value "\
        "is encrypted at rest.")
  end

  def external_authorization_client_pass_help_text
    _("The passphrase required to decrypt the private key. This is optional "\
        "and the value is encrypted at rest.")
  end

155 156 157 158 159 160 161
  def visible_attributes
    [
      :admin_notification_email,
      :after_sign_out_path,
      :after_sign_up_text,
      :akismet_api_key,
      :akismet_enabled,
162
      :allow_local_requests_from_hooks_and_services,
163 164
      :allow_local_requests_from_web_hooks_and_services,
      :allow_local_requests_from_system_hooks,
165
      :dns_rebinding_protection_enabled,
166
      :archive_builds_in_human_readable,
167 168 169 170
      :asset_proxy_enabled,
      :asset_proxy_secret_key,
      :asset_proxy_url,
      :asset_proxy_whitelist,
171 172
      :static_objects_external_storage_auth_token,
      :static_objects_external_storage_url,
173
      :authorized_keys_enabled,
174
      :auto_devops_enabled,
175
      :auto_devops_domain,
176 177 178 179
      :container_registry_token_expire_delay,
      :default_artifacts_expire_in,
      :default_branch_protection,
      :default_group_visibility,
Gosia Ksionek's avatar
Gosia Ksionek committed
180
      :default_project_creation,
181 182 183 184
      :default_project_visibility,
      :default_projects_limit,
      :default_snippet_visibility,
      :disabled_oauth_sign_in_sources,
185
      :domain_blacklist,
186
      :domain_blacklist_enabled,
187
      # TODO Remove domain_blacklist_raw in APIv5 (See https://gitlab.com/gitlab-org/gitlab-foss/issues/67204)
188
      :domain_blacklist_raw,
189
      :domain_whitelist,
190
      # TODO Remove domain_whitelist_raw in APIv5 (See https://gitlab.com/gitlab-org/gitlab-foss/issues/67204)
191
      :domain_whitelist_raw,
192
      :outbound_local_requests_whitelist_raw,
193 194 195
      :dsa_key_restriction,
      :ecdsa_key_restriction,
      :ed25519_key_restriction,
196 197 198 199
      :eks_integration_enabled,
      :eks_account_id,
      :eks_access_key_id,
      :eks_secret_access_key,
200 201
      :email_author_in_body,
      :enabled_git_access_protocol,
202
      :enforce_terms,
203
      :first_day_of_week,
204 205 206
      :gitaly_timeout_default,
      :gitaly_timeout_medium,
      :gitaly_timeout_fast,
207 208
      :grafana_enabled,
      :grafana_url,
209
      :gravatar_enabled,
210
      :hashed_storage_enabled,
211 212 213
      :help_page_hide_commercial_content,
      :help_page_support_url,
      :help_page_text,
214
      :hide_third_party_offers,
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
      :home_page_url,
      :housekeeping_bitmaps_enabled,
      :housekeeping_enabled,
      :housekeeping_full_repack_period,
      :housekeeping_gc_period,
      :housekeeping_incremental_repack_period,
      :html_emails_enabled,
      :import_sources,
      :max_artifacts_size,
      :max_attachment_size,
      :max_pages_size,
      :metrics_enabled,
      :metrics_host,
      :metrics_method_call_threshold,
      :metrics_packet_size,
      :metrics_pool_size,
      :metrics_port,
      :metrics_sample_interval,
      :metrics_timeout,
234
      :mirror_available,
235
      :pages_domain_verification_enabled,
236
      :password_authentication_enabled_for_web,
237
      :password_authentication_enabled_for_git,
238
      :performance_bar_allowed_group_path,
239 240 241 242
      :performance_bar_enabled,
      :plantuml_enabled,
      :plantuml_url,
      :polling_interval_multiplier,
243
      :project_export_enabled,
244 245 246 247
      :prometheus_metrics_enabled,
      :recaptcha_enabled,
      :recaptcha_private_key,
      :recaptcha_site_key,
248
      :login_recaptcha_protection_enabled,
249
      :receive_max_input_size,
250 251 252 253
      :repository_checks_enabled,
      :repository_storages,
      :require_two_factor_authentication,
      :restricted_visibility_levels,
254
      :rsa_key_restriction,
255 256 257 258 259 260 261
      :send_user_confirmation_email,
      :session_expire_delay,
      :shared_runners_enabled,
      :shared_runners_text,
      :sign_in_text,
      :signup_enabled,
      :terminal_max_session_time,
262
      :terms,
Michael Kozono's avatar
Michael Kozono committed
263 264
      :throttle_authenticated_api_enabled,
      :throttle_authenticated_api_period_in_seconds,
265 266 267 268 269 270 271
      :throttle_authenticated_api_requests_per_period,
      :throttle_authenticated_web_enabled,
      :throttle_authenticated_web_period_in_seconds,
      :throttle_authenticated_web_requests_per_period,
      :throttle_unauthenticated_enabled,
      :throttle_unauthenticated_period_in_seconds,
      :throttle_unauthenticated_requests_per_period,
272 273 274 275
      :throttle_protected_paths_enabled,
      :throttle_protected_paths_period_in_seconds,
      :throttle_protected_paths_requests_per_period,
      :protected_paths_raw,
276
      :time_tracking_limit_to_hours,
277 278 279 280 281
      :two_factor_grace_period,
      :unique_ips_limit_enabled,
      :unique_ips_limit_per_user,
      :unique_ips_limit_time_window,
      :usage_ping_enabled,
282
      :instance_statistics_visibility_private,
283
      :user_default_external,
284
      :user_show_add_ssh_key_message,
285
      :user_default_internal_regex,
286
      :user_oauth_applications,
Phil Hughes's avatar
Phil Hughes committed
287
      :version_check_enabled,
288
      :web_ide_clientside_preview_enabled,
289
      :diff_max_patch_bytes,
290
      :commit_email_hostname,
Jan Provaznik's avatar
Jan Provaznik committed
291
      :protected_ci_variables,
292 293 294 295
      :local_markdown_version,
      :snowplow_collector_hostname,
      :snowplow_cookie_domain,
      :snowplow_enabled,
296
      :snowplow_site_id,
297
      :snowplow_iglu_registry_url,
298
      :push_event_hooks_limit,
299
      :push_event_activities_limit,
300 301 302
      :custom_http_clone_url_root,
      :pendo_enabled,
      :pendo_url
303 304
    ]
  end
305

306 307 308 309 310 311 312 313 314 315 316 317
  def external_authorization_service_attributes
    [
      :external_auth_client_cert,
      :external_auth_client_key,
      :external_auth_client_key_pass,
      :external_authorization_service_default_label,
      :external_authorization_service_enabled,
      :external_authorization_service_timeout,
      :external_authorization_service_url
    ]
  end

318 319 320
  def expanded_by_default?
    Rails.env.test?
  end
321

322 323 324 325
  def integration_expanded?(substring)
    @application_setting.errors.any? { |k| k.to_s.start_with?(substring) }
  end

326 327 328
  def instance_clusters_enabled?
    can?(current_user, :read_cluster, Clusters::Instance.new)
  end
329 330 331 332

  def omnibus_protected_paths_throttle?
    Rack::Attack.throttles.key?('protected paths')
  end
333
end
334 335 336 337 338 339

ApplicationSettingsHelper.prepend_if_ee('EE::ApplicationSettingsHelper') # rubocop: disable Cop/InjectEnterpriseEditionModule

# The methods in `EE::ApplicationSettingsHelper` should be available as both
# instance and class methods.
ApplicationSettingsHelper.extend_if_ee('EE::ApplicationSettingsHelper')