condition.rb 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class CoreWorkflow::Condition
  3. include ::Mixin::HasBackends
  4. attr_accessor :user, :payload, :workflow, :attribute_object, :result_object, :check
  5. def initialize(result_object:, workflow:)
  6. @user = result_object.user
  7. @payload = result_object.payload
  8. @workflow = workflow
  9. @attribute_object = result_object.attributes
  10. @result_object = result_object
  11. @check = nil
  12. end
  13. def attributes
  14. @attribute_object.send(@check)
  15. end
  16. def condition_key_value_object(key_split)
  17. case key_split[0]
  18. when 'session'
  19. key_split.shift
  20. obj = user
  21. when attributes.class.to_s.downcase
  22. key_split.shift
  23. obj = attributes
  24. else
  25. obj = attributes
  26. end
  27. obj
  28. end
  29. def condition_key_value(key)
  30. return Array(key) if key == 'custom.module'
  31. key_split = key.split('.')
  32. obj = condition_key_value_object(key_split)
  33. key_split.each do |attribute|
  34. if obj.instance_of?(User) && attribute =~ %r{^group_ids_(full|create|change|read|overview)$}
  35. obj = obj.group_ids_access($1)
  36. break
  37. end
  38. obj = obj.try(attribute.to_sym)
  39. break if obj.blank?
  40. end
  41. condition_value_result(obj)
  42. end
  43. def condition_value_result(obj)
  44. Array(obj).map(&:to_s).map(&:html2text)
  45. end
  46. def condition_value_match?(key, condition, value)
  47. "CoreWorkflow::Condition::#{condition['operator'].tr(' ', '_').camelize}".constantize&.new(condition_object: self, key: key, condition: condition, value: value)&.match
  48. end
  49. def condition_match?(key, condition)
  50. value_key = condition_key_value(key)
  51. condition_value_match?(key, condition, value_key)
  52. end
  53. def condition_attributes_match?(check)
  54. @check = check
  55. condition = @workflow.send(:"condition_#{@check}")
  56. return true if condition.blank?
  57. result = true
  58. condition.each do |key, value|
  59. next if condition_match?(key, value)
  60. result = false
  61. break
  62. end
  63. result
  64. end
  65. def object_match?
  66. return true if @workflow.object.blank?
  67. @workflow.object.include?(@payload['class_name'])
  68. end
  69. def screen_match?
  70. return true if @workflow.preferences['screen'].blank?
  71. Array(@workflow.preferences['screen']).include?(@payload['screen'])
  72. end
  73. def match_all?
  74. return if !object_match?
  75. return if !screen_match?
  76. return if !condition_attributes_match?('saved')
  77. return if !condition_attributes_match?('selected')
  78. true
  79. end
  80. end