2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2020-09-08 15:06:23 +00:00
|
|
|
|
|
|
|
class DataPrivacyTask < ApplicationModel
|
|
|
|
include DataPrivacyTask::HasActivityStreamLog
|
|
|
|
include ChecksClientNotification
|
|
|
|
|
|
|
|
store :preferences
|
|
|
|
|
|
|
|
belongs_to :created_by, class_name: 'User'
|
|
|
|
belongs_to :updated_by, class_name: 'User'
|
|
|
|
|
|
|
|
# optional because related data will get deleted and it would
|
|
|
|
# cause validation errors if e.g. the created_by_id of the task
|
|
|
|
# would need to get mapped by a deletion
|
|
|
|
belongs_to :deletable, polymorphic: true, optional: true
|
|
|
|
|
|
|
|
before_create :prepare_deletion_preview
|
|
|
|
|
|
|
|
validates_with DataPrivacyTask::Validation
|
|
|
|
|
|
|
|
def perform
|
2021-06-02 11:37:57 +00:00
|
|
|
perform_deletable
|
|
|
|
update!(state: 'completed')
|
|
|
|
rescue => e
|
|
|
|
handle_exception(e)
|
|
|
|
end
|
|
|
|
|
2022-02-04 15:43:28 +00:00
|
|
|
# set user inactive before destroy to prevent
|
|
|
|
# new online notifications or other events while
|
|
|
|
# the deletion process is running
|
|
|
|
# https://github.com/zammad/zammad/issues/3942
|
|
|
|
def update_inactive(object)
|
|
|
|
object.update(active: false)
|
|
|
|
end
|
|
|
|
|
2021-06-02 11:37:57 +00:00
|
|
|
def perform_deletable
|
2022-02-04 15:43:28 +00:00
|
|
|
return if !deletable_type.constantize.exists?(id: deletable_id)
|
2020-09-08 15:06:23 +00:00
|
|
|
|
|
|
|
prepare_deletion_preview
|
|
|
|
save!
|
|
|
|
|
|
|
|
if delete_organization?
|
2022-02-04 15:43:28 +00:00
|
|
|
perform_organization
|
2020-09-08 15:06:23 +00:00
|
|
|
else
|
2022-02-04 15:43:28 +00:00
|
|
|
perform_user
|
2020-09-08 15:06:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-02-04 15:43:28 +00:00
|
|
|
def perform_organization
|
|
|
|
update_inactive(deletable.organization)
|
|
|
|
deletable.organization.members.find_each { |user| update_inactive(user) }
|
|
|
|
deletable.organization.destroy(associations: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform_user
|
|
|
|
update_inactive(deletable)
|
|
|
|
deletable.destroy
|
|
|
|
end
|
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
def handle_exception(e)
|
|
|
|
Rails.logger.error e
|
|
|
|
preferences[:error] = "ERROR: #{e.inspect}"
|
|
|
|
self.state = 'failed'
|
|
|
|
save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_organization?
|
|
|
|
return false if preferences[:delete_organization].blank?
|
|
|
|
return false if preferences[:delete_organization] != 'true'
|
|
|
|
return false if !deletable.organization
|
|
|
|
return false if deletable.organization.members.count != 1
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def prepare_deletion_preview
|
|
|
|
prepare_deletion_preview_tickets
|
|
|
|
prepare_deletion_preview_user
|
|
|
|
prepare_deletion_preview_organization
|
|
|
|
prepare_deletion_preview_anonymize
|
|
|
|
end
|
|
|
|
|
|
|
|
def prepare_deletion_preview_tickets
|
|
|
|
preferences[:owner_tickets] = deletable.owner_tickets.order(id: 'DESC').map(&:number)
|
|
|
|
preferences[:customer_tickets] = deletable.customer_tickets.order(id: 'DESC').map(&:number)
|
|
|
|
end
|
|
|
|
|
|
|
|
def prepare_deletion_preview_user
|
|
|
|
preferences[:user] = {
|
|
|
|
firstname: deletable.firstname,
|
|
|
|
lastname: deletable.lastname,
|
|
|
|
email: deletable.email,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def prepare_deletion_preview_organization
|
|
|
|
return if !deletable.organization
|
|
|
|
|
|
|
|
preferences[:user][:organization] = deletable.organization.name
|
|
|
|
end
|
|
|
|
|
|
|
|
def prepare_deletion_preview_anonymize
|
|
|
|
preferences[:user] = Pseudonymisation.of_hash(preferences[:user])
|
|
|
|
end
|
|
|
|
end
|