application_setting.rb 4.22 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
class ApplicationSetting < ActiveRecord::Base
29
  serialize :restricted_visibility_levels
30
  serialize :import_sources
31 32
  serialize :restricted_signup_domains, Array
  attr_accessor :restricted_signup_domains_raw
33 34 35 36

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

38 39
  validates :home_page_url,
    allow_blank: true,
40
    format: { with: /\A#{URI.regexp(%w(http https))}\z/, message: "should be a valid url" },
41
    if: :home_page_url_column_exist
42

43 44 45 46
  validates :after_sign_out_path,
    allow_blank: true,
    format: { with: /\A#{URI.regexp(%w(http https))}\z/, message: "should be a valid url" }

47 48 49 50
  validates :admin_notification_email,
    allow_blank: true,
    email: true

51
  validates_each :restricted_visibility_levels do |record, attr, value|
52 53 54 55 56
    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
57 58 59 60
      end
    end
  end

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

71 72 73
  def self.current
    ApplicationSetting.last
  end
74 75 76 77

  def self.create_from_defaults
    create(
      default_projects_limit: Settings.gitlab['default_projects_limit'],
78
      default_branch_protection: Settings.gitlab['default_branch_protection'],
79 80
      signup_enabled: Settings.gitlab['signup_enabled'],
      signin_enabled: Settings.gitlab['signin_enabled'],
81
      twitter_sharing_enabled: Settings.gitlab['twitter_sharing_enabled'],
82 83
      gravatar_enabled: Settings.gravatar['enabled'],
      sign_in_text: Settings.extra['sign_in_text'],
84
      restricted_visibility_levels: Settings.gitlab['restricted_visibility_levels'],
Vinnie Okada's avatar
Vinnie Okada committed
85
      max_attachment_size: Settings.gitlab['max_attachment_size'],
86
      session_expire_delay: Settings.gitlab['session_expire_delay'],
Vinnie Okada's avatar
Vinnie Okada committed
87
      default_project_visibility: Settings.gitlab.default_projects_features['visibility_level'],
88
      default_snippet_visibility: Settings.gitlab.default_projects_features['visibility_level'],
89
      restricted_signup_domains: Settings.gitlab['restricted_signup_domains'],
90
      import_sources: ['github','bitbucket','gitlab','gitorious','google_code','fogbugz','git']
91 92
    )
  end
93 94 95 96

  def home_page_url_column_exist
    ActiveRecord::Base.connection.column_exists?(:application_settings, :home_page_url)
  end
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

  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(
        /\s*[,;]\s*     # comma or semicolon, optionally surrounded by whitespace
        |               # or
        \s              # any whitespace character
        |               # or
        [\r\n]          # any number of newline characters
        /x)
    self.restricted_signup_domains.reject! { |d| d.empty? }
  end

114
end