debian-mirror-gitlab/lib/gitlab/current_settings.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

101 lines
3.4 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2015-04-26 12:48:37 +05:30
module Gitlab
module CurrentSettings
2018-03-17 18:26:18 +05:30
class << self
2021-03-11 19:13:27 +05:30
def signup_disabled?
!signup_enabled?
end
2021-06-02 17:11:27 +05:30
def signup_limited?
2021-12-07 22:27:20 +05:30
domain_allowlist.present? || email_restrictions_enabled? || require_admin_approval_after_user_signup? || user_default_external?
2021-06-02 17:11:27 +05:30
end
2018-03-17 18:26:18 +05:30
def current_application_settings
2018-12-05 23:21:45 +05:30
Gitlab::SafeRequestStore.fetch(:current_application_settings) { ensure_application_settings! }
end
2016-04-02 18:10:28 +05:30
2021-03-11 19:13:27 +05:30
def current_application_settings?
Gitlab::SafeRequestStore.exist?(:current_application_settings) || ::ApplicationSetting.current.present?
end
2019-10-12 21:52:04 +05:30
def expire_current_application_settings
::ApplicationSetting.expire
Gitlab::SafeRequestStore.delete(:current_application_settings)
end
2018-11-20 20:47:30 +05:30
def clear_in_memory_application_settings!
@in_memory_application_settings = nil
end
2021-01-29 00:20:46 +05:30
def method_missing(name, *args, **kwargs, &block)
current_application_settings.send(name, *args, **kwargs, &block) # rubocop:disable GitlabSecurity/PublicSend
2018-03-17 18:26:18 +05:30
end
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
def respond_to_missing?(name, include_private = false)
current_application_settings.respond_to?(name, include_private) || super
end
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
private
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
def ensure_application_settings!
cached_application_settings || uncached_application_settings
2017-09-10 17:25:29 +05:30
end
2016-04-02 18:10:28 +05:30
2018-03-17 18:26:18 +05:30
def cached_application_settings
2018-11-08 19:23:39 +05:30
return in_memory_application_settings if ENV['IN_MEMORY_APPLICATION_SETTINGS'] == 'true'
2018-03-17 18:26:18 +05:30
begin
::ApplicationSetting.cached
2021-06-08 01:23:25 +05:30
rescue StandardError
2018-11-08 19:23:39 +05:30
# In case Redis isn't running
# or the Redis UNIX socket file is not available
# or the DB is not running (we use migrations in the cache key)
2018-03-17 18:26:18 +05:30
end
end
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
def uncached_application_settings
2020-04-22 19:07:51 +05:30
return fake_application_settings if Gitlab::Runtime.rake? && !connect_to_db?
2017-09-10 17:25:29 +05:30
2018-11-08 19:23:39 +05:30
current_settings = ::ApplicationSetting.current
2018-03-17 18:26:18 +05:30
# If there are pending migrations, it's possible there are columns that
# need to be added to the application settings. To prevent Rake tasks
# and other callers from failing, use any loaded settings and return
# defaults for missing columns.
2022-04-04 11:22:00 +05:30
if Gitlab::Runtime.rake? && ::ApplicationSetting.connection.migration_context.needs_migration?
2019-02-15 15:39:39 +05:30
db_attributes = current_settings&.attributes || {}
2019-07-07 11:18:12 +05:30
fake_application_settings(db_attributes)
2019-02-15 15:39:39 +05:30
elsif current_settings.present?
current_settings
else
::ApplicationSetting.create_from_defaults
2018-11-08 19:23:39 +05:30
end
2022-05-07 20:08:51 +05:30
rescue ::ApplicationSetting::Recursion
in_memory_application_settings
2018-03-17 18:26:18 +05:30
end
2015-04-26 12:48:37 +05:30
2019-02-15 15:39:39 +05:30
def fake_application_settings(attributes = {})
Gitlab::FakeApplicationSettings.new(::ApplicationSetting.defaults.merge(attributes || {}))
2018-11-08 19:23:39 +05:30
end
2019-02-15 15:39:39 +05:30
def in_memory_application_settings
@in_memory_application_settings ||= ::ApplicationSetting.build_from_defaults
2018-03-17 18:26:18 +05:30
end
def connect_to_db?
# When the DBMS is not available, an exception (e.g. PG::ConnectionBad) is raised
2022-08-27 11:52:29 +05:30
active_db_connection = begin
::ApplicationSetting.connection.active?
rescue StandardError
false
end
2016-04-02 18:10:28 +05:30
2018-03-17 18:26:18 +05:30
active_db_connection &&
2021-12-11 22:18:48 +05:30
ApplicationSetting.database.cached_table_exists?
2018-03-17 18:26:18 +05:30
rescue ActiveRecord::NoDatabaseError
false
end
2015-09-25 12:07:36 +05:30
end
2015-04-26 12:48:37 +05:30
end
end