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
Add restricted context to journal visibility on API
Kabiru Mwenjac2bb6d7cf6a5
diff --git a/app/models/journal.rb b/app/models/journal.rbindex e7b2e22336b..1ade0900904 100644--- a/app/models/journal.rb+++ b/app/models/journal.rb@@ -150,6 +150,22 @@ class Journal < ApplicationRecordendend+ def attachments_visible?(user = User.current)+ if restricted?+ super && user.allowed_in_project?(:view_comments_with_restricted_visibility, project)+ else+ super+ end+ end++ def visible?(user = User.current)+ if restricted?+ user.allowed_in_project?(:view_comments_with_restricted_visibility, project)+ else+ journable.visible?(user)+ end+ end+def editable_by?(user)journable.journal_editable_by?(self, user)enddiff --git a/lib/api/v3/activities/activities_api.rb b/lib/api/v3/activities/activities_api.rbindex 3240e20f55f..c4c4b96b6ed 100644--- a/lib/api/v3/activities/activities_api.rb+++ b/lib/api/v3/activities/activities_api.rb@@ -35,7 +35,13 @@ module APIafter_validation do@activity = Journal.find(declared_params[:id])- authorize_by_with_raise @activity.journable.visible?(current_user) do+ authorize_by_with_raise(+ if request.get?+ @activity.visible?(current_user)+ else+ @activity.editable_by?(current_user)+ end+ ) doraise API::Errors::NotFoundendenddiff --git a/spec/models/journal_spec.rb b/spec/models/journal_spec.rbindex 205a3502f49..034d8bc24d6 100644--- a/spec/models/journal_spec.rb+++ b/spec/models/journal_spec.rb@@ -34,6 +34,89 @@ RSpec.describe Journal dolet(:project) { create(:project) }let(:work_package) { create(:work_package, project:) }let(:model_instance) { create(:work_package_journal, journable: work_package, version: 2) }++ describe "#attachments_visible?" do+ context "with a restricted journal" do+ let(:restricted_journal) { build_stubbed(:work_package_journal, journable: work_package, restricted: true) }++ context "with the user having the restricted permission" do+ let(:user_with_restricted_permission) do+ create(:user, member_with_permissions: { project => %i[view_work_packages view_comments_with_restricted_visibility] })+ end++ let(:current_user) { user_with_restricted_permission }++ before do+ login_as(current_user)+ end++ it "is visible" do+ expect(restricted_journal).to be_attachments_visible+ end+ end++ context "with the user not having the restricted permission" do+ it "returns false" do+ expect(restricted_journal).not_to be_attachments_visible+ end+ end+ end+ end+ end++ describe "#visible?" do+ let(:work_package) { create(:work_package) }+ let(:journal) { create(:work_package_journal, journable: work_package, version: 2) }++ let(:user_with_restricted_permission) do+ create(:user,+ member_with_permissions: { work_package.project => %i[view_work_packages+ view_comments_with_restricted_visibility] })+ end++ let(:user_with_view_work_packages_permission) do+ create(:user,+ member_with_permissions: { work_package.project => %i[view_work_packages] })+ end++ context "with a restricted journal" do+ before do+ journal.update!(restricted: true)+ end++ context "with the user having view permission for restricted journals" do+ before do+ login_as(user_with_restricted_permission)+ end++ it "is visible" do+ expect(journal).to be_visible+ end+ end++ context "with the user not having view permission for restricted journals" do+ before do+ login_as(user_with_view_work_packages_permission)+ end++ it "is not visible" do+ expect(journal).not_to be_visible+ end+ end+ end++ context "with a journal that is not restricted" do+ before do+ journal.update!(restricted: false)+ login_as(user_with_view_work_packages_permission)+ end++ context "and the user has permission to view unrestricted journals" do+ it "is visible" do+ expect(journal).to be_visible+ end+ end+ endenddescribe "#journable" do