2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-10-01 18:41:08 +00:00
|
|
|
class EmailAddress < ApplicationModel
|
2017-05-02 15:21:13 +00:00
|
|
|
include ChecksLatestChangeObserved
|
2020-02-20 13:34:03 +00:00
|
|
|
include HasCollectionUpdate
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2015-08-28 08:19:27 +00:00
|
|
|
has_many :groups, after_add: :cache_update, after_remove: :cache_update
|
2019-07-04 11:16:55 +00:00
|
|
|
belongs_to :channel, optional: true
|
2015-08-28 08:19:27 +00:00
|
|
|
validates :realname, presence: true
|
|
|
|
validates :email, presence: true
|
|
|
|
|
2017-08-22 12:55:36 +00:00
|
|
|
before_validation :check_email
|
2015-09-11 13:44:05 +00:00
|
|
|
before_create :check_if_channel_exists_set_inactive
|
|
|
|
before_update :check_if_channel_exists_set_inactive
|
2016-02-25 21:04:51 +00:00
|
|
|
after_create :update_email_address_id
|
|
|
|
after_update :update_email_address_id
|
2017-06-06 15:49:49 +00:00
|
|
|
before_destroy :delete_group_reference
|
2015-02-25 20:52:14 +00:00
|
|
|
|
2020-02-20 13:34:03 +00:00
|
|
|
collection_push_permission('ticket.agent')
|
|
|
|
|
2015-08-28 08:19:27 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
check and if channel not exists reset configured channels for email addresses
|
|
|
|
|
|
|
|
EmailAddress.channel_cleanup
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.channel_cleanup
|
2017-10-01 12:25:52 +00:00
|
|
|
EmailAddress.all.each do |email_address|
|
2015-09-11 13:44:05 +00:00
|
|
|
|
|
|
|
# set to active if channel exists
|
2015-08-28 14:50:40 +00:00
|
|
|
if email_address.channel_id && Channel.find_by(id: email_address.channel_id)
|
|
|
|
if !email_address.active
|
2017-05-02 12:11:59 +00:00
|
|
|
email_address.save!
|
2015-08-28 14:50:40 +00:00
|
|
|
end
|
|
|
|
next
|
|
|
|
end
|
2015-09-11 13:44:05 +00:00
|
|
|
|
|
|
|
# set in inactive if channel not longer exists
|
|
|
|
next if !email_address.active
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-05-02 12:11:59 +00:00
|
|
|
email_address.save!
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-08-28 08:19:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-08-22 12:55:36 +00:00
|
|
|
def check_email
|
|
|
|
return true if Setting.get('import_mode')
|
|
|
|
return true if email.blank?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-08-22 12:55:36 +00:00
|
|
|
self.email = email.downcase.strip
|
2019-11-21 15:49:14 +00:00
|
|
|
email_address_validation = EmailAddressValidation.new(email)
|
|
|
|
if !email_address_validation.valid_format?
|
2020-01-31 21:40:55 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, "Invalid email '#{email}'"
|
2019-11-21 15:49:14 +00:00
|
|
|
end
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-08-22 12:55:36 +00:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2015-09-11 13:44:05 +00:00
|
|
|
# set email address to inactive/active if channel exists or not
|
|
|
|
def check_if_channel_exists_set_inactive
|
|
|
|
|
|
|
|
# set to active if channel exists
|
2015-08-28 14:50:40 +00:00
|
|
|
if channel_id && Channel.find_by(id: channel_id)
|
|
|
|
self.active = true
|
|
|
|
return true
|
|
|
|
end
|
2015-09-11 13:44:05 +00:00
|
|
|
|
|
|
|
# set in inactive if channel not longer exists
|
2015-08-28 08:19:27 +00:00
|
|
|
self.channel_id = nil
|
2015-08-28 14:50:40 +00:00
|
|
|
self.active = false
|
|
|
|
true
|
2015-08-28 08:19:27 +00:00
|
|
|
end
|
|
|
|
|
2015-09-11 13:44:05 +00:00
|
|
|
# delete group.email_address_id reference if email address get's deleted
|
|
|
|
def delete_group_reference
|
2017-10-01 12:25:52 +00:00
|
|
|
Group.where(email_address_id: id).each do |group|
|
2017-09-11 11:16:08 +00:00
|
|
|
group.update!(email_address_id: nil)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-09-11 13:44:05 +00:00
|
|
|
end
|
2016-02-25 21:04:51 +00:00
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
# keep email email address is of initial group filled
|
2016-02-25 21:04:51 +00:00
|
|
|
def update_email_address_id
|
|
|
|
not_configured = Group.where(email_address_id: nil).count
|
|
|
|
total = Group.count
|
2016-07-26 22:02:28 +00:00
|
|
|
return if not_configured.zero?
|
2016-02-25 21:04:51 +00:00
|
|
|
return if total != 1
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-02-25 21:04:51 +00:00
|
|
|
group = Group.find_by(email_address_id: nil)
|
|
|
|
group.email_address_id = id
|
|
|
|
group.updated_by_id = updated_by_id
|
2017-05-02 12:11:59 +00:00
|
|
|
group.save!
|
2016-02-25 21:04:51 +00:00
|
|
|
end
|
|
|
|
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|