backend.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class ObjectManager::Element::Backend
  3. attr_reader :user, :attribute, :record, :skip_permission
  4. def initialize(user:, attribute:, record:, skip_permission: false)
  5. @user = user
  6. @attribute = attribute
  7. @record = record
  8. @skip_permission = skip_permission
  9. end
  10. def visible?
  11. return true if skip_permission
  12. return true if attribute.data_option[:permission].blank?
  13. return false if user.blank?
  14. attribute.data_option[:permission].any? do |permission|
  15. authorized?(permission)
  16. end
  17. end
  18. def authorized?(permission)
  19. return true if skip_permission
  20. user.permissions?(permission)
  21. end
  22. def data
  23. data = default_data
  24. data[:screen] = screens if attribute.screens.present?
  25. return data if attribute.data_option.blank?
  26. data.merge(attribute.data_option.symbolize_keys)
  27. end
  28. def default_data
  29. {
  30. name: attribute.name,
  31. display: attribute.display,
  32. tag: attribute.data_type,
  33. # :null => attribute.null,
  34. }
  35. end
  36. def screens
  37. @screens ||= attribute.screens.transform_values do |permission_options|
  38. screen_value(permission_options)
  39. end
  40. end
  41. def screen_value(permission_options)
  42. return permission_options['-all-'] if permission_options['-all-']
  43. return {} if user.blank?
  44. screen_permission_options(permission_options)
  45. end
  46. def screen_permission_options(permission_options)
  47. booleans = [true, false]
  48. permission_options.each_with_object({}) do |(permission, options), result|
  49. next if !authorized?(permission)
  50. options.each do |key, value|
  51. next if booleans.include?(result[key]) && !value
  52. result[key] = value
  53. end
  54. end
  55. end
  56. end