current_settings.rb 2.17 KB
Newer Older
1 2 3
module Gitlab
  module CurrentSettings
    def current_application_settings
4 5 6
      key = :current_application_settings

      RequestStore.store[key] ||= begin
7 8
        settings = nil

9
        if connect_to_db?
10 11
          settings = ::ApplicationSetting.current
          settings ||= ::ApplicationSetting.create_from_defaults unless ActiveRecord::Migrator.needs_migration?
12
        end
13 14

        settings || fake_application_settings
15
      end
16
    end
17 18 19 20

    def fake_application_settings
      OpenStruct.new(
        default_projects_limit: Settings.gitlab['default_projects_limit'],
Marco Wessel's avatar
Marco Wessel committed
21
        default_branch_protection: Settings.gitlab['default_branch_protection'],
22 23 24 25
        signup_enabled: Settings.gitlab['signup_enabled'],
        signin_enabled: Settings.gitlab['signin_enabled'],
        gravatar_enabled: Settings.gravatar['enabled'],
        sign_in_text: Settings.extra['sign_in_text'],
26
        restricted_visibility_levels: Settings.gitlab['restricted_visibility_levels'],
27
        max_attachment_size: Settings.gitlab['max_attachment_size'],
28
        session_expire_delay: Settings.gitlab['session_expire_delay'],
29 30 31 32
        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'],
33
        shared_runners_enabled: Settings.gitlab_ci['shared_runners_enabled'],
Kamil Trzcinski's avatar
Kamil Trzcinski committed
34
        max_artifacts_size: Settings.artifacts['max_size'],
35
        require_two_factor_authentication: false,
36
        two_factor_grace_period: 48,
37 38
        akismet_enabled: false,
        repository_checks_enabled: true,
39 40
      )
    end
41 42 43 44

    private

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

48
      ENV['USE_DB'] != 'false' &&
49
      active_db_connection &&
50
      ActiveRecord::Base.connection.table_exists?('application_settings')
51 52 53

    rescue ActiveRecord::NoDatabaseError
      false
54
    end
55 56
  end
end