current_settings.rb 2.4 KB
Newer Older
1 2 3
module Gitlab
  module CurrentSettings
    def current_application_settings
4 5 6 7 8 9
      if RequestStore.active?
        RequestStore.fetch(:current_application_settings) { ensure_application_settings! }
      else
        ensure_application_settings!
      end
    end
10

11
    def ensure_application_settings!
12
      settings = ::ApplicationSetting.cached
13

14
      if !settings && connect_to_db?
15 16
        settings = ::ApplicationSetting.current
        settings ||= ::ApplicationSetting.create_from_defaults unless ActiveRecord::Migrator.needs_migration?
17
      end
18 19

      settings || fake_application_settings
20
    end
21 22 23 24

    def fake_application_settings
      OpenStruct.new(
        default_projects_limit: Settings.gitlab['default_projects_limit'],
M
Marco Wessel 已提交
25
        default_branch_protection: Settings.gitlab['default_branch_protection'],
26 27 28
        signup_enabled: Settings.gitlab['signup_enabled'],
        signin_enabled: Settings.gitlab['signin_enabled'],
        gravatar_enabled: Settings.gravatar['enabled'],
29 30 31 32
        sign_in_text: nil,
        after_sign_up_text: nil,
        help_page_text: nil,
        shared_runners_text: nil,
33
        restricted_visibility_levels: Settings.gitlab['restricted_visibility_levels'],
34
        max_attachment_size: Settings.gitlab['max_attachment_size'],
35
        session_expire_delay: Settings.gitlab['session_expire_delay'],
36 37 38 39
        default_project_visibility: Settings.gitlab.default_projects_features['visibility_level'],
        default_snippet_visibility: Settings.gitlab.default_projects_features['visibility_level'],
        restricted_signup_domains: Settings.gitlab['restricted_signup_domains'],
        import_sources: ['github','bitbucket','gitlab','gitorious','google_code','fogbugz','git'],
40
        shared_runners_enabled: Settings.gitlab_ci['shared_runners_enabled'],
K
Kamil Trzcinski 已提交
41
        max_artifacts_size: Settings.artifacts['max_size'],
42
        require_two_factor_authentication: false,
43
        two_factor_grace_period: 48,
44 45
        akismet_enabled: false,
        repository_checks_enabled: true,
46
        container_registry_token_expire_delay: 5,
47 48
      )
    end
49 50 51 52

    private

    def connect_to_db?
53 54 55
      # When the DBMS is not available, an exception (e.g. PG::ConnectionBad) is raised
      active_db_connection = ActiveRecord::Base.connection.active? rescue false

56
      ENV['USE_DB'] != 'false' &&
57
      active_db_connection &&
58
      ActiveRecord::Base.connection.table_exists?('application_settings')
59 60 61

    rescue ActiveRecord::NoDatabaseError
      false
62
    end
63 64
  end
end