list.rb 983 B

12345678910111213141516171819202122232425262728293031
  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. source: object,
  21. target: target,
  22. type: type
  23. }
  24. end.uniq
  25. end
  26. end