application_setting.rb 6.5 KB
Newer Older
1
class ApplicationSetting < ActiveRecord::Base
2
  include CacheMarkdownField
3
  include TokenAuthenticatable
4

5
  add_authentication_token_field :runners_registration_token
6
  add_authentication_token_field :health_check_access_token
7

8
  CACHE_KEY = 'application_setting.last'
9 10 11 12 13 14
  DOMAIN_LIST_SEPARATOR = %r{\s*[,;]\s*     # comma or semicolon, optionally surrounded by whitespace
                            |               # or
                            \s              # any whitespace character
                            |               # or
                            [\r\n]          # any number of newline characters
                          }x
15

16
  serialize :restricted_visibility_levels
17
  serialize :import_sources
18
  serialize :disabled_oauth_sign_in_sources, Array
19
  serialize :domain_whitelist, Array
20 21
  serialize :domain_blacklist, Array

22 23 24 25 26
  cache_markdown_field :sign_in_text
  cache_markdown_field :help_page_text
  cache_markdown_field :shared_runners_text, pipeline: :plain_markdown
  cache_markdown_field :after_sign_up_text

27
  attr_accessor :domain_whitelist_raw, :domain_blacklist_raw
28 29

  validates :session_expire_delay,
30 31
            presence: true,
            numericality: { only_integer: true, greater_than_or_equal_to: 0 }
32

33
  validates :home_page_url,
34 35 36
            allow_blank: true,
            url: true,
            if: :home_page_url_column_exist
37

38
  validates :after_sign_out_path,
39 40
            allow_blank: true,
            url: true
41

D
Douwe Maan 已提交
42
  validates :admin_notification_email,
43
            email: true,
44
            allow_blank: true
D
Douwe Maan 已提交
45

46
  validates :two_factor_grace_period,
47 48 49 50 51 52 53 54 55
            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
56

J
Jeroen Nijhof 已提交
57 58 59 60
  validates :sentry_dsn,
            presence: true,
            if: :sentry_enabled

61 62 63 64
  validates :akismet_api_key,
            presence: true,
            if: :akismet_enabled

65 66 67 68
  validates :koding_url,
            presence: true,
            if: :koding_enabled

69 70 71 72
  validates :max_attachment_size,
            presence: true,
            numericality: { only_integer: true, greater_than: 0 }

73 74 75 76
  validates :container_registry_token_expire_delay,
            presence: true,
            numericality: { only_integer: true, greater_than: 0 }

77 78 79 80
  validates :repository_storage,
    presence: true,
    inclusion: { in: ->(_object) { Gitlab.config.repositories.storages.keys } }

81
  validates :enabled_git_access_protocol,
82
            inclusion: { in: %w(ssh http), allow_blank: true, allow_nil: true }
83

84
  validates :domain_blacklist,
85
            presence: { message: 'Domain blacklist cannot be empty if Blacklist is enabled.' },
86 87
            if: :domain_blacklist_enabled?

88
  validates_each :restricted_visibility_levels do |record, attr, value|
V
Vinnie Okada 已提交
89 90 91 92 93
    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
94 95 96 97
      end
    end
  end

98 99 100 101 102 103 104 105 106 107
  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

108 109 110 111
  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 已提交
112
          record.errors.add(attr, "'#{source}' is not an OAuth sign-in source")
113 114 115 116 117
        end
      end
    end
  end

118
  before_save :ensure_runners_registration_token
119
  before_save :ensure_health_check_access_token
120

121
  after_commit do
122
    Rails.cache.write(CACHE_KEY, self)
123 124
  end

125
  def self.current
126
    Rails.cache.fetch(CACHE_KEY) do
127 128
      ApplicationSetting.last
    end
129
  end
130

131
  def self.expire
132
    Rails.cache.delete(CACHE_KEY)
133 134
  end

135 136 137 138
  def self.cached
    Rails.cache.fetch(CACHE_KEY)
  end

139 140 141
  def self.create_from_defaults
    create(
      default_projects_limit: Settings.gitlab['default_projects_limit'],
142
      default_branch_protection: Settings.gitlab['default_branch_protection'],
143 144 145
      signup_enabled: Settings.gitlab['signup_enabled'],
      signin_enabled: Settings.gitlab['signin_enabled'],
      gravatar_enabled: Settings.gravatar['enabled'],
146 147 148 149
      sign_in_text: nil,
      after_sign_up_text: nil,
      help_page_text: nil,
      shared_runners_text: nil,
150
      restricted_visibility_levels: Settings.gitlab['restricted_visibility_levels'],
V
Vinnie Okada 已提交
151
      max_attachment_size: Settings.gitlab['max_attachment_size'],
152
      session_expire_delay: Settings.gitlab['session_expire_delay'],
V
Vinnie Okada 已提交
153
      default_project_visibility: Settings.gitlab.default_projects_features['visibility_level'],
154
      default_snippet_visibility: Settings.gitlab.default_projects_features['visibility_level'],
155
      domain_whitelist: Settings.gitlab['domain_whitelist'],
156
      import_sources: Gitlab::ImportSources.values,
157
      shared_runners_enabled: Settings.gitlab_ci['shared_runners_enabled'],
K
Kamil Trzcinski 已提交
158
      max_artifacts_size: Settings.artifacts['max_size'],
159
      require_two_factor_authentication: false,
160 161
      two_factor_grace_period: 48,
      recaptcha_enabled: false,
162
      akismet_enabled: false,
163 164
      koding_enabled: false,
      koding_url: nil,
165
      repository_checks_enabled: true,
166
      disabled_oauth_sign_in_sources: [],
167 168
      send_user_confirmation_email: false,
      container_registry_token_expire_delay: 5,
169
      repository_storage: 'default',
170
      user_default_external: false,
171 172
    )
  end
173 174 175 176

  def home_page_url_column_exist
    ActiveRecord::Base.connection.column_exists?(:application_settings, :home_page_url)
  end
177

178 179
  def domain_whitelist_raw
    self.domain_whitelist.join("\n") unless self.domain_whitelist.nil?
180 181
  end

182 183 184 185
  def domain_blacklist_raw
    self.domain_blacklist.join("\n") unless self.domain_blacklist.nil?
  end

186 187 188 189 190
  def domain_whitelist_raw=(values)
    self.domain_whitelist = []
    self.domain_whitelist = values.split(DOMAIN_LIST_SEPARATOR)
    self.domain_whitelist.reject! { |d| d.empty? }
    self.domain_whitelist
191
  end
192

193 194
  def domain_blacklist_raw=(values)
    self.domain_blacklist = []
195
    self.domain_blacklist = values.split(DOMAIN_LIST_SEPARATOR)
196
    self.domain_blacklist.reject! { |d| d.empty? }
197
    self.domain_blacklist
198 199 200 201 202 203
  end

  def domain_blacklist_file=(file)
    self.domain_blacklist_raw = file.read
  end

204 205 206
  def runners_registration_token
    ensure_runners_registration_token!
  end
207 208 209 210

  def health_check_access_token
    ensure_health_check_access_token!
  end
211
end