application_setting.rb 5.4 KB
Newer Older
1
class ApplicationSetting < ActiveRecord::Base
2 3
  include TokenAuthenticatable
  add_authentication_token_field :runners_registration_token
4
  add_authentication_token_field :health_check_access_token
5

6 7
  CACHE_KEY = 'application_setting.last'

8
  serialize :restricted_visibility_levels
9
  serialize :import_sources
10
  serialize :disabled_oauth_sign_in_sources, Array
11 12
  serialize :restricted_signup_domains, Array
  attr_accessor :restricted_signup_domains_raw
13 14

  validates :session_expire_delay,
15 16
            presence: true,
            numericality: { only_integer: true, greater_than_or_equal_to: 0 }
17

18
  validates :home_page_url,
19 20 21
            allow_blank: true,
            url: true,
            if: :home_page_url_column_exist
22

23
  validates :after_sign_out_path,
24 25
            allow_blank: true,
            url: true
26

D
Douwe Maan 已提交
27
  validates :admin_notification_email,
28
            email: true,
29
            allow_blank: true
D
Douwe Maan 已提交
30

31
  validates :two_factor_grace_period,
32 33 34 35 36 37 38 39 40
            numericality: { greater_than_or_equal_to: 0 }

  validates :recaptcha_site_key,
            presence: true,
            if: :recaptcha_enabled

  validates :recaptcha_private_key,
            presence: true,
            if: :recaptcha_enabled
41

J
Jeroen Nijhof 已提交
42 43 44 45
  validates :sentry_dsn,
            presence: true,
            if: :sentry_enabled

46 47 48 49
  validates :akismet_api_key,
            presence: true,
            if: :akismet_enabled

50 51 52 53
  validates :max_attachment_size,
            presence: true,
            numericality: { only_integer: true, greater_than: 0 }

54 55 56 57
  validates :container_registry_token_expire_delay,
            presence: true,
            numericality: { only_integer: true, greater_than: 0 }

58 59 60 61
  validates :repository_storage,
    presence: true,
    inclusion: { in: ->(_object) { Gitlab.config.repositories.storages.keys } }

62
  validates_each :restricted_visibility_levels do |record, attr, value|
V
Vinnie Okada 已提交
63 64 65 66 67
    unless value.nil?
      value.each do |level|
        unless Gitlab::VisibilityLevel.options.has_value?(level)
          record.errors.add(attr, "'#{level}' is not a valid visibility level")
        end
68 69 70 71
      end
    end
  end

72 73 74 75 76 77 78 79 80 81
  validates_each :import_sources do |record, attr, value|
    unless value.nil?
      value.each do |source|
        unless Gitlab::ImportSources.options.has_value?(source)
          record.errors.add(attr, "'#{source}' is not a import source")
        end
      end
    end
  end

82 83 84 85
  validates_each :disabled_oauth_sign_in_sources do |record, attr, value|
    unless value.nil?
      value.each do |source|
        unless Devise.omniauth_providers.include?(source.to_sym)
A
typo  
Andrei Gliga 已提交
86
          record.errors.add(attr, "'#{source}' is not an OAuth sign-in source")
87 88 89 90 91
        end
      end
    end
  end

92
  before_save :ensure_runners_registration_token
93
  before_save :ensure_health_check_access_token
94

95
  after_commit do
96
    Rails.cache.write(CACHE_KEY, self)
97 98
  end

99
  def self.current
100
    Rails.cache.fetch(CACHE_KEY) do
101 102
      ApplicationSetting.last
    end
103
  end
104

105
  def self.expire
106
    Rails.cache.delete(CACHE_KEY)
107 108
  end

109 110 111 112
  def self.cached
    Rails.cache.fetch(CACHE_KEY)
  end

113 114 115
  def self.create_from_defaults
    create(
      default_projects_limit: Settings.gitlab['default_projects_limit'],
116
      default_branch_protection: Settings.gitlab['default_branch_protection'],
117 118 119
      signup_enabled: Settings.gitlab['signup_enabled'],
      signin_enabled: Settings.gitlab['signin_enabled'],
      gravatar_enabled: Settings.gravatar['enabled'],
120 121 122 123
      sign_in_text: nil,
      after_sign_up_text: nil,
      help_page_text: nil,
      shared_runners_text: nil,
124
      restricted_visibility_levels: Settings.gitlab['restricted_visibility_levels'],
V
Vinnie Okada 已提交
125
      max_attachment_size: Settings.gitlab['max_attachment_size'],
126
      session_expire_delay: Settings.gitlab['session_expire_delay'],
V
Vinnie Okada 已提交
127
      default_project_visibility: Settings.gitlab.default_projects_features['visibility_level'],
128
      default_snippet_visibility: Settings.gitlab.default_projects_features['visibility_level'],
129
      restricted_signup_domains: Settings.gitlab['restricted_signup_domains'],
J
James Lopez 已提交
130
      import_sources: %w[github bitbucket gitlab gitorious google_code fogbugz git gitlab_project],
131
      shared_runners_enabled: Settings.gitlab_ci['shared_runners_enabled'],
K
Kamil Trzcinski 已提交
132
      max_artifacts_size: Settings.artifacts['max_size'],
133
      require_two_factor_authentication: false,
134 135
      two_factor_grace_period: 48,
      recaptcha_enabled: false,
136 137
      akismet_enabled: false,
      repository_checks_enabled: true,
138
      disabled_oauth_sign_in_sources: [],
139 140
      send_user_confirmation_email: false,
      container_registry_token_expire_delay: 5,
141
      repository_storage: 'default',
142 143
    )
  end
144 145 146 147

  def home_page_url_column_exist
    ActiveRecord::Base.connection.column_exists?(:application_settings, :home_page_url)
  end
148 149 150 151 152 153 154 155

  def restricted_signup_domains_raw
    self.restricted_signup_domains.join("\n") unless self.restricted_signup_domains.nil?
  end

  def restricted_signup_domains_raw=(values)
    self.restricted_signup_domains = []
    self.restricted_signup_domains = values.split(
G
Gabriel Mazetto 已提交
156 157 158 159 160 161
      /\s*[,;]\s*     # comma or semicolon, optionally surrounded by whitespace
      |               # or
      \s              # any whitespace character
      |               # or
      [\r\n]          # any number of newline characters
      /x)
162 163
    self.restricted_signup_domains.reject! { |d| d.empty? }
  end
164 165 166 167

  def runners_registration_token
    ensure_runners_registration_token!
  end
168 169 170 171

  def health_check_access_token
    ensure_health_check_access_token!
  end
172
end