Skip to content

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

fix visibility scope of labor items

ulferts9bd0244f7644
diff --git a/modules/budgets/app/models/labor_budget_item.rb b/modules/budgets/app/models/labor_budget_item.rb
index 993330ab529..6800e7bed70 100644
--- a/modules/budgets/app/models/labor_budget_item.rb
+++ b/modules/budgets/app/models/labor_budget_item.rb
@@ -41,26 +41,8 @@ class LaborBudgetItem < ApplicationRecord
include ActiveModel::ForbiddenAttributesProtection
# user_id correctness is ensured in Budget#*_labor_budget_item_attributes=
- def self.visible(user, project)
- table = arel_table
-
- view_allowed = Project.allowed_to(user, :view_hourly_rates).select(:id)
- view_own_allowed = Project.allowed_to(user, :view_own_hourly_rate).select(:id)
-
- view_or_view_own = table[:project_id]
- .in(view_allowed.arel)
- .or(table[:project_id]
- .in(view_own_allowed.arel)
- .and(table[:user_id].eq(user.id)))
-
- scope = includes([{ budget: :project }, :user])
- .references(:projects)
- .where(view_or_view_own)
-
- if project
- scope.where(budget: { projects_id: project.id })
- end
- end
+ include Scopes::Scoped
+ scopes :visible
scope :visible_costs, lambda { |*args|
visible((args.first || User.current), args[1])
diff --git a/modules/budgets/app/models/labor_budget_items/scopes/visible.rb b/modules/budgets/app/models/labor_budget_items/scopes/visible.rb
new file mode 100644
index 00000000000..6449f055db8
--- /dev/null
+++ b/modules/budgets/app/models/labor_budget_items/scopes/visible.rb
@@ -0,0 +1,56 @@
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2024 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 LaborBudgetItems::Scopes
+ module Visible
+ extend ActiveSupport::Concern
+
+ class_methods do
+ # Return labor budget items visible to a user:
+ # * the user has permission to see all labor budget items in the project the budget is in.
+ # * the user has permission to see own labor budget items in the project the budget is in and the item
+ # is of the user.
+ def visible(user, project)
+ view_scope = includes(:budget)
+ .where(budget: { project_id: Project.allowed_to(user, :view_hourly_rates).select(:id) })
+
+ view_own_scope = includes(:budget)
+ .where(budget: { project_id: Project.allowed_to(user, :view_own_hourly_rate).select(:id) })
+ .where(user_id: user.id)
+
+ scope = view_scope.or(view_own_scope)
+
+ if project
+ scope.where(budget: { project_id: project.id })
+ else
+ scope
+ end
+ end
+ end
+ end
+end
diff --git a/modules/budgets/spec/models/labor_budget_items/scopes/visibles_spec.rb b/modules/budgets/spec/models/labor_budget_items/scopes/visibles_spec.rb
new file mode 100644
index 00000000000..f943ae5d3d4
--- /dev/null
+++ b/modules/budgets/spec/models/labor_budget_items/scopes/visibles_spec.rb
@@ -0,0 +1,71 @@
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2024 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 "#{File.dirname(__FILE__)}/../../../spec_helper"
+
+RSpec.describe LaborBudgetItems::Scopes::Visible do
+ shared_let(:view_project) { create(:project) }
+ shared_let(:view_own_project) { create(:project) }
+ shared_let(:other_user) { create(:user) }
+ shared_let(:user) do
+ create(:user,
+ member_with_permissions: { view_project => %i[view_hourly_rates],
+ view_own_project => %i[view_own_hourly_rate] })
+ end
+ shared_let(:view_budget) { create(:budget, project: view_project) }
+ shared_let(:view_own_budget) { create(:budget, project: view_own_project) }
+ shared_let(:own_budget_item_of_view_budget) { create(:labor_budget_item, budget: view_budget, user:) }
+ shared_let(:budget_item_of_view_budget) { create(:labor_budget_item, budget: view_budget, user: other_user) }
+ shared_let(:own_budget_item_of_view_own_budget) { create(:labor_budget_item, budget: view_own_budget, user:) }
+ shared_let(:budget_item_of_view_own_budget) { create(:labor_budget_item, budget: view_own_budget, user: other_user) }
+
+ describe ".visible" do
+ context "for all items" do
+ it "returns all from project the user has view permission and only own from project the user has view own permission" do
+ expect(LaborBudgetItem.visible(user, nil))
+ .to contain_exactly(own_budget_item_of_view_budget,
+ budget_item_of_view_budget,
+ own_budget_item_of_view_own_budget)
+ end
+ end
+
+ context "for items in a project the user has view rates permission" do
+ it "returns own and non own items" do
+ expect(LaborBudgetItem.visible(user, view_project))
+ .to contain_exactly(own_budget_item_of_view_budget, budget_item_of_view_budget)
+ end
+ end
+
+ context "for items in a project the user has view own rates permission" do
+ it "returns own items" do
+ expect(LaborBudgetItem.visible(user, view_own_project))
+ .to contain_exactly(own_budget_item_of_view_own_budget)
+ end
+ end
+ end
+end