knowledge_base.rb 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class KnowledgeBase < ApplicationModel
  3. include HasTranslations
  4. include HasAgentAllowedParams
  5. include ChecksKbClientNotification
  6. AGENT_ALLOWED_NESTED_RELATIONS = %i[translations].freeze
  7. LAYOUTS = %w[grid list].freeze
  8. ICONSETS = %w[FontAwesome anticon material ionicons Simple-Line-Icons].freeze
  9. has_many :kb_locales, class_name: 'KnowledgeBase::Locale',
  10. inverse_of: :knowledge_base,
  11. dependent: :destroy
  12. accepts_nested_attributes_for :kb_locales, allow_destroy: true
  13. validates :kb_locales, presence: true
  14. validates :kb_locales, length: { maximum: 1, message: 'System supports only one locale for knowledge base. Upgrade your plan to use more locales.' }, unless: :multi_lingual_support?
  15. has_many :categories, class_name: 'KnowledgeBase::Category',
  16. inverse_of: :knowledge_base,
  17. dependent: :restrict_with_exception
  18. has_many :answers, through: :categories
  19. validates :category_layout, inclusion: { in: KnowledgeBase::LAYOUTS }
  20. validates :homepage_layout, inclusion: { in: KnowledgeBase::LAYOUTS }
  21. validates :color_highlight, presence: true, color: true
  22. validates :color_header, presence: true, color: true
  23. validates :iconset, inclusion: { in: KnowledgeBase::ICONSETS }
  24. scope :active, -> { where(active: true) }
  25. scope :check_active_unless_editor, lambda { |user|
  26. return if user&.permissions? 'knowledge_base.editor'
  27. active
  28. }
  29. alias assets_essential assets
  30. def assets(data)
  31. return data if assets_added_to?(data)
  32. data = super(data)
  33. ApplicationModel::CanAssets.reduce(kb_locales + translations, data)
  34. end
  35. # assets without unnecessary bits
  36. def assets_public(data)
  37. data = assets_essential(data)
  38. data[:KnowledgeBase].each do |_, elem|
  39. elem.delete_if do |k, _|
  40. k.end_with?('_ids')
  41. end
  42. end
  43. data
  44. end
  45. def custom_address_uri
  46. return nil if custom_address.blank?
  47. scheme = Setting.get('http_type') || 'http'
  48. URI("#{scheme}://#{custom_address}")
  49. rescue URI::InvalidURIError
  50. nil
  51. end
  52. def custom_address_matches?(request)
  53. uri = custom_address_uri
  54. return false if uri.blank?
  55. given_fqdn = request.headers.env['SERVER_NAME']&.downcase
  56. given_path = request.headers.env['HTTP_X_ORIGINAL_URL']&.downcase
  57. # original url header not present, server not configured
  58. return false if given_path.nil?
  59. # path doesn't match
  60. return false if uri.path.downcase != given_path[0, uri.path.length]
  61. # domain present, but doesn't match
  62. return false if uri.host.present? && uri.host.downcase != given_fqdn
  63. true
  64. rescue URI::InvalidURIError
  65. false
  66. end
  67. def custom_address_prefix(request)
  68. host = custom_address_uri.host || request.headers.env['SERVER_NAME']
  69. port = request.headers.env['SERVER_PORT']
  70. port_silent = request.ssl? && port == '443' || !request.ssl? && port == '80'
  71. port_string = port_silent ? '' : ":#{port}"
  72. "#{custom_address_uri.scheme}://#{host}#{port_string}"
  73. end
  74. def custom_address_path(path)
  75. uri = custom_address_uri
  76. return path if !uri
  77. custom_path = custom_address_uri.path || ''
  78. applied_path = path.gsub(%r{^/help}, custom_path)
  79. applied_path.presence || '/'
  80. end
  81. def canonical_host
  82. custom_address_uri&.host || Setting.get('fqdn')
  83. end
  84. def canonical_scheme_host
  85. "#{Setting.get('http_type')}://#{canonical_host}"
  86. end
  87. def canonical_url(path)
  88. "#{canonical_scheme_host}#{custom_address_path(path)}"
  89. end
  90. def full_destroy!
  91. ChecksKbClientNotification.disable_in_all_classes!
  92. transaction do
  93. # get all categories with their children and reverse to delete children first
  94. categories.root.map(&:self_with_children).flatten.reverse.each(&:full_destroy!)
  95. translations.each(&:destroy!)
  96. kb_locales.each(&:destroy!)
  97. destroy!
  98. end
  99. ensure
  100. ChecksKbClientNotification.enable_in_all_classes!
  101. end
  102. def visible?
  103. active?
  104. end
  105. def api_url
  106. Rails.application.routes.url_helpers.knowledge_base_path(self)
  107. end
  108. def load_category(locale, id)
  109. categories.localed(locale).find_by(id: id)
  110. end
  111. def self.with_multiple_locales_exists?
  112. KnowledgeBase
  113. .active
  114. .joins(:kb_locales)
  115. .group('knowledge_bases.id')
  116. .pluck(Arel.sql('COUNT(knowledge_base_locales.id) as locales_count'))
  117. .any? { |e| e > 1 }
  118. end
  119. private
  120. def set_defaults
  121. self.translations = kb_locales.map do |kb_locale|
  122. name = Setting.get('organization').presence || Setting.get('product_name').presence || 'Zammad'
  123. kb_suffix = ::Translation.translate kb_locale.system_locale.locale, 'Knowledge Base'
  124. KnowledgeBase::Translation.new(
  125. title: "#{name} #{kb_suffix}",
  126. footer_note: "© #{name}",
  127. kb_locale: kb_locale
  128. )
  129. end
  130. end
  131. before_validation :patch_custom_address
  132. after_create :set_defaults
  133. def validate_custom_address
  134. return if custom_address.nil?
  135. # not domain, but no leading slash
  136. if custom_address.exclude?('.') && custom_address[0] != '/'
  137. errors.add(:custom_address, 'must begin with a slash ("/").')
  138. end
  139. if custom_address.include?('://')
  140. errors.add(:custom_address, 'must not include a protocol (e.g., "http://" or "https://").')
  141. end
  142. if custom_address.last == '/'
  143. errors.add(:custom_address, 'must not end with a slash ("/").')
  144. end
  145. if custom_address == '/' # rubocop:disable Style/GuardClause
  146. errors.add(:custom_address, 'Please enter valid path or domain')
  147. end
  148. end
  149. validate :validate_custom_address
  150. def patch_custom_address
  151. self.custom_address = nil if custom_address == ''
  152. end
  153. def multi_lingual_support?
  154. Setting.get 'kb_multi_lingual_support'
  155. end
  156. def set_kb_active_setting
  157. Setting.set 'kb_active', KnowledgeBase.active.exists?
  158. CanBePublished.update_active_publicly!
  159. end
  160. after_destroy :set_kb_active_setting
  161. after_save :set_kb_active_setting
  162. end