Class: ApplicationSetting

Inherits:
ApplicationRecord show all
Includes:
ApplicationSettingImplementation, CacheMarkdownField, CacheableAttributes, ChronicDurationAttribute, IgnorableColumns, TokenAuthenticatable
Defined in:
app/models/application_setting.rb,
app/models/application_setting/term.rb,
app/policies/application_setting/term_policy.rb

Defined Under Namespace

Classes: Term, TermPolicy

Constant Summary collapse

GRAFANA_URL_ERROR_MESSAGE =
'Please check your Grafana URL setting in ' \
'Admin Area > Settings > Metrics and profiling > Metrics - Grafana'

Constants included from ApplicationSettingImplementation

ApplicationSettingImplementation::DEFAULT_MINIMUM_PASSWORD_LENGTH, ApplicationSettingImplementation::DEFAULT_PROTECTED_PATHS, ApplicationSettingImplementation::FORBIDDEN_KEY_VALUE, ApplicationSettingImplementation::STRING_LIST_SEPARATOR, ApplicationSettingImplementation::SUPPORTED_KEY_TYPES

Constants included from CacheMarkdownField

CacheMarkdownField::INVALIDATED_BY

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApplicationSettingImplementation

#add_to_outbound_local_requests_whitelist, #allow_signup?, #allowed_key_types, #archive_builds_older_than, #asset_proxy_whitelist=, #commit_email_hostname, #default_group_visibility=, #default_project_visibility=, #default_snippet_visibility=, #disabled_oauth_sign_in_sources=, #domain_blacklist_file=, #domain_blacklist_raw, #domain_blacklist_raw=, #domain_whitelist_raw, #domain_whitelist_raw=, #health_check_access_token, #help_page_support_url_column_exists?, #home_page_url_column_exists?, #key_restriction_for, #latest_terms, #normalized_repository_storage_weights, #outbound_local_requests_whitelist_arrays, #outbound_local_requests_whitelist_raw, #outbound_local_requests_whitelist_raw=, #password_authentication_enabled?, #performance_bar_allowed_group, #performance_bar_enabled, #pick_repository_storage, #protected_paths_raw, #protected_paths_raw=, #repository_storages, #repository_storages_weighted, #reset_memoized_terms, #restricted_visibility_levels=, #runners_registration_token, #static_objects_external_storage_enabled?, #usage_ping_can_be_configured?, #usage_ping_enabled, #user_default_internal_regex_enabled?, #user_default_internal_regex_instance, #web_ide_clientside_preview_bundler_url

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods included from ChronicDurationAttribute

#chronic_duration_attributes, #output_chronic_duration_attribute

Methods included from CacheMarkdownField

#attribute_invalidated?, #banzai_render_context, #cached_html_for, #cached_html_up_to_date?, #can_cache_field?, #invalidated_markdown_cache?, #latest_cached_markdown_version, #local_version, #parent_user, #refresh_markdown_cache, #refresh_markdown_cache!, #rendered_field_content, #skip_project_check?, #updated_cached_html_for

Methods included from CacheableAttributes

#cache!

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, without_order

Class Method Details

.cache_backendObject

By default, the backend is Rails.cache, which uses ActiveSupport::Cache::RedisStore. Since loading ApplicationSetting can cause a significant amount of load on Redis, let's cache it in memory.


459
460
461
# File 'app/models/application_setting.rb', line 459

def self.cache_backend
  Gitlab::ProcessMemoryCache.cache_backend
end

.check_schema!Object

Due to the frequency with which settings are accessed, it is likely that during a backup restore a running GitLab process will insert a new `application_settings` row before the constraints have been added to the table. This would add an extra row with ID 1 and prevent the primary key constraint from being added, which made ActiveRecord throw a IrreversibleOrderError anytime the settings were accessed (gitlab.com/gitlab-org/gitlab/-/issues/36405). To prevent this from happening, we do a sanity check that the primary key constraint is present before inserting a new entry.


449
450
451
452
453
# File 'app/models/application_setting.rb', line 449

def self.check_schema!
  return if ActiveRecord::Base.connection.primary_key(self.table_name).present?

  raise "The `#{self.table_name}` table is missing a primary key constraint in the database schema"
end

.create_from_defaultsObject


428
429
430
431
432
433
434
435
436
437
# File 'app/models/application_setting.rb', line 428

def self.create_from_defaults
  check_schema!

  transaction(requires_new: true) do
    super
  end
rescue ActiveRecord::RecordNotUnique
  # We already have an ApplicationSetting record, so just return it.
  current_without_cache
end

.repository_storages_weighted_attributesObject


27
28
29
# File 'app/models/application_setting.rb', line 27

def self.repository_storages_weighted_attributes
  @repository_storages_weighted_atributes ||= Gitlab.config.repositories.storages.keys.map { |k| "repository_storages_weighted_#{k}".to_sym }.freeze
end

Instance Method Details

#grafana_url_absolute?Boolean

Returns:

  • (Boolean)

420
421
422
# File 'app/models/application_setting.rb', line 420

def grafana_url_absolute?
  parsed_grafana_url&.absolute?
end

#recaptcha_or_login_protection_enabledObject


463
464
465
# File 'app/models/application_setting.rb', line 463

def 
  recaptcha_enabled || 
end

#sourcegraph_url_is_com?Boolean

Returns:

  • (Boolean)

424
425
426
# File 'app/models/application_setting.rb', line 424

def sourcegraph_url_is_com?
  !!(sourcegraph_url =~ /\Ahttps:\/\/(www\.)?sourcegraph\.com/)
end

#validate_grafana_urlObject


411
412
413
414
415
416
417
418
# File 'app/models/application_setting.rb', line 411

def validate_grafana_url
  unless parsed_grafana_url
    self.errors.add(
      :grafana_url,
      "must be a valid relative or absolute URL. #{GRAFANA_URL_ERROR_MESSAGE}"
    )
  end
end