attachments_controller_policy.rb 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Controllers::AttachmentsControllerPolicy < Controllers::ApplicationControllerPolicy
  3. def show?
  4. store_object_policy(store_object_owner, allow_kb_preview_token: true)&.show?
  5. end
  6. def destroy?
  7. store_object_policy(store_object_owner)&.destroy?
  8. end
  9. def user_required?
  10. false
  11. end
  12. def custom_exception
  13. ActiveRecord::RecordNotFound.new
  14. end
  15. private
  16. def download_file
  17. record.send(:download_file)
  18. end
  19. def store_object_class
  20. download_file
  21. &.store_object
  22. &.name
  23. &.safe_constantize
  24. end
  25. def store_object_policy(target, allow_kb_preview_token: false)
  26. if allow_kb_preview_token &&
  27. attached_to_kb?(target) &&
  28. (token = record.session[:kb_preview_token])
  29. token_user = Token.check action: 'KnowledgeBasePreview', token: token
  30. end
  31. Pundit.policy token_user || user, target
  32. end
  33. def attached_to_kb?(target)
  34. return true if target.is_a?(KnowledgeBase::Answer::Translation::Content)
  35. return true if target.is_a?(KnowledgeBase::Answer)
  36. false
  37. end
  38. def store_object_owner
  39. return Store.find(download_file.id) if store_object_class == UploadCache
  40. store_object_class
  41. &.find download_file.o_id
  42. end
  43. end