assets.rb 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. module Ticket::Assets
  3. extend ActiveSupport::Concern
  4. =begin
  5. get all assets / related models for this ticket
  6. ticket = Ticket.find(123)
  7. result = ticket.assets(assets_if_exists)
  8. returns
  9. result = {
  10. users: {
  11. 123: user_model_123,
  12. 1234: user_model_1234,
  13. },
  14. tickets: [ ticket_model1 ]
  15. }
  16. =end
  17. def assets(data)
  18. app_model = self.class.to_app_model
  19. if !data[ app_model ]
  20. data[ app_model ] = {}
  21. end
  22. return data if data[ app_model ][ id ]
  23. data[ app_model ][ id ] = attributes_with_association_ids
  24. organization&.assets(data)
  25. assets_user(data)
  26. data
  27. end
  28. def assets_user(data)
  29. app_model_user = User.to_app_model
  30. %w[created_by_id updated_by_id owner_id customer_id].each do |local_user_id|
  31. next if !self[ local_user_id ]
  32. next if data[ app_model_user ] && data[ app_model_user ][ self[ local_user_id ] ]
  33. user = User.lookup(id: self[ local_user_id ])
  34. next if !user
  35. data = user.assets(data)
  36. end
  37. end
  38. def authorized_asset?
  39. return true if UserInfo.current_user.blank?
  40. return true if TicketPolicy.new(UserInfo.current_user, self).show?
  41. false
  42. end
  43. end