knowledge_bases_controller.rb 3.8 KB

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