OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Apply a couple of refactorings.
Pavel Balashou89fac9d474a5
diff --git a/app/contracts/groups/delete_contract.rb b/app/contracts/groups/delete_contract.rbindex 028a2656f06..536f3c28cf1 100644--- a/app/contracts/groups/delete_contract.rb+++ b/app/contracts/groups/delete_contract.rb@@ -28,6 +28,6 @@module Groupsclass DeleteContract < ::DeleteContract- delete_permission -> { user.active? && user.admin? }+ delete_permission(:admin)endenddiff --git a/app/contracts/roles/delete_contract.rb b/app/contracts/roles/delete_contract.rbnew file mode 100644index 00000000000..d0a28ba3733--- /dev/null+++ b/app/contracts/roles/delete_contract.rb@@ -0,0 +1,33 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 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.+#++++module Roles+ class DeleteContract < ::DeleteContract+ delete_permission(:admin)+ end+enddiff --git a/app/controllers/roles_controller.rb b/app/controllers/roles_controller.rbindex 708f06bd4ae..331b8e3c212 100644--- a/app/controllers/roles_controller.rb+++ b/app/controllers/roles_controller.rb@@ -79,20 +79,16 @@ class RolesController < ApplicationControllerenddef destroy- @role = Role.find(params[:id])- # after destroy permissions can not be reached- permissions = @role.permissions- @role.destroy-- flash[:notice] = I18n.t(:notice_successful_delete)- redirect_to action: 'index'+ service_result = Roles::DeleteService.new(+ model: Role.find(params[:id]),+ user: current_user+ ).call- OpenProject::Notifications.send(- OpenProject::Events::ROLE_DESTROYED,- permissions:- )- rescue StandardError- flash[:error] = I18n.t(:error_can_not_remove_role)+ if service_result.success?+ flash[:notice] = I18n.t(:notice_successful_delete)+ else+ flash[:error] = I18n.t(:error_can_not_remove_role)+ endredirect_to action: 'index'enddiff --git a/app/models/role.rb b/app/models/role.rbindex a599c0eeec2..4384396f6cc 100644--- a/app/models/role.rb+++ b/app/models/role.rb@@ -39,7 +39,13 @@ class Role < ApplicationRecordwhere("#{compare} builtin = #{NON_BUILTIN}")}- before_destroy :check_deletable+ before_destroy(prepend: true) do+ unless deletable?+ errors.add(:base, "can't be destroyed")+ throw :abort+ end+ end+has_many :workflows, dependent: :delete_all dodef copy_from_role(source_role)Workflow.copy(nil, source_role, nil, proxy_association.owner)@@ -74,7 +80,7 @@ class Role < ApplicationRecordenddef permissions=(perms)- not_included_yet = (perms.map(&:to_sym) - permissions).reject(&:blank?)+ not_included_yet = (perms.map(&:to_sym) - permissions).compact_blankincluded_until_now = permissions - perms.map(&:to_sym)remove_permission!(*included_until_now)@@ -176,6 +182,10 @@ class Role < ApplicationRecord.firstend+ def deletable?+ members.none? && !builtin?+ end+privatedef allowed_permissions@@ -188,11 +198,6 @@ class Role < ApplicationRecordendend- def check_deletable- raise "Can't delete role" if members.any?- raise "Can't delete builtin role" if builtin?- end-def add_permission(permission)if persisted?role_permissions.create(permission:)diff --git a/app/services/roles/create_service.rb b/app/services/roles/create_service.rbindex 20044048d73..e4a3fb29c51 100644--- a/app/services/roles/create_service.rb+++ b/app/services/roles/create_service.rb@@ -41,16 +41,6 @@ class Roles::CreateService < BaseServices::Createsuper_callend- def after_perform(call)- role = call.result- OpenProject::Notifications.send(- OpenProject::Events::ROLE_CREATED,- permissions: role.permissions- )-- call- end-def instance(params)if params.delete(:global_role)GlobalRole.newdiff --git a/app/services/roles/delete_service.rb b/app/services/roles/delete_service.rbnew file mode 100644index 00000000000..12bbef8dfc8--- /dev/null+++ b/app/services/roles/delete_service.rb@@ -0,0 +1,46 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 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.+#++++class Roles::DeleteService < BaseServices::Delete+ def persist(service_result)+ # after destroy permissions can not be reached+ @permissions = model.permissions+ super(service_result)+ end++ protected++ def after_perform(service_call)+ super(service_call).tap do |_call|+ ::OpenProject::Notifications.send(+ ::OpenProject::Events::ROLE_DESTROYED,+ permissions: @permissions+ )+ end+ end+enddiff --git a/config/initializers/subscribe_listeners.rb b/config/initializers/subscribe_listeners.rbindex 9f972489475..0a5b68859b8 100644--- a/config/initializers/subscribe_listeners.rb+++ b/config/initializers/subscribe_listeners.rb@@ -51,7 +51,7 @@ Rails.application.config.after_initialize dopayload[:watcher_setter])end- OpenProject::Notifications.subscribe(OpenProject::Events::WATCHER_REMOVED) do |payload|+ OpenProject::Notifications.subscribe(OpenProject::Events::WATCHER_DESTROYED) do |payload|Mails::WatcherRemovedJob.perform_later(payload[:watcher].attributes,payload[:watcher_remover])diff --git a/lib/open_project/events.rb b/lib/open_project/events.rbindex ef552448c50..a93f29f678a 100644--- a/lib/open_project/events.rb+++ b/lib/open_project/events.rb@@ -48,8 +48,7 @@ module OpenProjectMEMBER_CREATED = 'member_created'.freezeMEMBER_UPDATED = 'member_updated'.freeze- # Called like this for historic reasons, should be called 'member_destroyed'- MEMBER_DESTROYED = 'member_removed'.freeze+ MEMBER_DESTROYED = 'member_destroyed'.freezeOAUTH_CLIENT_TOKEN_CREATED = 'oauth_client_token_created'.freeze@@ -61,15 +60,14 @@ module OpenProjectPROJECT_UPDATED = "project_updated".freezePROJECT_RENAMED = "project_renamed".freeze- PROJECTS_STORAGE_CREATED = "projects_storage_created".freeze- PROJECTS_STORAGE_UPDATED = "projects_storage_updated".freeze- PROJECTS_STORAGE_DESTROYED = "projects_storage_destroyed".freeze+ PROJECT_STORAGE_CREATED = "project_storage_created".freeze+ PROJECT_STORAGE_UPDATED = "project_storage_updated".freeze+ PROJECT_STORAGE_DESTROYED = "project_storage_destroyed".freeze- ROLE_CREATED = "role_created".freezeROLE_UPDATED = "role_updated".freezeROLE_DESTROYED = "role_destroyed".freezeWATCHER_ADDED = 'watcher_added'.freeze- WATCHER_REMOVED = 'watcher_removed'.freeze+ WATCHER_DESTROYED = 'watcher_destroyed'.freezeendenddiff --git a/lib/services/remove_watcher.rb b/lib/services/remove_watcher.rbindex 92bb78113d1..f0dad9d43c5 100644--- a/lib/services/remove_watcher.rb+++ b/lib/services/remove_watcher.rb@@ -33,12 +33,12 @@ class Services::RemoveWatcherenddef run(success: -> {}, failure: -> {})- watcher = @work_package.watchers.find_by_user_id(@user.id)+ watcher = @work_package.watchers.find_by(user_id: @user.id)if watcher.present?@work_package.watcher_users.delete(@user)success.call- OpenProject::Notifications.send(OpenProject::Events::WATCHER_REMOVED,+ OpenProject::Notifications.send(OpenProject::Events::WATCHER_DESTROYED,watcher:,watcher_remover: User.current)elsediff --git a/modules/storages/app/services/storages/project_storages/create_service.rb b/modules/storages/app/services/storages/project_storages/create_service.rbindex 78caa92e285..52ccd224eac 100644--- a/modules/storages/app/services/storages/project_storages/create_service.rb+++ b/modules/storages/app/services/storages/project_storages/create_service.rb@@ -38,7 +38,7 @@ module Storages::ProjectStoragesproject_folder_mode = project_storage.project_folder_mode.to_symadd_historical_data(service_call) if project_folder_mode != :inactiveOpenProject::Notifications.send(- OpenProject::Events::PROJECTS_STORAGE_CREATED,+ OpenProject::Events::PROJECT_STORAGE_CREATED,project_folder_mode:)diff --git a/modules/storages/app/services/storages/project_storages/delete_service.rb b/modules/storages/app/services/storages/project_storages/delete_service.rbindex 653114284b0..b0d64fc933f 100644--- a/modules/storages/app/services/storages/project_storages/delete_service.rb+++ b/modules/storages/app/services/storages/project_storages/delete_service.rb@@ -47,7 +47,7 @@ module Storages::ProjectStoragesif deletion_result.success?delete_associated_file_linksOpenProject::Notifications.send(- OpenProject::Events::PROJECTS_STORAGE_DESTROYED,+ OpenProject::Events::PROJECT_STORAGE_DESTROYED,project_folder_mode: deletion_result.result.project_folder_mode.to_sym)enddiff --git a/modules/storages/app/services/storages/project_storages/update_service.rb b/modules/storages/app/services/storages/project_storages/update_service.rbindex b75e4d38b3f..98a455344b7 100644--- a/modules/storages/app/services/storages/project_storages/update_service.rb+++ b/modules/storages/app/services/storages/project_storages/update_service.rb@@ -39,7 +39,7 @@ module Storages::ProjectStoragesproject_folder_mode = project_storage.project_folder_mode.to_symadd_historical_data(service_call) if project_folder_mode != :inactiveOpenProject::Notifications.send(- OpenProject::Events::PROJECTS_STORAGE_UPDATED,+ OpenProject::Events::PROJECT_STORAGE_UPDATED,project_folder_mode:)diff --git a/modules/storages/lib/open_project/storages/engine.rb b/modules/storages/lib/open_project/storages/engine.rbindex a84c2391a3f..acd34eb79a1 100644--- a/modules/storages/lib/open_project/storages/engine.rb+++ b/modules/storages/lib/open_project/storages/engine.rb@@ -32,13 +32,9 @@# gets loaded.module OpenProject::Storagesclass Engine < ::Rails::Engine- PERMISSIONS = %i[- read_files- write_files- create_files- delete_files- share_files- ].freeze+ def self.permissions+ @permissions ||= Storages::GroupFolderPropertiesSyncService::PERMISSIONS_MAP.keys+ end# engine name is used as a default prefix for module tables when generating# tables with the rails command.# It may also be used in other places, please investigate.@@ -73,32 +69,25 @@ module OpenProject::Storages::Storages::ManageNextcloudIntegrationEventsJob.debounceendend- OpenProject::Notifications.subscribe(- OpenProject::Events::ROLE_CREATED- ) do |payload|- if payload[:permissions].intersect?(PERMISSIONS)- ::Storages::ManageNextcloudIntegrationEventsJob.debounce- end- endOpenProject::Notifications.subscribe(OpenProject::Events::ROLE_UPDATED) do |payload|- if payload[:permissions_diff].intersect?(PERMISSIONS)+ if payload[:permissions_diff]&.intersect?(OpenProject::Storages::Engine.permissions)::Storages::ManageNextcloudIntegrationEventsJob.debounceendendOpenProject::Notifications.subscribe(OpenProject::Events::ROLE_DESTROYED) do |payload|- if payload[:permissions].intersect?(PERMISSIONS)+ if payload[:permissions]&.intersect?(OpenProject::Storages::Engine.permissions)::Storages::ManageNextcloudIntegrationEventsJob.debounceendend[- OpenProject::Events::PROJECTS_STORAGE_CREATED,- OpenProject::Events::PROJECTS_STORAGE_UPDATED,- OpenProject::Events::PROJECTS_STORAGE_DESTROYED+ OpenProject::Events::PROJECT_STORAGE_CREATED,+ OpenProject::Events::PROJECT_STORAGE_UPDATED,+ OpenProject::Events::PROJECT_STORAGE_DESTROYED].each do |event|OpenProject::Notifications.subscribe(event) do |payload|if payload[:project_folder_mode] == :automatic@@ -133,7 +122,7 @@ module OpenProject::Storages{ 'storages/admin/projects_storages': %i[index new edit update create destroy destroy_info set_permissions] },dependencies: %i[]- PERMISSIONS.each do |p|+ OpenProject::Storages::Engine.permissions.each do |p|permission(p, {}, dependencies: %i[])endenddiff --git a/spec/controllers/roles_controller_spec.rb b/spec/controllers/roles_controller_spec.rbindex 387e5d6a9e4..96592a8f719 100644--- a/spec/controllers/roles_controller_spec.rb+++ b/spec/controllers/roles_controller_spec.rb@@ -361,7 +361,7 @@ RSpec.describe RolesController doit 'has the service call assigned' doexpect(assigns[:calls])- .to match_array [service_call0, service_call1, service_call2, service_call3]+ .to contain_exactly(service_call0, service_call1, service_call2, service_call3)endit 'has the roles assigned' do@@ -371,55 +371,41 @@ RSpec.describe RolesController doendend- describe '#destroys' do- let(:role) do- build_stubbed(:role).tap do |d|- allow(Role)- .to receive(:find)- .with(d.id.to_s)- .and_return(d)-- allow(d)- .to receive(:destroy)- end- end+ describe '#destroy' do+ let(:role) { create(:role) }let(:params) { { id: role.id } }- subject do- delete :destroy, params:- end+ subject { delete(:destroy, params:) }context 'with the role not in use' do- before { subject }+ it 'redirects and destroyes the role' do+ allow_any_instance_of(Role).to receive(:permissions).and_return([:read_files])+ role+ expect(Role.count).to eq(1)+ expect(enqueued_jobs.count).to eq(0)- it 'redirects' do- expect(response)- .to redirect_to roles_path- end+ subject- it 'destroys the role' do- expect(role)- .to have_received(:destroy)+ expect(enqueued_jobs.count).to eq(1)+ expect(enqueued_jobs[0][:job]).to eq(Storages::ManageNextcloudIntegrationEventsJob)+ expect(response).to redirect_to roles_path+ expect(Role.count).to eq(0)endendcontext 'with the role in use' do- before do- allow(role)- .to receive(:destroy)- .and_throw(:an_error)+ it 'redirects with a flash error' do+ allow_any_instance_of(Role).to receive(:deletable?).and_return(false)+ role+ expect(Role.count).to eq(1)+ expect(enqueued_jobs.count).to eq(0)subject- end-- it 'redirects' do- expect(response)- .to redirect_to roles_path- end- it 'sets a flash error' do- expect(flash[:error])- .to eq I18n.t(:error_can_not_remove_role)+ expect(enqueued_jobs.count).to eq(0)+ expect(Role.count).to eq(1)+ expect(response).to redirect_to roles_path+ expect(flash[:error]).to eq I18n.t(:error_can_not_remove_role)endendenddiff --git a/spec/lib/open_project/events_spec.rb b/spec/lib/open_project/events_spec.rbnew file mode 100644index 00000000000..568e46e076c--- /dev/null+++ b/spec/lib/open_project/events_spec.rb@@ -0,0 +1,168 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 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'++RSpec.describe OpenProject::Events do+ def subject_constructor(event)+ OpenProject::Notifications.send(+ "#{described_class}::#{event}".constantize,+ payload+ )+ end++ %w[+ PROJECT_STORAGE_CREATED+ PROJECT_STORAGE_UPDATED+ PROJECT_STORAGE_DESTROYED+ ].each do |event|+ describe(event) do+ subject { subject_constructor(event) }++ context 'when payload is empty' do+ let(:payload) { {} }++ it do+ expect { subject }.not_to change(enqueued_jobs, :count)+ end+ end++ context 'when payload contains automatic project_folder_mpde' do+ let(:payload) { { project_folder_mode: :automatic } }++ it do+ expect { subject }.to change(enqueued_jobs, :count).from(0).to(1)+ end++ it do+ subject+ expect(enqueued_jobs[0][:job]).to eq(Storages::ManageNextcloudIntegrationEventsJob)+ end+ end+ end+ end++ %w[+ MEMBER_CREATED+ MEMBER_UPDATED+ MEMBER_DESTROYED+ PROJECT_CREATED+ PROJECT_UPDATED+ PROJECT_RENAMED+ ].each do |event|+ describe(event) do+ subject { subject_constructor(event) }++ let(:payload) { {} }++ it do+ expect { subject }.to change(enqueued_jobs, :count).from(0).to(1)+ end++ it do+ subject+ expect(enqueued_jobs[0][:job]).to eq(Storages::ManageNextcloudIntegrationEventsJob)+ end+ end+ end++ describe 'OAUTH_CLIENT_TOKEN_CREATED' do+ subject { subject_constructor('OAUTH_CLIENT_TOKEN_CREATED') }++ context 'when payload is empty' do+ let(:payload) { {} }++ it do+ expect { subject }.not_to change(enqueued_jobs, :count)+ end+ end++ context 'when payload contains storage integration type' do+ let(:payload) { { integration_type: 'Storages::Storage' } }++ it do+ expect { subject }.to change(enqueued_jobs, :count).from(0).to(1)+ end++ it do+ subject+ expect(enqueued_jobs[0][:job]).to eq(Storages::ManageNextcloudIntegrationEventsJob)+ end+ end+ end++ describe 'ROLE_UPDATED' do+ subject { subject_constructor('ROLE_UPDATED') }++ context 'when payload is empty' do+ let(:payload) { {} }++ it do+ expect { subject }.not_to change(enqueued_jobs, :count)+ end+ end++ context 'when payload contains some nextcloud related permissions as a diff' do+ let(:payload) { { permissions_diff: [:read_files] } }++ it do+ expect { subject }.to change(enqueued_jobs, :count).from(0).to(1)+ end++ it do+ subject+ expect(enqueued_jobs[0][:job]).to eq(Storages::ManageNextcloudIntegrationEventsJob)+ end+ end+ end++ describe 'ROLE_DESTROYED' do+ subject { subject_constructor('ROLE_DESTROYED') }++ context 'when payload is empty' do+ let(:payload) { {} }++ it do+ expect { subject }.not_to change(enqueued_jobs, :count)+ end+ end++ context 'when payload contains some nextcloud related permissions' do+ let(:payload) { { permissions: [:read_files] } }++ it do+ expect { subject }.to change(enqueued_jobs, :count).from(0).to(1)+ end++ it do+ subject+ expect(enqueued_jobs[0][:job]).to eq(Storages::ManageNextcloudIntegrationEventsJob)+ end+ end+ end+end