email_address.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class EmailAddress < ApplicationModel
  3. include ChecksHtmlSanitized
  4. include HasCollectionUpdate
  5. has_many :groups, after_add: :cache_update, after_remove: :cache_update
  6. belongs_to :channel, optional: true
  7. validates :name, presence: true
  8. validates :email, presence: true, uniqueness: { case_sensitive: true }
  9. before_validation :check_email
  10. before_create :check_if_channel_exists_set_inactive
  11. after_create :update_email_address_id
  12. before_update :check_if_channel_exists_set_inactive
  13. after_update :update_email_address_id
  14. before_destroy :delete_group_reference
  15. validates :note, length: { maximum: 250 }
  16. sanitized_html :note
  17. collection_push_permission('ticket.agent')
  18. =begin
  19. check and if channel not exists reset configured channels for email addresses
  20. EmailAddress.channel_cleanup
  21. =end
  22. def self.channel_cleanup
  23. EmailAddress.all.each do |email_address|
  24. # set to active if channel exists
  25. if email_address.channel_id && Channel.exists?(email_address.channel_id)
  26. if !email_address.active
  27. email_address.save!
  28. end
  29. next
  30. end
  31. # set in inactive if channel not longer exists
  32. next if !email_address.active
  33. email_address.save!
  34. end
  35. end
  36. private
  37. def check_email
  38. return true if Setting.get('import_mode')
  39. return true if email.blank?
  40. self.email = email.downcase.strip
  41. email_address_validation = EmailAddressValidation.new(email)
  42. if !email_address_validation.valid?
  43. raise Exceptions::UnprocessableEntity, "Invalid email '#{email}'"
  44. end
  45. true
  46. end
  47. # set email address to inactive/active if channel exists or not
  48. def check_if_channel_exists_set_inactive
  49. # set to active if channel exists
  50. if channel_id && Channel.exists?(id: channel_id)
  51. self.active = true
  52. return true
  53. end
  54. # set in inactive if channel not longer exists
  55. self.channel_id = nil
  56. self.active = false
  57. true
  58. end
  59. # delete group.email_address_id reference if email address get's deleted
  60. def delete_group_reference
  61. Group.where(email_address_id: id).each do |group|
  62. group.update!(email_address_id: nil)
  63. end
  64. end
  65. # keep email email address is of initial group filled
  66. def update_email_address_id
  67. not_configured = Group.where(email_address_id: nil).count
  68. total = Group.count
  69. return if not_configured.zero?
  70. return if total != 1
  71. group = Group.find_by(email_address_id: nil)
  72. group.email_address_id = id
  73. group.updated_by_id = updated_by_id
  74. group.save!
  75. end
  76. end