BigW Consortium Gitlab

application_setting.rb 3.64 KB
Newer Older
Dmitriy Zaporozhets committed
1 2 3 4
# == Schema Information
#
# Table name: application_settings
#
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
Stan Hu committed
17
#  max_attachment_size          :integer          default(10), not null
18
#  session_expire_delay       :integer          default(10080), not null
Vinnie Okada committed
19
#  default_project_visibility   :integer
Stan Hu committed
20
#  default_snippet_visibility   :integer
21
#  restricted_signup_domains    :text
22
#  user_oauth_applications      :bool             default(TRUE)
23
#  after_sign_out_path          :string(255)
Dmitriy Zaporozhets committed
24 25
#

26
class ApplicationSetting < ActiveRecord::Base
27
  serialize :restricted_visibility_levels
28 29
  serialize :restricted_signup_domains, Array
  attr_accessor :restricted_signup_domains_raw
30 31 32 33

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

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

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

44
  validates_each :restricted_visibility_levels do |record, attr, value|
45 46 47 48 49
    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
50 51 52 53
      end
    end
  end

54 55 56
  def self.current
    ApplicationSetting.last
  end
57 58 59 60

  def self.create_from_defaults
    create(
      default_projects_limit: Settings.gitlab['default_projects_limit'],
61
      default_branch_protection: Settings.gitlab['default_branch_protection'],
62 63
      signup_enabled: Settings.gitlab['signup_enabled'],
      signin_enabled: Settings.gitlab['signin_enabled'],
64
      twitter_sharing_enabled: Settings.gitlab['twitter_sharing_enabled'],
65 66
      gravatar_enabled: Settings.gravatar['enabled'],
      sign_in_text: Settings.extra['sign_in_text'],
67
      restricted_visibility_levels: Settings.gitlab['restricted_visibility_levels'],
Vinnie Okada committed
68
      max_attachment_size: Settings.gitlab['max_attachment_size'],
69
      session_expire_delay: Settings.gitlab['session_expire_delay'],
Vinnie Okada committed
70
      default_project_visibility: Settings.gitlab.default_projects_features['visibility_level'],
71 72
      default_snippet_visibility: Settings.gitlab.default_projects_features['visibility_level'],
      restricted_signup_domains: Settings.gitlab['restricted_signup_domains']
73 74
    )
  end
75 76 77 78

  def home_page_url_column_exist
    ActiveRecord::Base.connection.column_exists?(:application_settings, :home_page_url)
  end
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

  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

96
end