assets.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. module User::Assets
  3. =begin
  4. get all assets / related models for this user
  5. user = User.find(123)
  6. result = user.assets( assets_if_exists )
  7. returns
  8. result = {
  9. :User => {
  10. 123 => user_model_123,
  11. 1234 => user_model_1234,
  12. }
  13. }
  14. =end
  15. def assets (data)
  16. if !data[ User.to_app_model ]
  17. data[ User.to_app_model ] = {}
  18. end
  19. if !data[ User.to_app_model ][ self.id ]
  20. attributes = self.attributes_with_associations
  21. # do not transfer crypted pw
  22. attributes['password'] = ''
  23. # get linked accounts
  24. attributes['accounts'] = {}
  25. authorizations = self.authorizations()
  26. authorizations.each do | authorization |
  27. attributes['accounts'][authorization.provider] = {
  28. :uid => authorization[:uid],
  29. :username => authorization[:username]
  30. }
  31. end
  32. data[ User.to_app_model ][ self.id ] = attributes
  33. # get roles
  34. if attributes['role_ids']
  35. attributes['role_ids'].each {|role_id|
  36. role = Role.lookup( :id => role_id )
  37. data = role.assets( data )
  38. }
  39. end
  40. # get groups
  41. if attributes['group_ids']
  42. attributes['group_ids'].each {|group_id|
  43. group = Group.lookup( :id => group_id )
  44. data = group.assets( data )
  45. }
  46. end
  47. # get groups
  48. if attributes['organization_ids']
  49. attributes['organization_ids'].each {|organization_id|
  50. organization = Organization.lookup( :id => organization_id )
  51. data = organization.assets( data )
  52. }
  53. end
  54. end
  55. if self.organization_id
  56. if !data[ Organization.to_app_model ] || !data[ Organization.to_app_model ][ self.organization_id ]
  57. organization = Organization.lookup( :id => self.organization_id )
  58. data = organization.assets( data )
  59. end
  60. end
  61. ['created_by_id', 'updated_by_id'].each {|item|
  62. if self[ item ]
  63. if !data[ User.to_app_model ][ self[ item ] ]
  64. user = User.lookup( :id => self[ item ] )
  65. data = user.assets( data )
  66. end
  67. end
  68. }
  69. data
  70. end
  71. end