assets.rb 2.8 KB

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