assets.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class User
  3. module Assets
  4. extend ActiveSupport::Concern
  5. =begin
  6. get all assets / related models for this user
  7. user = User.find(123)
  8. result = user.assets(assets_if_exists)
  9. returns
  10. result = {
  11. :User => {
  12. 123 => user_model_123,
  13. 1234 => user_model_1234,
  14. }
  15. }
  16. =end
  17. def assets (data)
  18. return data if assets_added_to?(data)
  19. app_model = User.to_app_model
  20. if !data[ app_model ]
  21. data[ app_model ] = {}
  22. end
  23. if !data[ app_model ][ id ]
  24. local_attributes = attributes_with_association_ids
  25. # do not transfer crypted pw
  26. local_attributes.delete('password')
  27. # set temp. current attributes to assets pool to prevent
  28. # loops, will be updated with lookup attributes later
  29. data[ app_model ][ id ] = local_attributes
  30. # get linked accounts
  31. local_attributes['accounts'] = {}
  32. key = "User::authorizations::#{id}"
  33. local_accounts = Cache.get(key)
  34. if !local_accounts
  35. local_accounts = {}
  36. authorizations = self.authorizations()
  37. authorizations.each do |authorization|
  38. local_accounts[authorization.provider] = {
  39. uid: authorization[:uid],
  40. username: authorization[:username]
  41. }
  42. end
  43. Cache.write(key, local_accounts)
  44. end
  45. local_attributes['accounts'] = local_accounts
  46. # get roles
  47. local_attributes['role_ids']&.each do |role_id|
  48. next if data[:Role] && data[:Role][role_id]
  49. role = Role.lookup(id: role_id)
  50. next if !role
  51. data = role.assets(data)
  52. end
  53. # get groups
  54. local_attributes['group_ids']&.each do |group_id, _access|
  55. next if data[:Group] && data[:Group][group_id]
  56. group = Group.lookup(id: group_id)
  57. next if !group
  58. data = group.assets(data)
  59. end
  60. # get organizations
  61. local_attributes['organization_ids']&.each do |organization_id|
  62. next if data[:Organization] && data[:Organization][organization_id]
  63. organization = Organization.lookup(id: organization_id)
  64. next if !organization
  65. data = organization.assets(data)
  66. end
  67. data[ app_model ][ id ] = local_attributes
  68. end
  69. # add organization
  70. if self.organization_id
  71. if !data[:Organization] || !data[:Organization][self.organization_id]
  72. organization = Organization.lookup(id: self.organization_id)
  73. if organization
  74. data = organization.assets(data)
  75. end
  76. end
  77. end
  78. %w[created_by_id updated_by_id].each do |local_user_id|
  79. next if !self[ local_user_id ]
  80. next if data[ app_model ][ self[ local_user_id ] ]
  81. user = User.lookup(id: self[ local_user_id ])
  82. next if !user
  83. data = user.assets(data)
  84. end
  85. data
  86. end
  87. end
  88. end