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
ensure compatible structure on ORing relations
ulfertsdf501c23785a
diff --git a/modules/costs/app/models/time_entries/scopes/ongoing.rb b/modules/costs/app/models/time_entries/scopes/ongoing.rbindex 5160f27aa2b..68c0f7a0241 100644--- a/modules/costs/app/models/time_entries/scopes/ongoing.rb+++ b/modules/costs/app/models/time_entries/scopes/ongoing.rb@@ -39,18 +39,12 @@ module TimeEntries::ScopesTimeEntry.where(entity_type: "WorkPackage",- entity_id: visible_work_packages(user).select(:id),+ entity_id: WorkPackage.allowed_to_log_time(user).select(:id),user:,ongoing: true)end- def visible_work_packages(user)- WorkPackage.allowed_to(user, :log_own_time).or(- WorkPackage.where(project_id: Project.allowed_to(User.current, :log_time))- )- end-def not_ongoingTimeEntry.where(ongoing: false)enddiff --git a/modules/costs/app/models/work_packages/scopes/allowed_to_log_time.rb b/modules/costs/app/models/work_packages/scopes/allowed_to_log_time.rbnew file mode 100644index 00000000000..7a958b4c729--- /dev/null+++ b/modules/costs/app/models/work_packages/scopes/allowed_to_log_time.rb@@ -0,0 +1,47 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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 WorkPackages::Scopes+ module AllowedToLogTime+ extend ActiveSupport::Concern++ class_methods do+ # Returns an ActiveRecord::Relation to find all work packages for which+ # +user+ has the permission to log_time or log_own_time+ def allowed_to_log_time(user)+ # This nesting of WorkPackage.where(id: WorkPackage) is made necessary to+ # have the two ORed relations structurally identical.+ WorkPackage.where(id: WorkPackage.allowed_to(user, :log_own_time)).or(+ WorkPackage.where(project_id: Project.allowed_to(user, :log_time))+ )+ end+ end+ end+enddiff --git a/modules/costs/lib/api/v3/time_entries/available_work_packages_on_create_api.rb b/modules/costs/lib/api/v3/time_entries/available_work_packages_on_create_api.rbindex d73b9fae287..d697908a3fb 100644--- a/modules/costs/lib/api/v3/time_entries/available_work_packages_on_create_api.rb+++ b/modules/costs/lib/api/v3/time_entries/available_work_packages_on_create_api.rb@@ -40,8 +40,7 @@ module APIhelpers dodef allowed_scope- WorkPackage.where(id: WorkPackage.allowed_to(User.current, :log_own_time))- .or(WorkPackage.where(project_id: Project.allowed_to(User.current, :log_time)))+ WorkPackage.allowed_to_log_time(User.current)endenddiff --git a/modules/costs/lib/costs/engine.rb b/modules/costs/lib/costs/engine.rbindex 99067fe8b73..6176970cdbd 100644--- a/modules/costs/lib/costs/engine.rb+++ b/modules/costs/lib/costs/engine.rb@@ -173,7 +173,7 @@ module Costsactivity_provider :time_entries, class_name: "Activities::TimeEntryActivityProvider", default: false- patches %i[Project User PermittedParams]+ patches %i[Project User PermittedParams WorkPackage]patch_with_namespace :BasicData, :SettingSeederpatch_with_namespace :ActiveSupport, :NumberHelper, :NumberToCurrencyConverterdiff --git a/modules/costs/lib/costs/patches/work_package_patch.rb b/modules/costs/lib/costs/patches/work_package_patch.rbnew file mode 100644index 00000000000..a61f204f043--- /dev/null+++ b/modules/costs/lib/costs/patches/work_package_patch.rb@@ -0,0 +1,37 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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 Costs::Patches::WorkPackagePatch+ extend ActiveSupport::Concern++ included do+ scopes :allowed_to_log_time+ end+enddiff --git a/modules/costs/spec/models/work_packages/scopes/allowed_to_log_time_spec.rb b/modules/costs/spec/models/work_packages/scopes/allowed_to_log_time_spec.rbnew file mode 100644index 00000000000..8c44572040e--- /dev/null+++ b/modules/costs/spec/models/work_packages/scopes/allowed_to_log_time_spec.rb@@ -0,0 +1,300 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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 WorkPackages::Scopes::AllowedToLogTime do+ shared_let(:non_admin) { create(:user) }+ shared_let(:admin) { create(:admin) }+ shared_let(:project_status) { true }+ shared_let(:private_project) { create(:project, public: false, active: project_status) }+ shared_let(:public_project) { create(:project, public: true, active: project_status) }++ shared_let(:work_package_in_public_project) { create(:work_package, project: public_project) }+ shared_let(:work_package_in_private_project) { create(:work_package, project: private_project) }+ shared_let(:other_work_package_in_private_project) { create(:work_package, project: private_project) }++ let(:project_permissions) { [] }+ let(:project_role) { create(:project_role, permissions: project_permissions) }++ let(:work_package_permissions) { [] }+ let(:work_package_role) { create(:work_package_role, permissions: work_package_permissions) }++ let(:non_member_permissions) { [] }+ let!(:non_member_role) { create(:non_member, permissions: non_member_permissions) }++ let(:action) { project_or_work_package_action }+ let(:project_or_work_package_action) { :log_own_time }++ subject { WorkPackage.allowed_to_log_time(user) }++ context "when the user is an admin" do+ let(:user) { admin }++ it "returns all work packages" do+ expect(subject).to contain_exactly(+ work_package_in_public_project,+ work_package_in_private_project,+ other_work_package_in_private_project+ )+ end++ context "when the project is archived" do+ before do+ public_project.update!(active: false)+ private_project.update!(active: false)+ end++ it "returns no work packages" do+ expect(subject).to be_empty+ end+ end++ context "when the user is locked" do+ before do+ user.locked!+ end++ it "returns no work packages" do+ expect(subject).to be_empty+ end+ end++ context "when the costs module is disabled" do+ before do+ private_project.enabled_module_names = private_project.enabled_module_names - ["costs"]+ end++ it "excludes work packages where the module is disabled in" do+ expect(subject).to contain_exactly(work_package_in_public_project)+ end+ end+ end++ context "when the user is a non admin, logged in user" do+ let(:user) { non_admin }++ shared_context "for a non admin, logged in user" do+ context "when the user has log_own_time permission directly on the work package" do+ let(:work_package_permissions) { [:log_own_time] }++ before do+ create(:member,+ project: private_project,+ entity: work_package_in_private_project,+ user:,+ roles: [work_package_role])+ end++ it "returns the authorized work package" do+ expect(subject).to contain_exactly(work_package_in_private_project)+ end++ context "when the project is archived" do+ before do+ public_project.update!(active: false)+ private_project.update!(active: false)+ end++ it "returns no work packages" do+ expect(subject).to be_empty+ end+ end++ context "when the module is inactive in the project" do+ before do+ public_project.enabled_modules = []+ private_project.enabled_modules = []+ end++ it "returns no work packages" do+ expect(subject).to be_empty+ end+ end++ context "when the user is locked" do+ before do+ user.locked!+ end++ it "returns no work packages" do+ expect(subject).to be_empty+ end+ end+ end++ context "when the user has the log_own_time permission on the project the work package belongs to" do+ let(:project_permissions) { [:log_own_time] }++ before do+ create(:member,+ project: private_project,+ user:,+ roles: [project_role])+ end++ it "returns the authorized work packages" do+ expect(subject).to contain_exactly(+ work_package_in_private_project,+ other_work_package_in_private_project+ )+ end++ context "when the project is archived" do+ before do+ public_project.update!(active: false)+ private_project.update!(active: false)+ end++ it "returns no work packages" do+ expect(subject).to be_empty+ end+ end++ context "when the module is inactive in the project" do+ before do+ public_project.enabled_modules = []+ private_project.enabled_modules = []+ end++ it "returns no work packages" do+ expect(subject).to be_empty+ end+ end++ context "when the user is locked" do+ before do+ user.locked!+ end++ it "returns no work packages" do+ expect(subject).to be_empty+ end+ end+ end++ context "when the user has the log_time permission on the project the work package belongs to" do+ let(:project_permissions) { [:log_time] }++ before do+ create(:member,+ project: private_project,+ user:,+ roles: [project_role])+ end++ it "returns the authorized work packages" do+ expect(subject).to contain_exactly(+ work_package_in_private_project,+ other_work_package_in_private_project+ )+ end++ context "when the project is archived" do+ before do+ public_project.update!(active: false)+ private_project.update!(active: false)+ end++ it "returns no work packages" do+ expect(subject).to be_empty+ end+ end++ context "when the module is inactive in the project" do+ before do+ public_project.enabled_modules = []+ private_project.enabled_modules = []+ end++ it "returns no work packages" do+ expect(subject).to be_empty+ end+ end++ context "when the user is locked" do+ before do+ user.locked!+ end++ it "returns no work packages" do+ expect(subject).to be_empty+ end+ end+ end++ context "when the user has a different permission on the project, but log_own_time on a specific work package" do+ let(:project_permissions) { [:view_work_packages] }+ let(:work_package_permissions) { %i[log_own_time] }++ before do+ create(:member, project: private_project, entity: work_package_in_private_project, user:, roles: [work_package_role])+ create(:member, project: private_project, user:, roles: [project_role])+ end++ it "returns the authorized work packages" do+ expect(subject).to contain_exactly(+ work_package_in_private_project+ )+ end+ end++ context "when the user isn`t member in the project" do+ before do+ non_member_role.save!+ end++ context "with the non member role having the permission" do+ let(:non_member_permissions) { [:log_own_time] }++ it "returns work packages in the public project" do+ expect(subject).to contain_exactly(work_package_in_public_project)+ end+ end++ context "with the non member role lacking the permission" do+ let(:non_member_permissions) { [] }++ it "is empty" do+ expect(subject).to be_empty+ end+ end+ end+ end++ context "in non large instances", with_settings: { large_instance_wp_allowed_to_sql: false } do+ include_context "for a non admin, logged in user"+ end++ context "in large instances", with_settings: { large_instance_wp_allowed_to_sql: true } do+ include_context "for a non admin, logged in user"+ end+ end+end