current_settings.rb 2.2 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'],
M
Marco Wessel 已提交
21
        default_branch_protection: Settings.gitlab['default_branch_protection'],
22 23
        signup_enabled: Settings.gitlab['signup_enabled'],
        signin_enabled: Settings.gitlab['signin_enabled'],
24
        twitter_sharing_enabled: Settings.gitlab['twitter_sharing_enabled'],
25 26
        gravatar_enabled: Settings.gravatar['enabled'],
        sign_in_text: Settings.extra['sign_in_text'],
27
        restricted_visibility_levels: Settings.gitlab['restricted_visibility_levels'],
28
        max_attachment_size: Settings.gitlab['max_attachment_size'],
29
        session_expire_delay: Settings.gitlab['session_expire_delay'],
30 31 32 33
        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'],
34
        shared_runners_enabled: Settings.gitlab_ci['shared_runners_enabled'],
K
Kamil Trzcinski 已提交
35
        max_artifacts_size: Settings.artifacts['max_size'],
36 37
        require_two_factor_authentication: false,
        two_factor_grace_period: 48
38 39
      )
    end
40 41 42 43

    private

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

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

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