has_security_options.rb 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module FormUpdater::Concerns::HasSecurityOptions
  3. extend ActiveSupport::Concern
  4. def resolve
  5. if email_channel? && agent?
  6. if smime_active? || pgp_active?
  7. result_initialize_field('security')
  8. end
  9. fetch_security_options(SecureMailing::SMIME::SecurityOptions, 'SMIME') if smime_active?
  10. fetch_security_options(SecureMailing::PGP::SecurityOptions, 'PGP') if pgp_active?
  11. end
  12. super
  13. end
  14. private
  15. def fetch_security_options(klass, security_type)
  16. security_result = klass.new(ticket: data, article: data['article'] || {}).process
  17. return if !result
  18. map_result(result['security'], security_type, security_result.signing, 'sign')
  19. map_result(result['security'], security_type, security_result.encryption, 'encryption')
  20. end
  21. def map_result(target, type, result_method, mapped_type)
  22. push_to_sub_array(target, [:securityAllowed, type], mapped_type, result_method.possible?)
  23. push_to_sub_array(target, [:securityDefaultOptions, type], mapped_type, result_method.active_by_default?)
  24. initialize_value(target, type, result_method, mapped_type)
  25. set_sub_hash(target, [:securityMessages, type, mapped_type], map_message(result_method), result_method.message.present?)
  26. end
  27. def initialize_value(target, type, result_method, mapped_type)
  28. target[:value] ||= {}
  29. return if target[:value]['method'] && target[:value]['method'] != type
  30. target[:value]['method'] ||= type
  31. push_to_sub_array(target, [:value, 'options'], mapped_type, result_method.active_by_default?)
  32. end
  33. def map_message(result_method)
  34. { message: result_method.message, messagePlaceholder: result_method.message_placeholders }
  35. end
  36. def push_to_sub_array(hash, keys, value, condition)
  37. keys[0..-2].each do |key|
  38. hash[key] ||= {}
  39. hash = hash[key]
  40. end
  41. hash[keys.last] ||= []
  42. hash[keys.last] << value if condition
  43. end
  44. def set_sub_hash(hash, keys, value, condition)
  45. keys[0..-2].each do |key|
  46. hash[key] ||= {}
  47. hash = hash[key]
  48. end
  49. hash[keys.last] ||= {}
  50. hash[keys.last] = value if condition
  51. end
  52. def pgp_active?
  53. Setting.get('pgp_integration')
  54. end
  55. def smime_active?
  56. Setting.get('smime_integration')
  57. end
  58. def email_channel?
  59. data['articleSenderType'] == 'email-out' || data.dig('article', 'articleType') == 'email'
  60. end
  61. def agent?
  62. current_user.permissions?('ticket.agent')
  63. end
  64. end