diff --git a/modules/storages/config/routes.rb b/modules/storages/config/routes.rb index ccd604f102..0617772f3c 100644 --- a/modules/storages/config/routes.rb +++ b/modules/storages/config/routes.rb @@ -1,3 +1,31 @@ +#-- copyright +# OpenProject is an open source project management software. +# Copyright (C) 2012-2022 the OpenProject GmbH +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License version 3. +# +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: +# Copyright (C) 2006-2013 Jean-Philippe Lang +# Copyright (C) 2010-2013 the ChiliProject Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# See COPYRIGHT and LICENSE files for more details. +#++ + OpenProject::Application.routes.draw do scope 'admin/settings' do resources :storages, controller: 'storages/admin/storages' diff --git a/modules/storages/lib/open_project/storages/engine.rb b/modules/storages/lib/open_project/storages/engine.rb index 9a3c5e7e54..18d15356ff 100644 --- a/modules/storages/lib/open_project/storages/engine.rb +++ b/modules/storages/lib/open_project/storages/engine.rb @@ -79,6 +79,8 @@ module OpenProject::Storages parent: :settings end + patch_with_namespace :Principals, :ReplaceReferencesService + # This hook is executed when the module is loaded. config.to_prepare do # We have a bunch of filters defined within the module. Here we register the filters. diff --git a/modules/storages/lib/open_project/storages/patches/replace_references_service_patch.rb b/modules/storages/lib/open_project/storages/patches/replace_references_service_patch.rb new file mode 100644 index 0000000000..2f8e8238d2 --- /dev/null +++ b/modules/storages/lib/open_project/storages/patches/replace_references_service_patch.rb @@ -0,0 +1,22 @@ +module OpenProject::Storages::Patches::ReplaceReferencesServicePatch + def self.included(base) # :nodoc: + base.prepend InstanceMethods + end + + module InstanceMethods + private + + def rewrite_active_models(from, to) + super + rewrite_creator(from, to) + end + + def rewrite_creator(from, to) + [::Storages::Storage, + ::Storages::ProjectStorage, + ::Storages::FileLink].each do |klass| + klass.where(creator_id: from.id).update_all(creator_id: to.id) + end + end + end +end diff --git a/modules/storages/spec/services/principals/replace_references_service_call_integration_spec.rb b/modules/storages/spec/services/principals/replace_references_service_call_integration_spec.rb new file mode 100644 index 0000000000..bb374ac5c7 --- /dev/null +++ b/modules/storages/spec/services/principals/replace_references_service_call_integration_spec.rb @@ -0,0 +1,77 @@ +#-- copyright +# OpenProject is an open source project management software. +# Copyright (C) 2012-2022 the OpenProject GmbH +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License version 3. +# +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: +# Copyright (C) 2006-2013 Jean-Philippe Lang +# Copyright (C) 2010-2013 the ChiliProject Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# See COPYRIGHT and LICENSE files for more details. +#++ + +require 'spec_helper' + +describe Principals::ReplaceReferencesService, '#call', type: :model do + shared_let(:principal) { create(:user) } + shared_let(:to_principal) { create :user } + + subject(:service_call) { instance.call(from: principal, to: to_principal) } + + let(:instance) do + described_class.new + end + + shared_examples 'replaces the creator' do + before do + model + end + + it 'is successful' do + expect(service_call) + .to be_success + end + + it 'replaces principal with to_principal' do + service_call + model.reload + + expect(model.creator).to eql to_principal + end + end + + context 'with Storage' do + it_behaves_like 'replaces the creator' do + let(:model) { create(:storage, creator: principal) } + end + end + + context 'with ProjectStorage' do + it_behaves_like 'replaces the creator' do + let(:model) { create(:project_storage, creator: principal) } + end + end + + context 'with FileLink' do + it_behaves_like 'replaces the creator' do + let(:work_package) { create(:work_package) } + let(:model) { create(:file_link, creator: principal, container: work_package) } + end + end +end