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

add permission check on setting journal_restricted

ulfertsda2efde9b46a
diff --git a/app/contracts/work_packages/create_note_contract.rb b/app/contracts/work_packages/create_note_contract.rb
index 3747186d7cc..d9652dbe2ca 100644
--- a/app/contracts/work_packages/create_note_contract.rb
+++ b/app/contracts/work_packages/create_note_contract.rb
@@ -38,9 +38,14 @@ module WorkPackages
end
attribute :journal_restricted do
- if model.journal_restricted && !OpenProject::FeatureDecisions.comments_with_restricted_visibility_active?
+ next unless model.journal_restricted
+
+ unless OpenProject::FeatureDecisions.comments_with_restricted_visibility_active?
errors.add(:journal_restricted, :feature_disabled)
end
+ unless allowed_in_project?(:add_comments_with_restricted_visibility)
+ errors.add(:journal_restricted, :error_unauthorized)
+ end
end
private
@@ -52,5 +57,9 @@ module WorkPackages
def allowed_in_work_package?(permission)
user.allowed_in_work_package?(permission, model)
end
+
+ def allowed_in_project?(permission)
+ user.allowed_in_project?(permission, model.project)
+ end
end
end
diff --git a/spec/contracts/work_packages/create_note_contract_spec.rb b/spec/contracts/work_packages/create_note_contract_spec.rb
index 57846d03fb0..91c7c9167da 100644
--- a/spec/contracts/work_packages/create_note_contract_spec.rb
+++ b/spec/contracts/work_packages/create_note_contract_spec.rb
@@ -45,7 +45,7 @@ RSpec.describe WorkPackages::CreateNoteContract do
wp
end
let(:user) { build_stubbed(:user) }
- let(:permissions) { %i[add_work_package_notes] }
+ let(:permissions) { %i[add_work_package_notes add_comments_with_restricted_visibility] }
before do
mock_permissions_for(user) do |mock|
@@ -114,6 +114,28 @@ RSpec.describe WorkPackages::CreateNoteContract do
it_behaves_like "contract is valid"
end
+
+ context "with journal_restricted is true, comments_with_restricted_visibility_active? is active but lacking permissions",
+ with_flag: { comments_with_restricted_visibility_active: true } do
+ let(:permissions) { super() - [:add_comments_with_restricted_visibility] }
+
+ before do
+ work_package.journal_restricted = true
+ end
+
+ it_behaves_like "contract is invalid", journal_restricted: :error_unauthorized
+ end
+
+ context "with journal_restricted is false, comments_with_restricted_visibility_active? is active and lacking permissions",
+ with_flag: { comments_with_restricted_visibility_active: true } do
+ let(:permissions) { super() - [:add_comments_with_restricted_visibility] }
+
+ before do
+ work_package.journal_restricted = false
+ end
+
+ it_behaves_like "contract is valid"
+ end
end
describe "another attribute of work package" do