assets.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://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. return data 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_accounts = {}
  32. authorizations.each do |authorization|
  33. local_accounts[authorization.provider] = {
  34. uid: authorization[:uid],
  35. username: authorization[:username]
  36. }
  37. end
  38. local_attributes['accounts'] = local_accounts
  39. # get roles
  40. local_attributes['role_ids']&.each do |role_id|
  41. next if data[:Role] && data[:Role][role_id]
  42. role = Role.lookup(id: role_id)
  43. next if !role
  44. data = role.assets(data)
  45. end
  46. # get groups
  47. local_attributes['group_ids']&.each do |group_id, _access|
  48. next if data[:Group] && data[:Group][group_id]
  49. group = Group.lookup(id: group_id)
  50. next if !group
  51. data = group.assets(data)
  52. end
  53. # get organizations
  54. Array(local_attributes['organization_ids'])[0, 3].each do |organization_id|
  55. next if data[:Organization] && data[:Organization][organization_id]
  56. organization = Organization.lookup(id: organization_id)
  57. next if !organization
  58. data = organization.assets(data)
  59. end
  60. data[ app_model ][ id ] = local_attributes
  61. # add organization
  62. if self.organization_id
  63. if !data[:Organization] || !data[:Organization][self.organization_id] # rubocop:disable Style/SoleNestedConditional
  64. organization = Organization.lookup(id: self.organization_id)
  65. if organization
  66. data = organization.assets(data)
  67. end
  68. end
  69. end
  70. data
  71. end
  72. def filter_unauthorized_attributes(attributes)
  73. return super if UserInfo.assets.blank? || UserInfo.assets.agent?
  74. # customer assets for the user session
  75. if UserInfo.current_user_id == id
  76. attributes = super
  77. attributes.except!('web', 'phone', 'mobile', 'fax', 'department', 'street', 'zip', 'city', 'country', 'address', 'note')
  78. return attributes
  79. end
  80. # customer assets for other user
  81. attributes = super
  82. attributes.slice('id', 'firstname', 'lastname', 'image', 'image_source', 'active')
  83. end
  84. end
  85. end