From ceac28ce4dd39c3698b34c27521bca39287053fb Mon Sep 17 00:00:00 2001 From: Rolf Schmidt Date: Fri, 4 Feb 2022 16:43:28 +0100 Subject: [PATCH] Fixes #3942 - New online notifications blocks data privacy user deletion. --- app/models/data_privacy_task.rb | 25 ++++++++++++++++++++++--- spec/models/data_privacy_task_spec.rb | 11 ++--------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/app/models/data_privacy_task.rb b/app/models/data_privacy_task.rb index d2dd09590..4c4644578 100644 --- a/app/models/data_privacy_task.rb +++ b/app/models/data_privacy_task.rb @@ -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}" diff --git a/spec/models/data_privacy_task_spec.rb b/spec/models/data_privacy_task_spec.rb index 848ef52d1..2c70d65e8 100644 --- a/spec/models/data_privacy_task_spec.rb +++ b/spec/models/data_privacy_task_spec.rb @@ -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: #") + expect(task.reload.state).to eq('completed') end end