application_setting.rb 4.88 KB
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1 2 3 4
# == Schema Information
#
# Table name: application_settings
#
Stan Hu's avatar
Stan Hu committed
5
#  id                           :integer          not null, primary key
6 7 8 9 10 11 12 13 14 15 16
#  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
Atsushi Ishida's avatar
Atsushi Ishida committed
17
#  version_check_enabled        :boolean          default(TRUE)
Stan Hu's avatar
Stan Hu committed
18
#  max_attachment_size          :integer          default(10), not null
Vinnie Okada's avatar
Vinnie Okada committed
19
#  default_project_visibility   :integer
Stan Hu's avatar
Stan Hu committed
20
#  default_snippet_visibility   :integer
21
#  restricted_signup_domains    :text
Atsushi Ishida's avatar
Atsushi Ishida committed
22
#  user_oauth_applications      :boolean          default(TRUE)
23
#  after_sign_out_path          :string(255)
Atsushi Ishida's avatar
Atsushi Ishida committed
24
#  session_expire_delay         :integer          default(10080), not null
25
#  import_sources               :text
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
26 27 28 29
#  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
30
#  runners_registration_token   :string(255)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
31 32
#

33
class ApplicationSetting < ActiveRecord::Base
34 35 36
  include TokenAuthenticatable
  add_authentication_token_field :runners_registration_token

37 38
  CACHE_KEY = 'application_setting.last'

39
  serialize :restricted_visibility_levels
40
  serialize :import_sources
41 42
  serialize :restricted_signup_domains, Array
  attr_accessor :restricted_signup_domains_raw
43 44 45 46

  validates :session_expire_delay,
    presence: true,
    numericality: { only_integer: true, greater_than_or_equal_to: 0 }
47

48 49
  validates :home_page_url,
    allow_blank: true,
Robert Speicher's avatar
Robert Speicher committed
50
    url: true,
51
    if: :home_page_url_column_exist
52

53 54
  validates :after_sign_out_path,
    allow_blank: true,
Robert Speicher's avatar
Robert Speicher committed
55
    url: true
56

57 58 59 60
  validates :admin_notification_email,
    allow_blank: true,
    email: true

61
  validates_each :restricted_visibility_levels do |record, attr, value|
62 63 64 65 66
    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
67 68 69 70
      end
    end
  end

71 72 73 74 75 76 77 78 79 80
  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

81 82
  before_save :ensure_runners_registration_token

83
  after_commit do
84
    Rails.cache.write(CACHE_KEY, self)
85 86
  end

87
  def self.current
88
    Rails.cache.fetch(CACHE_KEY) do
89 90
      ApplicationSetting.last
    end
91
  end
92

93
  def self.expire
94
    Rails.cache.delete(CACHE_KEY)
95 96
  end

97 98 99
  def self.create_from_defaults
    create(
      default_projects_limit: Settings.gitlab['default_projects_limit'],
100
      default_branch_protection: Settings.gitlab['default_branch_protection'],
101 102
      signup_enabled: Settings.gitlab['signup_enabled'],
      signin_enabled: Settings.gitlab['signin_enabled'],
103
      twitter_sharing_enabled: Settings.gitlab['twitter_sharing_enabled'],
104 105
      gravatar_enabled: Settings.gravatar['enabled'],
      sign_in_text: Settings.extra['sign_in_text'],
106
      restricted_visibility_levels: Settings.gitlab['restricted_visibility_levels'],
Vinnie Okada's avatar
Vinnie Okada committed
107
      max_attachment_size: Settings.gitlab['max_attachment_size'],
108
      session_expire_delay: Settings.gitlab['session_expire_delay'],
Vinnie Okada's avatar
Vinnie Okada committed
109
      default_project_visibility: Settings.gitlab.default_projects_features['visibility_level'],
110
      default_snippet_visibility: Settings.gitlab.default_projects_features['visibility_level'],
111
      restricted_signup_domains: Settings.gitlab['restricted_signup_domains'],
112 113
      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
114
      max_artifacts_size: Settings.artifacts['max_size'],
115 116
    )
  end
117 118 119 120

  def home_page_url_column_exist
    ActiveRecord::Base.connection.column_exists?(:application_settings, :home_page_url)
  end
121 122 123 124 125 126 127 128

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

  def runners_registration_token
    ensure_runners_registration_token!
  end
141
end