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 permission checks for edit_own_work_package_notes

ulferts75b308d154a3
diff --git a/app/models/journal.rb b/app/models/journal.rb
index d597bd6e8d7..29e1ee00116 100644
--- a/app/models/journal.rb
+++ b/app/models/journal.rb
@@ -102,7 +102,7 @@ class Journal < ApplicationRecord
end
def editable_by?(user)
- (journable.journal_editable_by?(user) && self.user == user) || user.admin?
+ journable.journal_editable_by?(self, user)
end
def details
diff --git a/app/models/work_package.rb b/app/models/work_package.rb
index 5bd34a5d71d..d74b28b6a95 100644
--- a/app/models/work_package.rb
+++ b/app/models/work_package.rb
@@ -380,10 +380,9 @@ class WorkPackage < ApplicationRecord
# check if user is allowed to edit WorkPackage Journals.
# see Redmine::Acts::Journalized::Permissions#journal_editable_by
- def editable_by?(user)
- project = self.project
+ def journal_editable_by?(journal, user)
user.allowed_to?(:edit_work_package_notes, project, global: project.present?) ||
- user.allowed_to?(:edit_own_work_package_notes, project, global: project.present?)
+ user.allowed_to?(:edit_own_work_package_notes, project, global: project.present?) && journal.user_id == user.id
end
# Adds the 'virtual' attribute 'hours' to the result set. Using the
diff --git a/lib/api/v3/activities/activity_representer.rb b/lib/api/v3/activities/activity_representer.rb
index 83bbfd9c5e5..cf3e95f70b3 100644
--- a/lib/api/v3/activities/activity_representer.rb
+++ b/lib/api/v3/activities/activity_representer.rb
@@ -107,11 +107,7 @@ module API
private
def current_user_allowed_to_edit?
- (current_user_allowed_to(:edit_own_work_package_notes,
- context: represented.journable.project) &&
- represented.editable_by?(current_user)) ||
- current_user_allowed_to(:edit_work_package_notes,
- context: represented.journable.project)
+ represented.editable_by?(current_user)
end
def render_details(journal, no_html: false)
diff --git a/lib/plugins/acts_as_journalized/lib/redmine/acts/journalized/permissions.rb b/lib/plugins/acts_as_journalized/lib/redmine/acts/journalized/permissions.rb
index 0413184cc9e..7e9d60a1b2a 100644
--- a/lib/plugins/acts_as_journalized/lib/redmine/acts/journalized/permissions.rb
+++ b/lib/plugins/acts_as_journalized/lib/redmine/acts/journalized/permissions.rb
@@ -50,16 +50,18 @@ module Redmine::Acts::Journalized
module Permissions
# Default implementation of journal editing permission
# Is overridden if defined in the journalized model directly
- def journal_editable_by?(user)
+ def journal_editable_by?(journal, user)
return true if user.admin?
- if respond_to? :editable_by?
- editable_by? user
- else
- p = @project || (project if respond_to? :project)
- options = { global: p.present? }
- user.allowed_to? journable_edit_permission, p, options
- end
+ editable = if respond_to? :editable_by?
+ editable_by?(user)
+ else
+ p = @project || (project if respond_to? :project)
+ options = { global: p.present? }
+ user.allowed_to? journable_edit_permission, p, options
+ end
+
+ editable && journal.user_id == user.id
end
private
diff --git a/spec/lib/acts_as_journalized/journaled_spec.rb b/spec/lib/acts_as_journalized/journaled_spec.rb
index 7e92c7dfcc4..65a9c4412ca 100644
--- a/spec/lib/acts_as_journalized/journaled_spec.rb
+++ b/spec/lib/acts_as_journalized/journaled_spec.rb
@@ -109,26 +109,24 @@ describe 'Journalized Objects' do
context 'when the journable is a work package' do
let!(:user) { FactoryBot.create(:user) }
let!(:project) { FactoryBot.create(:project_with_types) }
- let!(:role) { FactoryBot.create(:role, permissions: [:edit_work_packages]) }
- let!(:member) {
- FactoryBot.create(:member, project: project,
- roles: [role],
- principal: user)
- }
- let!(:work_package) {
- FactoryBot.build(:work_package, type: project.types.first,
- author: user,
- project: project,
- description: '')
- }
-
- subject { work_package.journal_editable_by?(user) }
+ let!(:role) { FactoryBot.create(:role, permissions: []) }
+ let!(:member) do
+ FactoryBot.create(:member,
+ project: project,
+ roles: [role],
+ principal: user)
+ end
+ let!(:work_package) do
+ FactoryBot.create(:work_package,
+ type: project.types.first,
+ author: user,
+ project: project,
+ description: '')
+ end
- context 'and the user has no permission to "edit_work_packages"' do
- before do
- role.remove_permission! :edit_work_packages
- end
+ subject { work_package.journal_editable_by?(work_package.journals.first, user) }
+ context 'and the user has no permission to "edit_work_packages"' do
it { is_expected.to be_falsey }
end
end
diff --git a/spec/lib/api/v3/activities/activity_representer_spec.rb b/spec/lib/api/v3/activities/activity_representer_spec.rb
index dfb79af68db..f2b3f91f3bc 100644
--- a/spec/lib/api/v3/activities/activity_representer_spec.rb
+++ b/spec/lib/api/v3/activities/activity_representer_spec.rb
@@ -135,5 +135,22 @@ describe ::API::V3::Activities::ActivityRepresenter do
it 'should link to update' do
expect(subject).to have_json_path('_links/update/href')
end
+
+ context 'for a non own journal' do
+ context 'when having edit_work_package_notes' do
+ it 'should link to update' do
+ expect(subject).to have_json_path('_links/update/href')
+ end
+ end
+
+ context 'when only having edit_own_work_package_notes' do
+ let(:permissions) { %i(edit_own_work_package_notes) }
+
+ it 'has no update link' do
+ expect(subject)
+ .not_to have_json_path('_links/update/href')
+ end
+ end
+ end
end
end
diff --git a/spec/requests/api/v3/activities_api_spec.rb b/spec/requests/api/v3/activities_api_spec.rb
index 5ca219c1fca..82516f8f06f 100644
--- a/spec/requests/api/v3/activities_api_spec.rb
+++ b/spec/requests/api/v3/activities_api_spec.rb
@@ -39,7 +39,9 @@ describe API::V3::Activities::ActivitiesAPI, type: :request, content_type: :json
member_with_permissions: permissions)
end
let(:project) { FactoryBot.create(:project, public: false) }
- let(:work_package) { FactoryBot.create(:work_package, author: current_user, project: project) }
+ let(:work_package) do
+ FactoryBot.create(:work_package, author: current_user, project: project)
+ end
let(:permissions) { %i[view_work_packages edit_work_package_notes] }
let(:activity) { work_package.journals.first }
let(:comment) { 'This is a new test comment!' }
@@ -96,6 +98,35 @@ describe API::V3::Activities::ActivitiesAPI, type: :request, content_type: :json
end
end
+ context 'for an activity created by a different user' do
+ let(:activity) do
+ work_package.journals.first.tap do |journal|
+ # it does not matter that the user does not exist
+ journal.update_column(:user_id, 0)
+ end
+ end
+
+ context 'when having the necessary permission' do
+ it_behaves_like 'valid activity request', 'Activity::Comment'
+
+ it_behaves_like 'valid activity patch request'
+ end
+
+ context 'when having only the edit own permission' do
+ let(:permissions) { %i[view_work_packages edit_own_work_package_notes] }
+
+ it_behaves_like 'unauthorized access'
+ end
+ end
+
+ context 'when having only the edit own permission' do
+ let(:permissions) { %i[view_work_packages edit_own_work_package_notes] }
+
+ it_behaves_like 'valid activity request', 'Activity::Comment'
+
+ it_behaves_like 'valid activity patch request'
+ end
+
context 'without sufficient permissions to edit' do
let(:permissions) { [:view_work_packages] }