Fixes #3942 - New online notifications blocks data privacy user deletion.

This commit is contained in:
Rolf Schmidt 2022-02-04 16:43:28 +01:00
parent f44a1a2bd4
commit 7703c3efc6
2 changed files with 24 additions and 12 deletions

View file

@ -25,19 +25,38 @@ class DataPrivacyTask < ApplicationModel
handle_exception(e)
end
# 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
def perform_deletable
return if deletable.blank?
return if !deletable_type.constantize.exists?(id: deletable_id)
prepare_deletion_preview
save!
if delete_organization?
deletable.organization.destroy(associations: true)
perform_organization
else
deletable.destroy
perform_user
end
end
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
def handle_exception(e)
Rails.logger.error e
preferences[:error] = "ERROR: #{e.inspect}"

View file

@ -36,18 +36,11 @@ RSpec.describe DataPrivacyTask, type: :model do
expect(create(:data_privacy_task, deletable: admin)).to be_truthy
end
it 'sets the failed state when task failed' do
it 'sets no error message when user is already deleted' do
task = create(:data_privacy_task, deletable: user)
user.destroy
task.perform
expect(task.reload.state).to eq('failed')
end
it 'sets an error message when task failed' do
task = create(:data_privacy_task, deletable: user)
user.destroy
task.perform
expect(task.reload.preferences[:error]).to eq("ERROR: #<ActiveRecord::RecordNotFound: Couldn't find User with 'id'=#{user.id}>")
expect(task.reload.state).to eq('completed')
end
end