PageRenderTime 20ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/app/models/application_setting.rb

https://gitlab.com/graingert/gitlab-ee
Ruby | 181 lines | 152 code | 29 blank | 0 comment | 2 complexity | 1a1f8f4ec55a5b4f4384f147cbe46115 MD5 | raw file
  1. class ApplicationSetting < ActiveRecord::Base
  2. include TokenAuthenticatable
  3. add_authentication_token_field :runners_registration_token
  4. add_authentication_token_field :health_check_access_token
  5. CACHE_KEY = 'application_setting.last'
  6. serialize :restricted_visibility_levels
  7. serialize :import_sources
  8. serialize :disabled_oauth_sign_in_sources, Array
  9. serialize :restricted_signup_domains, Array
  10. attr_accessor :restricted_signup_domains_raw
  11. validates :session_expire_delay,
  12. presence: true,
  13. numericality: { only_integer: true, greater_than_or_equal_to: 0 }
  14. validates :home_page_url,
  15. allow_blank: true,
  16. url: true,
  17. if: :home_page_url_column_exist
  18. validates :after_sign_out_path,
  19. allow_blank: true,
  20. url: true
  21. validates :admin_notification_email,
  22. email: true,
  23. allow_blank: true
  24. validates :two_factor_grace_period,
  25. numericality: { greater_than_or_equal_to: 0 }
  26. validates :recaptcha_site_key,
  27. presence: true,
  28. if: :recaptcha_enabled
  29. validates :recaptcha_private_key,
  30. presence: true,
  31. if: :recaptcha_enabled
  32. validates :sentry_dsn,
  33. presence: true,
  34. if: :sentry_enabled
  35. validates :akismet_api_key,
  36. presence: true,
  37. if: :akismet_enabled
  38. validates :max_attachment_size,
  39. presence: true,
  40. numericality: { only_integer: true, greater_than: 0 }
  41. validates :container_registry_token_expire_delay,
  42. presence: true,
  43. numericality: { only_integer: true, greater_than: 0 }
  44. validates :elasticsearch_host,
  45. presence: { message: "can't be blank when indexing is enabled" },
  46. if: :elasticsearch_indexing?
  47. validates :elasticsearch_port,
  48. presence: { message: "can't be blank when indexing is enabled" },
  49. if: :elasticsearch_indexing?
  50. validates_each :restricted_visibility_levels do |record, attr, value|
  51. unless value.nil?
  52. value.each do |level|
  53. unless Gitlab::VisibilityLevel.options.has_value?(level)
  54. record.errors.add(attr, "'#{level}' is not a valid visibility level")
  55. end
  56. end
  57. end
  58. end
  59. validates_each :import_sources do |record, attr, value|
  60. unless value.nil?
  61. value.each do |source|
  62. unless Gitlab::ImportSources.options.has_value?(source)
  63. record.errors.add(attr, "'#{source}' is not a import source")
  64. end
  65. end
  66. end
  67. end
  68. validates_each :disabled_oauth_sign_in_sources do |record, attr, value|
  69. unless value.nil?
  70. value.each do |source|
  71. unless Devise.omniauth_providers.include?(source.to_sym)
  72. record.errors.add(attr, "'#{source}' is not an OAuth sign-in source")
  73. end
  74. end
  75. end
  76. end
  77. before_save :ensure_runners_registration_token
  78. before_save :ensure_health_check_access_token
  79. after_commit do
  80. Rails.cache.write(CACHE_KEY, self)
  81. end
  82. def self.current
  83. Rails.cache.fetch(CACHE_KEY) do
  84. ApplicationSetting.last
  85. end
  86. end
  87. def self.expire
  88. Rails.cache.delete(CACHE_KEY)
  89. end
  90. def self.cached
  91. Rails.cache.fetch(CACHE_KEY)
  92. end
  93. def self.create_from_defaults
  94. create(
  95. default_projects_limit: Settings.gitlab['default_projects_limit'],
  96. default_branch_protection: Settings.gitlab['default_branch_protection'],
  97. signup_enabled: Settings.gitlab['signup_enabled'],
  98. signin_enabled: Settings.gitlab['signin_enabled'],
  99. gravatar_enabled: Settings.gravatar['enabled'],
  100. sign_in_text: nil,
  101. after_sign_up_text: nil,
  102. help_page_text: nil,
  103. shared_runners_text: nil,
  104. restricted_visibility_levels: Settings.gitlab['restricted_visibility_levels'],
  105. max_attachment_size: Settings.gitlab['max_attachment_size'],
  106. session_expire_delay: Settings.gitlab['session_expire_delay'],
  107. default_project_visibility: Settings.gitlab.default_projects_features['visibility_level'],
  108. default_snippet_visibility: Settings.gitlab.default_projects_features['visibility_level'],
  109. restricted_signup_domains: Settings.gitlab['restricted_signup_domains'],
  110. import_sources: %w[github bitbucket gitlab gitorious google_code fogbugz git gitlab_project],
  111. shared_runners_enabled: Settings.gitlab_ci['shared_runners_enabled'],
  112. max_artifacts_size: Settings.artifacts['max_size'],
  113. require_two_factor_authentication: false,
  114. two_factor_grace_period: 48,
  115. recaptcha_enabled: false,
  116. akismet_enabled: false,
  117. repository_checks_enabled: true,
  118. disabled_oauth_sign_in_sources: [],
  119. send_user_confirmation_email: false,
  120. container_registry_token_expire_delay: 5,
  121. elasticsearch_host: ENV['ELASTIC_HOST'] || 'localhost',
  122. elasticsearch_port: ENV['ELASTIC_PORT'] || '9200'
  123. )
  124. end
  125. def elasticsearch_host
  126. read_attribute(:elasticsearch_host).split(',').map(&:strip)
  127. end
  128. def home_page_url_column_exist
  129. ActiveRecord::Base.connection.column_exists?(:application_settings, :home_page_url)
  130. end
  131. def restricted_signup_domains_raw
  132. self.restricted_signup_domains.join("\n") unless self.restricted_signup_domains.nil?
  133. end
  134. def restricted_signup_domains_raw=(values)
  135. self.restricted_signup_domains = []
  136. self.restricted_signup_domains = values.split(
  137. /\s*[,;]\s* # comma or semicolon, optionally surrounded by whitespace
  138. | # or
  139. \s # any whitespace character
  140. | # or
  141. [\r\n] # any number of newline characters
  142. /x)
  143. self.restricted_signup_domains.reject! { |d| d.empty? }
  144. end
  145. def runners_registration_token
  146. ensure_runners_registration_token!
  147. end
  148. def health_check_access_token
  149. ensure_health_check_access_token!
  150. end
  151. end