application_setting.rb 6.55 KB
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1 2 3 4
# == Schema Information
#
# Table name: application_settings
#
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#  id                                :integer          not null, primary key
#  default_projects_limit            :integer
#  signup_enabled                    :boolean
#  signin_enabled                    :boolean
#  gravatar_enabled                  :boolean
#  sign_in_text                      :text
#  created_at                        :datetime
#  updated_at                        :datetime
#  home_page_url                     :string(255)
#  default_branch_protection         :integer          default(2)
#  twitter_sharing_enabled           :boolean          default(TRUE)
#  restricted_visibility_levels      :text
#  version_check_enabled             :boolean          default(TRUE)
#  max_attachment_size               :integer          default(10), not null
#  default_project_visibility        :integer
#  default_snippet_visibility        :integer
#  restricted_signup_domains         :text
#  user_oauth_applications           :boolean          default(TRUE)
#  after_sign_out_path               :string(255)
#  session_expire_delay              :integer          default(10080), not null
#  import_sources                    :text
#  help_page_text                    :text
#  admin_notification_email          :string(255)
#  shared_runners_enabled            :boolean          default(TRUE), not null
#  max_artifacts_size                :integer          default(100), not null
Stan Hu's avatar
Stan Hu committed
30 31
#  runners_registration_token        :string
#  require_two_factor_authentication :boolean          default(FALSE)
32
#  two_factor_grace_period           :integer          default(48)
Stan Hu's avatar
Stan Hu committed
33 34 35 36 37 38 39 40 41 42 43
#  metrics_enabled                   :boolean          default(FALSE)
#  metrics_host                      :string           default("localhost")
#  metrics_username                  :string
#  metrics_password                  :string
#  metrics_pool_size                 :integer          default(16)
#  metrics_timeout                   :integer          default(10)
#  metrics_method_call_threshold     :integer          default(10)
#  recaptcha_enabled                 :boolean          default(FALSE)
#  recaptcha_site_key                :string
#  recaptcha_private_key             :string
#  metrics_port                      :integer          default(8089)
44 45
#  sentry_enabled                    :boolean          default(FALSE)
#  sentry_dsn                        :string
46 47
#  ip_blocking_enabled               :boolean          default(FALSE)
#  dns_blacklist_threshold           :float            default(0.33)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
48 49
#

50
class ApplicationSetting < ActiveRecord::Base
51 52 53
  include TokenAuthenticatable
  add_authentication_token_field :runners_registration_token

54 55
  CACHE_KEY = 'application_setting.last'

56
  serialize :restricted_visibility_levels
57
  serialize :import_sources
58 59
  serialize :restricted_signup_domains, Array
  attr_accessor :restricted_signup_domains_raw
60 61

  validates :session_expire_delay,
62 63
            presence: true,
            numericality: { only_integer: true, greater_than_or_equal_to: 0 }
64

65
  validates :home_page_url,
66 67 68
            allow_blank: true,
            url: true,
            if: :home_page_url_column_exist
69

70
  validates :after_sign_out_path,
71 72
            allow_blank: true,
            url: true
73

74
  validates :admin_notification_email,
75 76
            allow_blank: true,
            email: true
77

78
  validates :two_factor_grace_period,
79 80 81 82 83 84 85 86 87
            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
88

89 90 91 92
  validates :sentry_dsn,
            presence: true,
            if: :sentry_enabled

93
  validates_each :restricted_visibility_levels do |record, attr, value|
94 95 96 97 98
    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
99 100 101 102
      end
    end
  end

103 104 105 106 107 108 109 110 111 112
  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

113 114
  before_save :ensure_runners_registration_token

115
  after_commit do
116
    Rails.cache.write(CACHE_KEY, self)
117 118
  end

119
  def self.current
120
    Rails.cache.fetch(CACHE_KEY) do
121 122
      ApplicationSetting.last
    end
123
  end
124

125
  def self.expire
126
    Rails.cache.delete(CACHE_KEY)
127 128
  end

129 130 131
  def self.create_from_defaults
    create(
      default_projects_limit: Settings.gitlab['default_projects_limit'],
132
      default_branch_protection: Settings.gitlab['default_branch_protection'],
133 134
      signup_enabled: Settings.gitlab['signup_enabled'],
      signin_enabled: Settings.gitlab['signin_enabled'],
135
      twitter_sharing_enabled: Settings.gitlab['twitter_sharing_enabled'],
136 137
      gravatar_enabled: Settings.gravatar['enabled'],
      sign_in_text: Settings.extra['sign_in_text'],
138
      restricted_visibility_levels: Settings.gitlab['restricted_visibility_levels'],
Vinnie Okada's avatar
Vinnie Okada committed
139
      max_attachment_size: Settings.gitlab['max_attachment_size'],
140
      session_expire_delay: Settings.gitlab['session_expire_delay'],
Vinnie Okada's avatar
Vinnie Okada committed
141
      default_project_visibility: Settings.gitlab.default_projects_features['visibility_level'],
142
      default_snippet_visibility: Settings.gitlab.default_projects_features['visibility_level'],
143
      restricted_signup_domains: Settings.gitlab['restricted_signup_domains'],
144 145
      import_sources: ['github','bitbucket','gitlab','gitorious','google_code','fogbugz','git'],
      shared_runners_enabled: Settings.gitlab_ci['shared_runners_enabled'],
Kamil Trzcinski's avatar
Kamil Trzcinski committed
146
      max_artifacts_size: Settings.artifacts['max_size'],
147 148
      require_two_factor_authentication: false,
      two_factor_grace_period: 48
149 150
    )
  end
151 152 153 154

  def home_page_url_column_exist
    ActiveRecord::Base.connection.column_exists?(:application_settings, :home_page_url)
  end
155 156 157 158 159 160 161 162

  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(
Gabriel Mazetto's avatar
Gabriel Mazetto committed
163 164 165 166 167 168
      /\s*[,;]\s*     # comma or semicolon, optionally surrounded by whitespace
      |               # or
      \s              # any whitespace character
      |               # or
      [\r\n]          # any number of newline characters
      /x)
169 170
    self.restricted_signup_domains.reject! { |d| d.empty? }
  end
171 172 173 174

  def runners_registration_token
    ensure_runners_registration_token!
  end
175
end