assets.rb 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Channel
  3. module Assets
  4. =begin
  5. get all assets / related models for this channel
  6. channel = Channel.find(123)
  7. result = channel.assets(assets_if_exists)
  8. returns
  9. result = {
  10. :channels => {
  11. 123 => channel_model_123,
  12. 1234 => channel_model_1234,
  13. }
  14. }
  15. =end
  16. def assets(data = {})
  17. app_model = self.class.to_app_model
  18. if !data[ app_model ]
  19. data[ app_model ] = {}
  20. end
  21. if !data[ app_model ][ id ]
  22. attributes = attributes_with_association_ids
  23. # remove passwords if use is no admin
  24. access = false
  25. if UserInfo.current_user_id
  26. user = User.lookup(id: UserInfo.current_user_id)
  27. if user.permissions?('admin.channel')
  28. access = true
  29. end
  30. end
  31. if !access
  32. %w[inbound outbound].each do |key|
  33. if attributes['options'] && attributes['options'][key] && attributes['options'][key]['options']
  34. attributes['options'][key]['options'].delete('password')
  35. end
  36. end
  37. end
  38. data[ self.class.to_app_model ][ id ] = attributes
  39. end
  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