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

fixed permissions, handle permissions on UI, added permission specs

Jonas Jabari205af10a4a4e
diff --git a/app/components/work_packages/activities_tab/index_component.html.erb b/app/components/work_packages/activities_tab/index_component.html.erb
index 10af70e19db..bfd5bc31fa5 100644
--- a/app/components/work_packages/activities_tab/index_component.html.erb
+++ b/app/components/work_packages/activities_tab/index_component.html.erb
@@ -20,22 +20,24 @@
WorkPackages::ActivitiesTab::Journals::IndexComponent.new(work_package:, filter:)
)
end
- journals_wrapper_container.with_row(
- id: "input-container",
- classes: "sort-#{journal_sorting}", # css order attribute will take care of correct position
- mt: 3,
- mb: [3, nil, nil, nil, 0],
- pt: 2,
- pb: 2,
- pl: 3,
- pr: [3, nil, nil, nil, 2],
- border: [nil, nil, nil, nil, :top],
- border_radius: [2, nil, nil, nil, 0],
- bg: :subtle
- ) do
- render(
- WorkPackages::ActivitiesTab::Journals::NewComponent.new(work_package:)
- )
+ if adding_comment_allowed?
+ journals_wrapper_container.with_row(
+ id: "input-container",
+ classes: "sort-#{journal_sorting}", # css order attribute will take care of correct position
+ mt: 3,
+ mb: [3, nil, nil, nil, 0],
+ pt: 2,
+ pb: 2,
+ pl: 3,
+ pr: [3, nil, nil, nil, 2],
+ border: [nil, nil, nil, nil, :top],
+ border_radius: [2, nil, nil, nil, 0],
+ bg: :subtle
+ ) do
+ render(
+ WorkPackages::ActivitiesTab::Journals::NewComponent.new(work_package:)
+ )
+ end
end
end
end
diff --git a/app/components/work_packages/activities_tab/index_component.rb b/app/components/work_packages/activities_tab/index_component.rb
index b6db9d8bf16..c08331b9d62 100644
--- a/app/components/work_packages/activities_tab/index_component.rb
+++ b/app/components/work_packages/activities_tab/index_component.rb
@@ -69,6 +69,10 @@ module WorkPackages
def polling_interval
ENV["WORK_PACKAGES_ACTIVITIES_TAB_POLLING_INTERVAL_IN_MS"] || 10000
end
+
+ def adding_comment_allowed?
+ User.current.allowed_in_project?(:add_work_package_notes, @work_package.project)
+ end
end
end
end
diff --git a/app/components/work_packages/activities_tab/journals/item_component.html.erb b/app/components/work_packages/activities_tab/journals/item_component.html.erb
index e3928b9cc3c..35292b2e3f0 100644
--- a/app/components/work_packages/activities_tab/journals/item_component.html.erb
+++ b/app/components/work_packages/activities_tab/journals/item_component.html.erb
@@ -48,8 +48,8 @@
'aria-label': I18n.t(:button_actions),
scheme: :invisible)
copy_url_action_item(menu)
- edit_action_item(menu) if editable?
- quote_action_item(menu) if journal.notes.present?
+ edit_action_item(menu) if allowed_to_edit?
+ quote_action_item(menu) if journal.notes.present? && allowed_to_quote?
end
end
end
diff --git a/app/components/work_packages/activities_tab/journals/item_component.rb b/app/components/work_packages/activities_tab/journals/item_component.rb
index b134ae47264..6d4c3e33fe7 100644
--- a/app/components/work_packages/activities_tab/journals/item_component.rb
+++ b/app/components/work_packages/activities_tab/journals/item_component.rb
@@ -84,10 +84,6 @@ module WorkPackages
"#activity-#{journal.version}"
end
- def editable?
- journal.user == User.current
- end
-
def updated?
return false if journal.initial?
@@ -102,6 +98,23 @@ module WorkPackages
has_unread_notifications? && journal.notes.blank?
end
+ def allowed_to_edit?
+ allowed_to_edit_others? || allowed_to_edit_own?
+ end
+
+ def allowed_to_edit_others?
+ User.current.allowed_in_project?(:edit_work_package_notes, journal.journable.project)
+ end
+
+ def allowed_to_edit_own?
+ journal.user == User.current && User.current.allowed_in_project?(:edit_own_work_package_notes,
+ journal.journable.project)
+ end
+
+ def allowed_to_quote?
+ User.current.allowed_in_project?(:add_work_package_notes, journal.journable.project)
+ end
+
def copy_url_action_item(menu)
menu.with_item(label: t("button_copy_link_to_clipboard"),
tag: :button,
diff --git a/app/components/work_packages/activities_tab/journals/new_component.html.erb b/app/components/work_packages/activities_tab/journals/new_component.html.erb
index 267ce19a7c2..49a0236e70d 100644
--- a/app/components/work_packages/activities_tab/journals/new_component.html.erb
+++ b/app/components/work_packages/activities_tab/journals/new_component.html.erb
@@ -1,6 +1,6 @@
<%=
component_wrapper do
- flex_layout(my: 2) do |new_form_container|
+ flex_layout(my: 2, data: { test_selector: "op-work-package-journal-form" }) do |new_form_container|
new_form_container.with_row(data: {
"work-packages--activities-tab--index-target": "buttonRow"
}) do
diff --git a/app/components/work_packages/activities_tab/journals/new_component.rb b/app/components/work_packages/activities_tab/journals/new_component.rb
index 6468cd7d0d0..3b513e81a2e 100644
--- a/app/components/work_packages/activities_tab/journals/new_component.rb
+++ b/app/components/work_packages/activities_tab/journals/new_component.rb
@@ -40,6 +40,8 @@ module WorkPackages
@work_package = work_package
end
+ private
+
attr_reader :work_package
def journal
diff --git a/config/initializers/permissions.rb b/config/initializers/permissions.rb
index 86ba6b19835..14916a7d5b9 100644
--- a/config/initializers/permissions.rb
+++ b/config/initializers/permissions.rb
@@ -217,8 +217,7 @@ Rails.application.reloader.to_prepare do
work_packages: %i[show index],
work_packages_api: [:get],
"work_packages/reports": %i[report report_details],
- "work_packages/activities_tab": %i[index create update_streams edit cancel_edit update
- update_sorting update_filter],
+ "work_packages/activities_tab": %i[index update_streams update_sorting update_filter],
"work_packages/menus": %i[show]
},
permissible_on: %i[work_package project],
@@ -256,19 +255,24 @@ Rails.application.reloader.to_prepare do
{
# FIXME: Although the endpoint is removed, the code checking whether a user
# is eligible to add work packages through the API still seems to rely on this.
- journals: [:new]
+ journals: [:new],
+ "work_packages/activities_tab": %i[create]
},
permissible_on: %i[work_package project],
dependencies: :view_work_packages
wpt.permission :edit_work_package_notes,
- {},
+ {
+ "work_packages/activities_tab": %i[edit cancel_edit update]
+ },
permissible_on: :project,
require: :loggedin,
dependencies: :view_work_packages
wpt.permission :edit_own_work_package_notes,
- {},
+ {
+ "work_packages/activities_tab": %i[edit cancel_edit update]
+ },
permissible_on: %i[work_package project],
require: :loggedin,
dependencies: :view_work_packages
diff --git a/spec/features/activities/work_package/activities_spec.rb b/spec/features/activities/work_package/activities_spec.rb
index 771a5bfe3c6..3cdb2e7e20f 100644
--- a/spec/features/activities/work_package/activities_spec.rb
+++ b/spec/features/activities/work_package/activities_spec.rb
@@ -33,7 +33,7 @@ RSpec.describe "Work package activity", :js, :with_cuprite, with_flag: { primeri
let(:admin) { create(:admin) }
let(:member_role) do
create(:project_role,
- permissions: %i[view_work_packages edit_work_packages add_work_packages work_package_assigned])
+ permissions: %i[view_work_packages edit_work_packages add_work_packages work_package_assigned add_work_package_notes])
end
let(:member) do
create(:user,
@@ -45,6 +45,180 @@ RSpec.describe "Work package activity", :js, :with_cuprite, with_flag: { primeri
let(:wp_page) { Pages::FullWorkPackage.new(work_package, project) }
let(:activity_tab) { Components::WorkPackages::Activities.new(work_package) }
+ describe "permission checks" do
+ let(:viewer_role) do
+ create(:project_role,
+ permissions: %i[view_work_packages])
+ end
+ let(:viewer) do
+ create(:user,
+ firstname: "A",
+ lastname: "Viewer",
+ member_with_roles: { project => viewer_role })
+ end
+
+ let(:viewer_role_with_commenting_permission) do
+ create(:project_role,
+ permissions: %i[view_work_packages add_work_package_notes edit_own_work_package_notes])
+ end
+ let(:viewer_with_commenting_permission) do
+ create(:user,
+ firstname: "A",
+ lastname: "Viewer",
+ member_with_roles: { project => viewer_role_with_commenting_permission })
+ end
+
+ let(:user_role_with_editing_permission) do
+ create(:project_role,
+ permissions: %i[view_work_packages add_work_package_notes edit_work_package_notes])
+ end
+ let(:user_with_editing_permission) do
+ create(:user,
+ firstname: "A",
+ lastname: "Viewer",
+ member_with_roles: { project => user_role_with_editing_permission })
+ end
+
+ let(:work_package) { create(:work_package, project:, author: admin) }
+ let(:first_comment) do
+ create(:work_package_journal, user: admin, notes: "First comment by admin", journable: work_package,
+ version: 2)
+ end
+
+ context "when project is public", with_settings: { login_required: false } do
+ let(:project) { create(:project, public: true) }
+ let!(:anonymous_role) do
+ create(:anonymous_role, permissions: %i[view_project view_work_packages])
+ end
+
+ context "when visited by an anonymous visitor" do
+ before do
+ first_comment
+
+ login_as User.anonymous
+
+ wp_page.visit!
+ wp_page.wait_for_activity_tab
+ end
+
+ it "does show comments but does not enable adding, editing or quoting comments" do
+ activity_tab.expect_journal_notes(text: "First comment by admin")
+
+ activity_tab.within_journal_entry(first_comment) do
+ page.find_test_selector("op-wp-journal-#{first_comment.id}-action-menu").click
+
+ expect(page).not_to have_test_selector("op-wp-journal-#{first_comment.id}-edit")
+ expect(page).not_to have_test_selector("op-wp-journal-#{first_comment.id}-quote")
+ end
+
+ activity_tab.expect_no_input_field
+ end
+ end
+ end
+
+ context "when a user has only view_work_packages permission" do
+ current_user { viewer }
+
+ before do
+ first_comment
+
+ wp_page.visit!
+ wp_page.wait_for_activity_tab
+ end
+
+ it "does show comments but does not enable adding comments" do
+ activity_tab.expect_journal_notes(text: "First comment by admin")
+
+ activity_tab.within_journal_entry(first_comment) do
+ page.find_test_selector("op-wp-journal-#{first_comment.id}-action-menu").click
+
+ expect(page).not_to have_test_selector("op-wp-journal-#{first_comment.id}-edit")
+ expect(page).not_to have_test_selector("op-wp-journal-#{first_comment.id}-quote")
+ end
+
+ activity_tab.expect_no_input_field
+ end
+ end
+
+ context "when a user has add_work_package_notes and edit_own_work_package_notes permission" do
+ current_user { viewer_with_commenting_permission }
+
+ before do
+ first_comment
+
+ wp_page.visit!
+ wp_page.wait_for_activity_tab
+ end
+
+ it "does show comments and enable adding and quoting comments and editing own comments" do
+ activity_tab.expect_journal_notes(text: "First comment by admin")
+
+ activity_tab.within_journal_entry(first_comment) do
+ page.find_test_selector("op-wp-journal-#{first_comment.id}-action-menu").click
+
+ # not allowed to edit other user's comments
+ expect(page).not_to have_test_selector("op-wp-journal-#{first_comment.id}-edit")
+ # allowed to quote other user's comments
+ expect(page).to have_test_selector("op-wp-journal-#{first_comment.id}-quote")
+ end
+
+ activity_tab.expect_input_field
+
+ activity_tab.add_comment(text: "First comment by viewer with commenting permission")
+
+ second_comment = work_package.journals.reload.last
+
+ activity_tab.within_journal_entry(second_comment) do
+ # for some reason only opens on the second click in the test
+ # probably due to the previous click on the first comment
+ 2.times { page.find_test_selector("op-wp-journal-#{second_comment.id}-action-menu").click }
+
+ expect(page).to have_test_selector("op-wp-journal-#{second_comment.id}-edit")
+ expect(page).to have_test_selector("op-wp-journal-#{second_comment.id}-quote")
+ end
+ end
+ end
+
+ context "when a user has add_work_package_notes and general edit_work_package_notes permission" do
+ current_user { user_with_editing_permission }
+
+ before do
+ first_comment
+
+ wp_page.visit!
+ wp_page.wait_for_activity_tab
+ end
+
+ it "does show comments and enable adding and quoting comments and editing own comments" do
+ activity_tab.expect_journal_notes(text: "First comment by admin")
+
+ activity_tab.within_journal_entry(first_comment) do
+ page.find_test_selector("op-wp-journal-#{first_comment.id}-action-menu").click
+
+ # allowed to edit other user's comments
+ expect(page).to have_test_selector("op-wp-journal-#{first_comment.id}-edit")
+ # allowed to quote other user's comments
+ expect(page).to have_test_selector("op-wp-journal-#{first_comment.id}-quote")
+ end
+
+ activity_tab.expect_input_field
+
+ activity_tab.add_comment(text: "First comment by viewer with editing permission")
+
+ second_comment = work_package.journals.reload.last
+
+ activity_tab.within_journal_entry(second_comment) do
+ # for some reason only opens on the second click in the test
+ # probably due to the previous click on the first comment
+ 2.times { page.find_test_selector("op-wp-journal-#{second_comment.id}-action-menu").click }
+
+ expect(page).to have_test_selector("op-wp-journal-#{second_comment.id}-edit")
+ expect(page).to have_test_selector("op-wp-journal-#{second_comment.id}-quote")
+ end
+ end
+ end
+ end
+
context "when a workpackage is created and visited by the same user" do
current_user { admin }
let(:work_package) { create(:work_package, project:, author: admin) }
diff --git a/spec/support/components/work_packages/activities.rb b/spec/support/components/work_packages/activities.rb
index 97204b2f979..7200cbc7df5 100644
--- a/spec/support/components/work_packages/activities.rb
+++ b/spec/support/components/work_packages/activities.rb
@@ -135,6 +135,14 @@ module Components
expect(page).not_to have_test_selector("op-wp-journals-container-empty")
end
+ def expect_input_field
+ expect(page).to have_test_selector("op-work-package-journal-form")
+ end
+
+ def expect_no_input_field
+ expect(page).not_to have_test_selector("op-work-package-journal-form")
+ end
+
def add_comment(text: nil, save: true)
# TODO: get rid of static sleep
sleep 1 # otherwise the stimulus component is not mounted yet and the click does not work