handler_security_options.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class SecureMailing::Backend::HandlerSecurityOptions < SecureMailing::Backend::Handler
  3. SECURITY_OPTIONS_METHOD_STATUS = Struct.new(
  4. :possible?,
  5. :active_by_default?,
  6. :message,
  7. :message_placeholders,
  8. keyword_init: true
  9. )
  10. SECURITY_OPTIONS_RESULT = Struct.new(
  11. :type,
  12. :encryption,
  13. :signing,
  14. keyword_init: true
  15. )
  16. attr_reader :ticket, :article
  17. def initialize(ticket:, article:)
  18. super()
  19. @ticket = ticket
  20. @article = article
  21. end
  22. def process
  23. SECURITY_OPTIONS_RESULT.new(
  24. type: type,
  25. signing: check_signing,
  26. encryption: check_encryption,
  27. )
  28. end
  29. private
  30. def sign_security_options_status_default_message
  31. raise NotImplementedError
  32. end
  33. def encryption_security_options_status_default_message
  34. __('There was no recipient found.')
  35. end
  36. def check_signing
  37. result = SECURITY_OPTIONS_METHOD_STATUS.new(
  38. message: sign_security_options_status_default_message,
  39. message_placeholders: [],
  40. )
  41. result[:possible?] = can_sign?(result)
  42. result[:active_by_default?] = signing_default?(result)
  43. result
  44. end
  45. def check_encryption
  46. result = SECURITY_OPTIONS_METHOD_STATUS.new(
  47. message: encryption_security_options_status_default_message,
  48. message_placeholders: [],
  49. )
  50. result[:possible?] = can_encrypt?(result)
  51. result[:active_by_default?] = encryption_default?(result)
  52. result
  53. end
  54. def config
  55. raise NotImplementedError
  56. end
  57. def group_has_valid_secure_objects?
  58. raise NotImplementedError
  59. end
  60. def recipients_have_valid_secure_objects?
  61. raise NotImplementedError
  62. end
  63. def signing_default?(signing_result)
  64. return false if !signing_result.possible?
  65. return true if !config.dig('group_id', 'default_sign') || !ticket['group_id']
  66. config['group_id']['default_sign'][ticket['group_id'].to_s]
  67. end
  68. def can_sign?(signing_result)
  69. return false if !ticket['group_id']
  70. group = Group.find_by(id: ticket['group_id'])
  71. return false if !group
  72. group_email = group.email_address&.email
  73. return false if group_email.blank?
  74. group_has_valid_secure_objects?(signing_result, group_email)
  75. end
  76. def encryption_default?(encryption_result)
  77. return false if !encryption_result.possible?
  78. return true if !config.dig('group_id', 'default_encryption') || !ticket['group_id']
  79. config['group_id']['default_encryption'][ticket['group_id'].to_s]
  80. end
  81. def can_encrypt?(encryption_result)
  82. return false if !ticket['customer_id'] && !ticket['cc'] && !article['to'] && !article['cc']
  83. recipients = verified_recipient_addresses
  84. return false if recipients.blank?
  85. recipients_have_valid_secure_objects?(encryption_result, recipients)
  86. end
  87. def verified_recipient_addresses
  88. list = Mail::AddressList.new(recipient_addresses.compact.join(','))
  89. list.addresses.map(&:address).uniq
  90. end
  91. def recipient_addresses
  92. customer_recipient + target_recipients + additional_recipients
  93. end
  94. def customer_recipient
  95. return [] if ticket['customer_id'].nil?
  96. customer = ::User.find_by(id: ticket['customer_id'])
  97. return [] if !customer || customer.email.empty?
  98. [customer.email]
  99. end
  100. def target_recipients
  101. [article['to']].compact
  102. end
  103. def additional_recipients
  104. [ticket['cc'].presence, article['cc'].presence].compact
  105. end
  106. def from(group_email)
  107. list = Mail::AddressList.new(group_email)
  108. list.addresses.first.to_s
  109. end
  110. end