Read about our upcoming Code of Conduct on this issue

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
      :container_registry_token_expire_delay,
      :default_artifacts_expire_in,
      :default_branch_protection,
179
      :default_ci_config_path,
180
      :default_group_visibility,
Gosia Ksionek's avatar
Gosia Ksionek committed
181
      :default_project_creation,
182
183
184
185
      :default_project_visibility,
      :default_projects_limit,
      :default_snippet_visibility,
      :disabled_oauth_sign_in_sources,
186
      :domain_blacklist,
187
      :domain_blacklist_enabled,
188
      # TODO Remove domain_blacklist_raw in APIv5 (See https://gitlab.com/gitlab-org/gitlab-foss/issues/67204)
189
      :domain_blacklist_raw,
190
      :domain_whitelist,
191
      # TODO Remove domain_whitelist_raw in APIv5 (See https://gitlab.com/gitlab-org/gitlab-foss/issues/67204)
192
      :domain_whitelist_raw,
193
      :outbound_local_requests_whitelist_raw,
194
195
196
      :dsa_key_restriction,
      :ecdsa_key_restriction,
      :ed25519_key_restriction,
197
198
199
200
      :eks_integration_enabled,
      :eks_account_id,
      :eks_access_key_id,
      :eks_secret_access_key,
201
202
      :email_author_in_body,
      :enabled_git_access_protocol,
203
      :enforce_terms,
204
      :first_day_of_week,
205
206
207
      :gitaly_timeout_default,
      :gitaly_timeout_medium,
      :gitaly_timeout_fast,
208
209
      :grafana_enabled,
      :grafana_url,
210
      :gravatar_enabled,
211
      :hashed_storage_enabled,
212
213
214
      :help_page_hide_commercial_content,
      :help_page_support_url,
      :help_page_text,
215
      :hide_third_party_offers,
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
      :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,
235
      :mirror_available,
236
      :pages_domain_verification_enabled,
237
      :password_authentication_enabled_for_web,
238
      :password_authentication_enabled_for_git,
239
      :performance_bar_allowed_group_path,
240
241
242
243
      :performance_bar_enabled,
      :plantuml_enabled,
      :plantuml_url,
      :polling_interval_multiplier,
244
      :project_export_enabled,
245
246
247
248
      :prometheus_metrics_enabled,
      :recaptcha_enabled,
      :recaptcha_private_key,
      :recaptcha_site_key,
249
      :login_recaptcha_protection_enabled,
250
      :receive_max_input_size,
251
252
253
254
      :repository_checks_enabled,
      :repository_storages,
      :require_two_factor_authentication,
      :restricted_visibility_levels,
255
      :rsa_key_restriction,
256
257
258
259
260
261
262
      :send_user_confirmation_email,
      :session_expire_delay,
      :shared_runners_enabled,
      :shared_runners_text,
      :sign_in_text,
      :signup_enabled,
      :terminal_max_session_time,
263
      :terms,
Michael Kozono's avatar
Michael Kozono committed
264
265
      :throttle_authenticated_api_enabled,
      :throttle_authenticated_api_period_in_seconds,
266
267
268
269
270
271
272
      :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,
273
274
275
276
      :throttle_protected_paths_enabled,
      :throttle_protected_paths_period_in_seconds,
      :throttle_protected_paths_requests_per_period,
      :protected_paths_raw,
277
      :time_tracking_limit_to_hours,
278
279
280
281
282
      :two_factor_grace_period,
      :unique_ips_limit_enabled,
      :unique_ips_limit_per_user,
      :unique_ips_limit_time_window,
      :usage_ping_enabled,
283
      :instance_statistics_visibility_private,
284
      :user_default_external,
285
      :user_show_add_ssh_key_message,
286
      :user_default_internal_regex,
287
      :user_oauth_applications,
Phil Hughes's avatar
Phil Hughes committed
288
      :version_check_enabled,
289
      :web_ide_clientside_preview_enabled,
290
      :diff_max_patch_bytes,
291
      :commit_email_hostname,
Jan Provaznik's avatar
Jan Provaznik committed
292
      :protected_ci_variables,
293
294
295
296
      :local_markdown_version,
      :snowplow_collector_hostname,
      :snowplow_cookie_domain,
      :snowplow_enabled,
297
      :snowplow_app_id,
298
      :snowplow_iglu_registry_url,
299
      :push_event_hooks_limit,
300
      :push_event_activities_limit,
301
      :custom_http_clone_url_root
302
303
    ]
  end
304

305
306
307
308
309
310
311
312
313
314
315
316
  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

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

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

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

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

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')