knowledge_base.rb 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. # Copyright (C) 2012-2021 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. alias assets_essential assets
  26. def assets(data)
  27. return data if assets_added_to?(data)
  28. data = super(data)
  29. ApplicationModel::CanAssets.reduce(kb_locales + translations, data)
  30. end
  31. # assets without unnecessary bits
  32. def assets_public(data)
  33. data = assets_essential(data)
  34. data[:KnowledgeBase].each do |_, elem|
  35. elem.delete_if do |k, _|
  36. k.end_with?('_ids')
  37. end
  38. end
  39. data
  40. end
  41. def custom_address_uri
  42. return nil if custom_address.blank?
  43. scheme = Setting.get('http_type') || 'http'
  44. URI("#{scheme}://#{custom_address}")
  45. rescue URI::InvalidURIError
  46. nil
  47. end
  48. def custom_address_matches?(request)
  49. uri = custom_address_uri
  50. return false if uri.blank?
  51. given_fqdn = request.headers.env['SERVER_NAME']&.downcase
  52. given_path = request.headers.env['HTTP_X_ORIGINAL_URL']&.downcase
  53. # original url header not present, server not configured
  54. return false if given_path.nil?
  55. # path doesn't match
  56. return false if uri.path.downcase != given_path[0, uri.path.length]
  57. # domain present, but doesn't match
  58. return false if uri.host.present? && uri.host.downcase != given_fqdn
  59. true
  60. rescue URI::InvalidURIError
  61. false
  62. end
  63. def custom_address_prefix(request)
  64. host = custom_address_uri.host || request.headers.env['SERVER_NAME']
  65. port = request.headers.env['SERVER_PORT']
  66. port_silent = (request.ssl? && port == '443') || (!request.ssl? && port == '80')
  67. port_string = port_silent ? '' : ":#{port}"
  68. "#{custom_address_uri.scheme}://#{host}#{port_string}"
  69. end
  70. def custom_address_path(path)
  71. uri = custom_address_uri
  72. return path if !uri
  73. custom_path = custom_address_uri.path || ''
  74. applied_path = path.gsub(%r{^/help}, custom_path)
  75. applied_path.presence || '/'
  76. end
  77. def canonical_host
  78. custom_address_uri&.host || Setting.get('fqdn')
  79. end
  80. def canonical_scheme_host
  81. "#{Setting.get('http_type')}://#{canonical_host}"
  82. end
  83. def canonical_url(path)
  84. "#{canonical_scheme_host}#{custom_address_path(path)}"
  85. end
  86. def full_destroy!
  87. ChecksKbClientNotification.disable_in_all_classes!
  88. transaction do
  89. # get all categories with their children and reverse to delete children first
  90. categories.root.map(&:self_with_children).flatten.reverse.each(&:full_destroy!)
  91. translations.each(&:destroy!)
  92. kb_locales.each(&:destroy!)
  93. destroy!
  94. end
  95. ensure
  96. ChecksKbClientNotification.enable_in_all_classes!
  97. end
  98. def visible?
  99. active?
  100. end
  101. def api_url
  102. Rails.application.routes.url_helpers.knowledge_base_path(self)
  103. end
  104. def load_category(locale, id)
  105. categories.localed(locale).find_by(id: id)
  106. end
  107. def self.with_multiple_locales_exists?
  108. KnowledgeBase
  109. .active
  110. .joins(:kb_locales)
  111. .group('knowledge_bases.id')
  112. .pluck(Arel.sql('COUNT(knowledge_base_locales.id) as locales_count'))
  113. .any? { |e| e > 1 }
  114. end
  115. private
  116. def set_defaults
  117. self.translations = kb_locales.map do |kb_locale|
  118. name = Setting.get('organization').presence || Setting.get('product_name').presence || 'Zammad'
  119. kb_suffix = ::Translation.translate kb_locale.system_locale.locale, 'Knowledge Base'
  120. KnowledgeBase::Translation.new(
  121. title: "#{name} #{kb_suffix}",
  122. footer_note: "© #{name}",
  123. kb_locale: kb_locale
  124. )
  125. end
  126. end
  127. before_validation :patch_custom_address
  128. after_create :set_defaults
  129. def validate_custom_address
  130. return if custom_address.nil?
  131. # not domain, but no leading slash
  132. if custom_address.exclude?('.') && custom_address[0] != '/'
  133. errors.add(:custom_address, 'must begin with a slash ("/").')
  134. end
  135. if custom_address.include?('://')
  136. errors.add(:custom_address, 'must not include a protocol (e.g., "http://" or "https://").')
  137. end
  138. if custom_address.last == '/'
  139. errors.add(:custom_address, 'must not end with a slash ("/").')
  140. end
  141. if custom_address == '/' # rubocop:disable Style/GuardClause
  142. errors.add(:custom_address, 'Please enter valid path or domain')
  143. end
  144. end
  145. validate :validate_custom_address
  146. def patch_custom_address
  147. self.custom_address = nil if custom_address == ''
  148. end
  149. def multi_lingual_support?
  150. Setting.get 'kb_multi_lingual_support'
  151. end
  152. def set_kb_active_setting
  153. Setting.set 'kb_active', KnowledgeBase.active.exists?
  154. CanBePublished.update_active_publicly!
  155. end
  156. after_destroy :set_kb_active_setting
  157. after_save :set_kb_active_setting
  158. end