has_security_options.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. module FormUpdater::Concerns::HasSecurityOptions
  3. extend ActiveSupport::Concern
  4. def resolve
  5. if smime_active? && email_channel? && agent?
  6. result_initialize_field('security')
  7. result['security'][:allowed] = smime_allowed_values
  8. result['security'][:value] = smime_default_value
  9. end
  10. super
  11. end
  12. private
  13. def smime_active?
  14. Setting.get('smime_integration')
  15. end
  16. def email_channel?
  17. data['articleSenderType'] == 'email-out'
  18. end
  19. def agent?
  20. current_user.permissions?(['ticket.agent'])
  21. end
  22. def smime_config
  23. Setting.get('smime_config')
  24. end
  25. def smime_allowed_values
  26. result = []
  27. result.push('encryption') if smime_encryption?
  28. result.push('sign') if smime_sign?
  29. result
  30. end
  31. def smime_default_value
  32. result = smime_allowed_values # fallback
  33. return result if !smime_config['group_id'] || !data['group_id']
  34. filter_smime_config_default_values(result)
  35. end
  36. def filter_smime_config_default_values(result)
  37. { 'default_sign' => 'sign', 'default_encryption' => 'encryption' }.each do |type, selector|
  38. next if !smime_config['group_id'][type]
  39. next if smime_config['group_id'][type][data['group_id'].to_s]
  40. result.delete(selector)
  41. end
  42. result
  43. end
  44. def smime_encryption?
  45. return false if !data['customer_id'] && !data['cc']
  46. recipients = verified_recipient_addresses
  47. return false if recipients.blank?
  48. recipients_have_valid_certificate?(recipients)
  49. end
  50. def recipient_addresses
  51. result = []
  52. if data['customer_id'].present?
  53. customer = ::User.find_by(id: data['customer_id'])
  54. if customer && customer.email.present?
  55. result.push(customer.email)
  56. end
  57. end
  58. if data['cc'].present?
  59. result.push(data['cc'])
  60. end
  61. result
  62. end
  63. def verified_recipient_addresses
  64. result = []
  65. list = Mail::AddressList.new(recipient_addresses.compact.join(',').to_s)
  66. list.addresses.each do |address|
  67. result.push address.address
  68. end
  69. result
  70. end
  71. def recipients_have_valid_certificate?(recipients)
  72. result = false
  73. begin
  74. certs = SMIMECertificate.for_recipipent_email_addresses!(recipients)
  75. if certs
  76. result = certs.none?(&:expired?)
  77. end
  78. rescue
  79. result = false
  80. end
  81. result
  82. end
  83. def smime_sign?
  84. return false if !data['group_id']
  85. group = Group.find_by(id: data['group_id'])
  86. return false if !group
  87. group_has_valid_certificate?(group)
  88. end
  89. def group_has_valid_certificate?(group)
  90. result = false
  91. begin
  92. list = Mail::AddressList.new(group.email_address.email)
  93. from = list.addresses.first.to_s
  94. cert = SMIMECertificate.for_sender_email_address(from)
  95. if cert
  96. result = !cert.expired?
  97. end
  98. rescue
  99. result = false
  100. end
  101. result
  102. end
  103. end