list.rb 955 B

123456789101112131415161718192021222324252627282930
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Service::Link::List < Service::BaseWithCurrentUser
  3. # Create a list of object related references filtered by target type
  4. # (Ticket, KnowledgeBase::Answer::Translation).
  5. #
  6. # Any reference to a target object that is not accessible by the current user
  7. # is filtered out.
  8. def execute(object:, target_type:)
  9. links = ::Link.list(
  10. link_object: object.class.name,
  11. link_object_value: object.id
  12. ).select { |link| link['link_object'] == target_type }
  13. links.filter_map do |link|
  14. type = link['link_type']
  15. target_class = link['link_object'].constantize
  16. target = target_class.find(link['link_object_value'])
  17. # We prefer to do it here instead of passing the user to the Link model.
  18. next if !Pundit.policy(current_user, target).show?
  19. {
  20. item: target,
  21. type: type
  22. }
  23. end.uniq
  24. end
  25. end