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

Define restricted extension on work package journals for query safety

Kabiru Mwenja078c32608fdb
diff --git a/app/components/work_packages/activities_tab/journals/index_component.rb b/app/components/work_packages/activities_tab/journals/index_component.rb
index 6229ba0fb8f..7b9477103c8 100644
--- a/app/components/work_packages/activities_tab/journals/index_component.rb
+++ b/app/components/work_packages/activities_tab/journals/index_component.rb
@@ -71,7 +71,7 @@ module WorkPackages
API::V3::Activities::ActivityEagerLoadingWrapper.wrap(
work_package
.journals
- .restricted_visible(@work_package.project)
+ .restricted_visible
.includes(:user, :customizable_journals, :attachable_journals, :storable_journals, :notifications)
.reorder(version: journal_sorting)
.with_sequence_version
diff --git a/app/models/journal.rb b/app/models/journal.rb
index a4a4cdf24a5..95da1c3ed52 100644
--- a/app/models/journal.rb
+++ b/app/models/journal.rb
@@ -113,14 +113,6 @@ class Journal < ApplicationRecord
scope :for_wiki_page, -> { where(journable_type: "WikiPage") }
scope :for_work_package, -> { where(journable_type: "WorkPackage") }
scope :for_meeting, -> { where(journable_type: "Meeting") }
- scope :restricted_visible, ->(project) {
- if OpenProject::FeatureDecisions.comments_with_restricted_visibility_active? &&
- User.current.allowed_in_project?(:view_comments_with_restricted_visibility, project)
- all
- else
- where(restricted: false)
- end
- }
# In conjunction with the included Comparable module, allows comparison of journal records
# based on their corresponding version numbers, creation timestamps and IDs.
diff --git a/app/models/work_package/journalized.rb b/app/models/work_package/journalized.rb
index 4289e1eb3d9..6b6bd5a10a7 100644
--- a/app/models/work_package/journalized.rb
+++ b/app/models/work_package/journalized.rb
@@ -30,7 +30,16 @@ module WorkPackage::Journalized
extend ActiveSupport::Concern
included do
- acts_as_journalized
+ acts_as_journalized do
+ def restricted_visible
+ if OpenProject::FeatureDecisions.comments_with_restricted_visibility_active? &&
+ User.current.allowed_in_project?(:view_comments_with_restricted_visibility, proxy_association.owner.project)
+ all
+ else
+ where(restricted: false)
+ end
+ end
+ end
# This one is here only to ease reading
module JournalizedProcs
diff --git a/lib/api/v3/activities/activities_by_work_package_api.rb b/lib/api/v3/activities/activities_by_work_package_api.rb
index f2b69aa53d6..d19c152a464 100644
--- a/lib/api/v3/activities/activities_by_work_package_api.rb
+++ b/lib/api/v3/activities/activities_by_work_package_api.rb
@@ -38,7 +38,7 @@ module API
journals = @work_package
.journals
- .restricted_visible(@work_package.project)
+ .restricted_visible
.includes(:data,
:customizable_journals,
:attachable_journals,
diff --git a/lib_static/plugins/acts_as_journalized/lib/acts_as_journalized.rb b/lib_static/plugins/acts_as_journalized/lib/acts_as_journalized.rb
index dc5d76e6ea9..8b2ab573b43 100644
--- a/lib_static/plugins/acts_as_journalized/lib/acts_as_journalized.rb
+++ b/lib_static/plugins/acts_as_journalized/lib/acts_as_journalized.rb
@@ -67,16 +67,16 @@ module Acts
end
# This call will start journaling the model.
- def acts_as_journalized(options = {})
+ def acts_as_journalized(options = {}, &)
return if journaled?
include_aaj_modules
prepare_journaled_options(options)
- has_many :journals, -> {
+ has_many(:journals, -> {
order("#{Journal.table_name}.version ASC")
- }, **has_many_journals_options
+ }, **has_many_journals_options, &)
end
private
diff --git a/spec/models/journal_spec.rb b/spec/models/journal_spec.rb
index 814ecff068b..5815753e74a 100644
--- a/spec/models/journal_spec.rb
+++ b/spec/models/journal_spec.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
@@ -68,48 +70,4 @@ RSpec.describe Journal do
end
end
end
-
- describe ".restricted_visible scope", with_flag: { comments_with_restricted_visibility: true } do
- let(:work_package) { create(:work_package) }
- let(:admin) { create(:admin) }
- let(:user) { create(:user) }
- let!(:restricted_note) do
- create(:work_package_journal,
- user: admin,
- notes: "First comment by admin",
- journable: work_package,
- restricted: true,
- version: 2)
- end
-
- subject { described_class.restricted_visible(work_package.project) }
-
- before do
- login_as user
- end
-
- context "when the user cannot see restricted" do
- before do
- mock_permissions_for(user) do |mock|
- mock.allow_in_work_package :view_work_packages, work_package:
- end
- end
-
- it "does not return the restricted journal" do
- expect(subject.map(&:id)).not_to include(restricted_note.id)
- end
- end
-
- context "when the user can see restricted" do
- before do
- mock_permissions_for(user) do |mock|
- mock.allow_in_project(:view_comments_with_restricted_visibility, project: work_package.project)
- end
- end
-
- it "returns the restricted journal" do
- expect(subject.map(&:id)).to include(restricted_note.id)
- end
- end
- end
end
diff --git a/spec/models/work_package/work_package_acts_as_journalized_spec.rb b/spec/models/work_package/work_package_acts_as_journalized_spec.rb
index dfcca6fbf18..9a211518c20 100644
--- a/spec/models/work_package/work_package_acts_as_journalized_spec.rb
+++ b/spec/models/work_package/work_package_acts_as_journalized_spec.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
@@ -870,4 +872,74 @@ RSpec.describe WorkPackage do
.to be_nil
end
end
+
+ describe "#journals.restricted_visible" do
+ let(:work_package) { create(:work_package) }
+ let(:admin) { create(:admin) }
+ let(:user) { create(:user) }
+
+ let!(:restricted_note) do
+ create(:work_package_journal,
+ user: admin,
+ notes: "First comment by admin",
+ journable: work_package,
+ restricted: true,
+ version: 2)
+ end
+
+ let!(:unrestricted_note) do
+ create(:work_package_journal,
+ user:,
+ notes: "First comment by user",
+ journable: work_package,
+ restricted: false,
+ version: 3)
+ end
+
+ subject(:journals) { work_package.journals.restricted_visible }
+
+ before do
+ login_as user
+ end
+
+ context "when comments_with_restricted_visibility is enabled", with_flag: { comments_with_restricted_visibility: true } do
+ context "when the user cannot see restricted journals" do
+ before do
+ mock_permissions_for(user) do |mock|
+ mock.allow_in_work_package :view_work_packages, work_package:
+ end
+ end
+
+ it "does not return the restricted journal" do
+ expect(journals.map(&:id)).not_to include(restricted_note.id)
+ expect(journals.map(&:id)).to include(unrestricted_note.id)
+ end
+ end
+
+ context "when the user can see restricted journals" do
+ before do
+ mock_permissions_for(user) do |mock|
+ mock.allow_in_project(:view_comments_with_restricted_visibility, project: work_package.project)
+ end
+ end
+
+ it "returns all journals" do
+ expect(journals.map(&:id)).to include(restricted_note.id, unrestricted_note.id)
+ end
+ end
+ end
+
+ context "when comments_with_restricted_visibility is disabled", with_flag: { comments_with_restricted_visibility: false } do
+ before do
+ mock_permissions_for(user) do |mock|
+ mock.allow_in_project(:view_comments_with_restricted_visibility, project: work_package.project)
+ end
+ end
+
+ it "does not return the restricted journal regardless of permissions" do
+ expect(journals.map(&:id)).not_to include(restricted_note.id)
+ expect(journals.map(&:id)).to include(unrestricted_note.id)
+ end
+ end
+ end
end