assets.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Copyright (C) 2012-2014 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. if !data[ self.class.to_app_model ]
  18. data[ self.class.to_app_model ] = {}
  19. end
  20. if !data[ self.class.to_app_model ][ id ]
  21. attributes = attributes_with_associations
  22. # remove passwords if use is no admin
  23. access = false
  24. if UserInfo.current_user_id
  25. user = User.lookup(id: UserInfo.current_user_id)
  26. if user.role?('Admin')
  27. access = true
  28. end
  29. end
  30. if !access
  31. %w(inbound outbound).each {|key|
  32. if attributes['options'] && attributes['options'][key] && attributes['options'][key]['options']
  33. attributes['options'][key]['options'].delete('password')
  34. end
  35. }
  36. end
  37. data[ self.class.to_app_model ][ id ] = attributes
  38. end
  39. return data if !self['created_by_id'] && !self['updated_by_id']
  40. %w(created_by_id updated_by_id).each {|local_user_id|
  41. next if !self[ local_user_id ]
  42. next if data[ User.to_app_model ] && data[ User.to_app_model ][ self[ local_user_id ] ]
  43. user = User.lookup( id: self[ local_user_id ] )
  44. data = user.assets( data )
  45. }
  46. data
  47. end
  48. end
  49. end