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

5
6
  CACHE_KEY = 'application_setting.last'

7
  serialize :restricted_visibility_levels
8
  serialize :import_sources
9
10
  serialize :restricted_signup_domains, Array
  attr_accessor :restricted_signup_domains_raw
11
12

  validates :session_expire_delay,
13
14
            presence: true,
            numericality: { only_integer: true, greater_than_or_equal_to: 0 }
15

16
  validates :home_page_url,
17
18
19
            allow_blank: true,
            url: true,
            if: :home_page_url_column_exist
20

21
  validates :after_sign_out_path,
22
23
            allow_blank: true,
            url: true
24

25
  validates :admin_notification_email,
26
            email: true,
27
            allow_blank: true
28

29
  validates :two_factor_grace_period,
30
31
32
33
34
35
36
37
38
            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
39

Jeroen Nijhof's avatar
Jeroen Nijhof committed
40
41
42
43
  validates :sentry_dsn,
            presence: true,
            if: :sentry_enabled

44
45
46
47
  validates :akismet_api_key,
            presence: true,
            if: :akismet_enabled

48
49
50
51
  validates :max_attachment_size,
            presence: true,
            numericality: { only_integer: true, greater_than: 0 }

52
  validates_each :restricted_visibility_levels do |record, attr, value|
Vinnie Okada's avatar
Vinnie Okada committed
53
54
55
56
57
    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
58
59
60
61
      end
    end
  end

62
63
64
65
66
67
68
69
70
71
  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

72
73
  before_save :ensure_runners_registration_token

74
  after_commit do
75
    Rails.cache.write(CACHE_KEY, self)
76
77
  end

78
  def self.current
79
    Rails.cache.fetch(CACHE_KEY) do
80
81
      ApplicationSetting.last
    end
82
  end
83

84
  def self.expire
85
    Rails.cache.delete(CACHE_KEY)
86
87
  end

88
89
90
  def self.create_from_defaults
    create(
      default_projects_limit: Settings.gitlab['default_projects_limit'],
91
      default_branch_protection: Settings.gitlab['default_branch_protection'],
92
93
94
95
      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'],
96
      restricted_visibility_levels: Settings.gitlab['restricted_visibility_levels'],
Vinnie Okada's avatar
Vinnie Okada committed
97
      max_attachment_size: Settings.gitlab['max_attachment_size'],
98
      session_expire_delay: Settings.gitlab['session_expire_delay'],
Vinnie Okada's avatar
Vinnie Okada committed
99
      default_project_visibility: Settings.gitlab.default_projects_features['visibility_level'],
100
      default_snippet_visibility: Settings.gitlab.default_projects_features['visibility_level'],
101
      restricted_signup_domains: Settings.gitlab['restricted_signup_domains'],
102
103
      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
104
      max_artifacts_size: Settings.artifacts['max_size'],
105
      require_two_factor_authentication: false,
106
107
      two_factor_grace_period: 48,
      recaptcha_enabled: false,
108
109
      akismet_enabled: false,
      repository_checks_enabled: true,
110
111
    )
  end
112
113
114
115

  def home_page_url_column_exist
    ActiveRecord::Base.connection.column_exists?(:application_settings, :home_page_url)
  end
116
117
118
119
120
121
122
123

  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
124
125
126
127
128
129
      /\s*[,;]\s*     # comma or semicolon, optionally surrounded by whitespace
      |               # or
      \s              # any whitespace character
      |               # or
      [\r\n]          # any number of newline characters
      /x)
130
131
    self.restricted_signup_domains.reject! { |d| d.empty? }
  end
132
133
134
135

  def runners_registration_token
    ensure_runners_registration_token!
  end
136
end