knowledge_bases_controller.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class KnowledgeBasesController < KnowledgeBase::BaseController
  3. include KnowledgeBaseHelper
  4. def init
  5. render json: assets(params[:answer_translation_content_ids])
  6. end
  7. def visible_ids
  8. render json: calculate_visible_ids
  9. end
  10. def preview
  11. token = Token.renew_token! 'KnowledgeBasePreview'
  12. path = case params[:object]
  13. when 'KnowledgeBase'
  14. help_root_path params[:locale], preview_token: token
  15. when 'KnowledgeBaseCategory'
  16. help_category_path params[:locale], params[:id], preview_token: token
  17. when 'KnowledgeBaseAnswer'
  18. category_id = KnowledgeBase::Answer.find(params[:id]).category_id
  19. help_answer_path params[:locale], category_id, params[:id], preview_token: token
  20. end
  21. redirect_to custom_path(path, KnowledgeBase.first, full: true), allow_other_host: true
  22. end
  23. private
  24. def assets(answer_translation_content_ids = nil)
  25. if KnowledgeBase.granular_permissions?
  26. return granular_assets(answer_translation_content_ids) if kb_permissions?
  27. else
  28. return editor_assets(answer_translation_content_ids) if kb_permission_editor?
  29. return reader_assets(answer_translation_content_ids) if kb_permission_reader?
  30. end
  31. public_assets
  32. end
  33. def calculate_visible_ids
  34. if KnowledgeBase.granular_permissions? && kb_permissions?
  35. return KnowledgeBase::InternalAssets.new(current_user).visible_ids
  36. end
  37. if kb_permission_editor?
  38. editor_assets_visible_ids
  39. elsif kb_permission_reader?
  40. reader_assets_visible_ids
  41. else
  42. {}
  43. end
  44. end
  45. def kb_permissions?
  46. current_user&.permissions?(%w[knowledge_base.editor knowledge_base.reader])
  47. end
  48. def kb_permission_editor?
  49. current_user&.permissions?('knowledge_base.editor')
  50. end
  51. def kb_permission_reader?
  52. current_user&.permissions?('knowledge_base.reader')
  53. end
  54. def granular_assets(answer_translation_content_ids)
  55. KnowledgeBase::InternalAssets
  56. .new(current_user, answer_translation_content_ids: answer_translation_content_ids)
  57. .collect_assets
  58. end
  59. def editor_assets(answer_translation_content_ids)
  60. assets = [
  61. KnowledgeBase,
  62. KnowledgeBase::Translation,
  63. KnowledgeBase::Locale,
  64. KnowledgeBase::Category,
  65. KnowledgeBase::Category::Translation,
  66. KnowledgeBase::Answer,
  67. KnowledgeBase::Answer::Translation
  68. ].each_with_object({}) do |klass, memo|
  69. klass.find_in_batches do |group|
  70. memo = ApplicationModel::CanAssets.reduce(group, memo, :essential)
  71. end
  72. end
  73. if answer_translation_content_ids.present?
  74. contents = KnowledgeBase::Answer::Translation::Content.where(id: answer_translation_content_ids)
  75. assets = ApplicationModel::CanAssets.reduce(contents, assets)
  76. end
  77. assets
  78. end
  79. def editor_assets_visible_ids
  80. {
  81. answer_ids: KnowledgeBase::Answer.pluck(:id),
  82. category_ids: KnowledgeBase::Category.pluck(:id)
  83. }
  84. end
  85. def reader_assets(answer_translation_content_ids)
  86. assets = [
  87. KnowledgeBase,
  88. KnowledgeBase::Translation,
  89. KnowledgeBase::Locale,
  90. KnowledgeBase::Category,
  91. KnowledgeBase::Category::Translation
  92. ].each_with_object({}) do |klass, memo|
  93. klass.find_in_batches do |group|
  94. memo = ApplicationModel::CanAssets.reduce(group, memo, :essential)
  95. end
  96. end
  97. KnowledgeBase::Answer.internal.find_in_batches do |group|
  98. assets = ApplicationModel::CanAssets.reduce group, assets, :essential
  99. translations = KnowledgeBase::Answer::Translation.where(answer_id: group.pluck(:id))
  100. assets = ApplicationModel::CanAssets.reduce(translations, assets, :essential)
  101. if answer_translation_content_ids.present?
  102. contents = KnowledgeBase::Answer::Translation::Content
  103. .joins(:translation)
  104. .where(
  105. id: answer_translation_content_ids,
  106. knowledge_base_answer_translations: { answer_id: group }
  107. )
  108. assets = ApplicationModel::CanAssets.reduce(contents, assets)
  109. end
  110. end
  111. assets
  112. end
  113. def reader_assets_visible_ids
  114. {
  115. answer_ids: KnowledgeBase::Answer.internal.pluck(:id),
  116. category_ids: KnowledgeBase::Category.pluck(:id)
  117. }
  118. end
  119. # assets for users who don't have KB permissions
  120. def public_assets
  121. return [] if !Setting.get('kb_active_publicly')
  122. ApplicationModel::CanAssets.reduce(KnowledgeBase.active, {}, :public)
  123. end
  124. end