/app/models/application_setting.rb

https://gitlab.com/pedrolab/gitlab-ce · Ruby · 136 lines · 113 code · 23 blank · 0 comment · 0 complexity · 0eff4e2a2cc18a277317fc5e8ca76ed5 MD5 · raw file

  1. class ApplicationSetting < ActiveRecord::Base
  2. include TokenAuthenticatable
  3. add_authentication_token_field :runners_registration_token
  4. CACHE_KEY = 'application_setting.last'
  5. serialize :restricted_visibility_levels
  6. serialize :import_sources
  7. serialize :restricted_signup_domains, Array
  8. attr_accessor :restricted_signup_domains_raw
  9. validates :session_expire_delay,
  10. presence: true,
  11. numericality: { only_integer: true, greater_than_or_equal_to: 0 }
  12. validates :home_page_url,
  13. allow_blank: true,
  14. url: true,
  15. if: :home_page_url_column_exist
  16. validates :after_sign_out_path,
  17. allow_blank: true,
  18. url: true
  19. validates :admin_notification_email,
  20. email: true,
  21. allow_blank: true
  22. validates :two_factor_grace_period,
  23. numericality: { greater_than_or_equal_to: 0 }
  24. validates :recaptcha_site_key,
  25. presence: true,
  26. if: :recaptcha_enabled
  27. validates :recaptcha_private_key,
  28. presence: true,
  29. if: :recaptcha_enabled
  30. validates :sentry_dsn,
  31. presence: true,
  32. if: :sentry_enabled
  33. validates :akismet_api_key,
  34. presence: true,
  35. if: :akismet_enabled
  36. validates :max_attachment_size,
  37. presence: true,
  38. numericality: { only_integer: true, greater_than: 0 }
  39. validates_each :restricted_visibility_levels do |record, attr, value|
  40. unless value.nil?
  41. value.each do |level|
  42. unless Gitlab::VisibilityLevel.options.has_value?(level)
  43. record.errors.add(attr, "'#{level}' is not a valid visibility level")
  44. end
  45. end
  46. end
  47. end
  48. validates_each :import_sources do |record, attr, value|
  49. unless value.nil?
  50. value.each do |source|
  51. unless Gitlab::ImportSources.options.has_value?(source)
  52. record.errors.add(attr, "'#{source}' is not a import source")
  53. end
  54. end
  55. end
  56. end
  57. before_save :ensure_runners_registration_token
  58. after_commit do
  59. Rails.cache.write(CACHE_KEY, self)
  60. end
  61. def self.current
  62. Rails.cache.fetch(CACHE_KEY) do
  63. ApplicationSetting.last
  64. end
  65. end
  66. def self.expire
  67. Rails.cache.delete(CACHE_KEY)
  68. end
  69. def self.create_from_defaults
  70. create(
  71. default_projects_limit: Settings.gitlab['default_projects_limit'],
  72. default_branch_protection: Settings.gitlab['default_branch_protection'],
  73. signup_enabled: Settings.gitlab['signup_enabled'],
  74. signin_enabled: Settings.gitlab['signin_enabled'],
  75. gravatar_enabled: Settings.gravatar['enabled'],
  76. sign_in_text: Settings.extra['sign_in_text'],
  77. restricted_visibility_levels: Settings.gitlab['restricted_visibility_levels'],
  78. max_attachment_size: Settings.gitlab['max_attachment_size'],
  79. session_expire_delay: Settings.gitlab['session_expire_delay'],
  80. default_project_visibility: Settings.gitlab.default_projects_features['visibility_level'],
  81. default_snippet_visibility: Settings.gitlab.default_projects_features['visibility_level'],
  82. restricted_signup_domains: Settings.gitlab['restricted_signup_domains'],
  83. import_sources: ['github','bitbucket','gitlab','gitorious','google_code','fogbugz','git'],
  84. shared_runners_enabled: Settings.gitlab_ci['shared_runners_enabled'],
  85. max_artifacts_size: Settings.artifacts['max_size'],
  86. require_two_factor_authentication: false,
  87. two_factor_grace_period: 48,
  88. recaptcha_enabled: false,
  89. akismet_enabled: false,
  90. repository_checks_enabled: true,
  91. )
  92. end
  93. def home_page_url_column_exist
  94. ActiveRecord::Base.connection.column_exists?(:application_settings, :home_page_url)
  95. end
  96. def restricted_signup_domains_raw
  97. self.restricted_signup_domains.join("\n") unless self.restricted_signup_domains.nil?
  98. end
  99. def restricted_signup_domains_raw=(values)
  100. self.restricted_signup_domains = []
  101. self.restricted_signup_domains = values.split(
  102. /\s*[,;]\s* # comma or semicolon, optionally surrounded by whitespace
  103. | # or
  104. \s # any whitespace character
  105. | # or
  106. [\r\n] # any number of newline characters
  107. /x)
  108. self.restricted_signup_domains.reject! { |d| d.empty? }
  109. end
  110. def runners_registration_token
  111. ensure_runners_registration_token!
  112. end
  113. end