assets.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://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. 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. end
  41. return data if !self['created_by_id'] && !self['updated_by_id']
  42. %w[created_by_id updated_by_id].each do |local_user_id|
  43. next if !self[ local_user_id ]
  44. next if data[ User.to_app_model ] && data[ User.to_app_model ][ self[ local_user_id ] ]
  45. user = User.lookup(id: self[ local_user_id ])
  46. next if !user
  47. data = user.assets(data)
  48. end
  49. data
  50. end
  51. end
  52. end