assets.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Channel
  3. module Assets
  4. extend ActiveSupport::Concern
  5. =begin
  6. get all assets / related models for this channel
  7. channel = Channel.find(123)
  8. result = channel.assets(assets_if_exists)
  9. returns
  10. result = {
  11. :channels => {
  12. 123 => channel_model_123,
  13. 1234 => channel_model_1234,
  14. }
  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. attributes = attributes_with_association_ids
  24. # remove passwords if use is no admin
  25. access = false
  26. if UserInfo.current_user_id
  27. user = User.lookup(id: UserInfo.current_user_id)
  28. if user.permissions?('admin.channel')
  29. access = true
  30. end
  31. end
  32. if !access
  33. %w[inbound outbound].each do |key|
  34. if attributes['options'] && attributes['options'][key] && attributes['options'][key]['options']
  35. attributes['options'][key]['options'].delete('password')
  36. end
  37. end
  38. end
  39. data[ self.class.to_app_model ][ id ] = attributes
  40. return data if !self['created_by_id'] && !self['updated_by_id']
  41. %w[created_by_id updated_by_id].each do |local_user_id|
  42. next if !self[ local_user_id ]
  43. next if data[ User.to_app_model ] && data[ User.to_app_model ][ self[ local_user_id ] ]
  44. user = User.lookup(id: self[ local_user_id ])
  45. next if !user
  46. data = user.assets(data)
  47. end
  48. data
  49. end
  50. end
  51. end