assets.rb 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class Organization
  3. module Assets
  4. extend ActiveSupport::Concern
  5. =begin
  6. get all assets / related models for this organization
  7. organization = Organization.find(123)
  8. result = organization.assets(assets_if_exists)
  9. returns
  10. result = {
  11. :organizations => {
  12. 123 => organization_model_123,
  13. 1234 => organization_model_1234,
  14. }
  15. }
  16. =end
  17. def assets(data)
  18. app_model_organization = Organization.to_app_model
  19. if !data[ app_model_organization ]
  20. data[ app_model_organization ] = {}
  21. end
  22. return data if data[ app_model_organization ][ id ]
  23. local_attributes = attributes_with_association_ids
  24. # set temp. current attributes to assets pool to prevent
  25. # loops, will be updated with lookup attributes later
  26. data[ app_model_organization ][ id ] = local_attributes
  27. # support primary and secodary members
  28. local_attributes['member_ids'] = Array(local_attributes['member_ids']) | Array(local_attributes['secondary_member_ids'])
  29. app_model_user = User.to_app_model
  30. if local_attributes['member_ids'].present?
  31. # only provide assets for the first 10 organization users
  32. # rest will be loaded optionally by the frontend
  33. local_attributes['member_ids'] = local_attributes['member_ids'].sort
  34. local_attributes['member_ids'][0, 10].each do |local_user_id|
  35. next if data[ app_model_user ] && data[ app_model_user ][ local_user_id ]
  36. user = User.lookup(id: local_user_id)
  37. next if !user
  38. data = user.assets(data)
  39. end
  40. end
  41. data[ app_model_organization ][ id ] = local_attributes
  42. if !data[ app_model_user ]
  43. data[ app_model_user ] = {}
  44. end
  45. data
  46. end
  47. def filter_unauthorized_attributes(attributes)
  48. return super if UserInfo.assets.blank? || UserInfo.assets.agent?
  49. attributes = super
  50. attributes.slice('id', 'name', 'active')
  51. end
  52. end
  53. end