base_controller.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class KnowledgeBase::Public::BaseController < ApplicationController
  3. before_action :authenticate_with_preview_token
  4. before_action :load_kb
  5. helper_method :system_locale_via_uri, :fallback_locale, :current_user, :find_category,
  6. :filter_primary_kb_locale, :menu_items, :all_locales, :can_preview?
  7. layout 'knowledge_base'
  8. include KnowledgeBaseHelper
  9. private
  10. def load_kb
  11. @knowledge_base = policy_scope(KnowledgeBase)
  12. .localed(guess_locale_via_uri)
  13. .first!
  14. end
  15. def all_locales
  16. @all_locales ||= KnowledgeBase::Locale.system_with_kb_locales(@knowledge_base)
  17. end
  18. def menu_items
  19. @menu_items ||= KnowledgeBase::MenuItem.using_locale(guess_locale_via_uri || filter_primary_kb_locale)
  20. end
  21. def system_locale_via_uri
  22. @system_locale_via_uri ||= guess_locale_via_uri || filter_primary_kb_locale || all_locales.first
  23. end
  24. def fallback_locale
  25. return system_locale_via_uri if all_locales.find { |locale| locale.id == system_locale_via_uri&.id }
  26. filter_primary_kb_locale || all_locales.first
  27. end
  28. def filter_primary_kb_locale
  29. all_locales.find(&:primary_locale?)
  30. end
  31. def guess_locale_via_uri
  32. @guess_locale_via_uri ||= params[:locale].present? ? ::Locale.find_by(locale: params[:locale]) : nil
  33. end
  34. def find_category(id)
  35. @knowledge_base.load_category(system_locale_via_uri, id)
  36. end
  37. def find_locales(object)
  38. return [] if object.blank?
  39. system_locale_ids = KnowledgeBase::Locale.available_for(object).select(:system_locale_id).pluck(:system_locale_id)
  40. all_locales.select { |locale| system_locale_ids.include? locale.id }
  41. end
  42. def categories_filter(list)
  43. list
  44. .localed(system_locale_via_uri)
  45. .sorted
  46. .select { |category| policy(category).show_public? }
  47. end
  48. def answers_filter(list)
  49. answers = list
  50. .localed(system_locale_via_uri)
  51. .sorted
  52. if current_user&.permissions?('knowledge_base.editor')
  53. answers.filter { |answer| policy(answer).show_public? }
  54. else
  55. answers.published
  56. end
  57. end
  58. def find_answer(scope, id, locale: system_locale_via_uri)
  59. return if scope.nil?
  60. scope = scope.include_contents
  61. scope = scope.localed(locale) if locale
  62. if !current_user&.permissions?('knowledge_base.editor')
  63. return scope.published.find_by(id: id)
  64. end
  65. if (item = scope.find_by(id: id)) && policy(item).show_public?
  66. return item
  67. end
  68. nil
  69. end
  70. def can_preview?
  71. @can_preview ||= policy(@knowledge_base).update?
  72. end
  73. def not_found(e)
  74. @knowledge_base = policy_scope(KnowledgeBase).first
  75. if @knowledge_base.nil?
  76. mask_as_not_found(e)
  77. return
  78. end
  79. @page_title_error = :not_found
  80. render 'knowledge_base/public/not_found', status: :not_found
  81. end
  82. def mask_as_not_found(e)
  83. publicly_visible_error = case Rails.env
  84. when 'development'
  85. e
  86. else
  87. ActionController::RoutingError
  88. .new "This page doesn't exist." # rubocop:disable Zammad/DetectTranslatableString
  89. end
  90. logger.error e
  91. respond_to_exception(publicly_visible_error, :not_found)
  92. http_log
  93. end
  94. def authenticate_with_preview_token
  95. if params[:preview_token].present?
  96. session[:kb_preview_token] = params[:preview_token]
  97. end
  98. user = Token.check action: 'KnowledgeBasePreview', token: session[:kb_preview_token]
  99. return if user.blank?
  100. @_auth_type = 'kb_preview_token'
  101. @_current_user = user
  102. end
  103. end