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

Adopt permission model for restricted comments

Kabiru Mwenja139751fc8860
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 1ab04e2cd4a..0b1173c5564 100644
--- a/app/components/work_packages/activities_tab/journals/new_component.rb
+++ b/app/components/work_packages/activities_tab/journals/new_component.rb
@@ -59,8 +59,7 @@ module WorkPackages
end
def adding_restricted_comment_allowed?
- # TODO: change to use permissions
- true
+ User.current.allowed_in_work_package?(:write_comments_with_restricted_visibility, work_package)
end
end
end
diff --git a/app/models/work_package.rb b/app/models/work_package.rb
index 9e1a6eb3441..4d6374da712 100644
--- a/app/models/work_package.rb
+++ b/app/models/work_package.rb
@@ -382,8 +382,18 @@ class WorkPackage < ApplicationRecord
# check if user is allowed to edit WorkPackage Journals.
# see Acts::Journalized::Permissions#journal_editable_by
def journal_editable_by?(journal, user)
- user.allowed_in_project?(:edit_work_package_notes, project) ||
- (user.allowed_in_work_package?(:edit_own_work_package_notes, self) && journal.user_id == user.id)
+ can_edit_journal_note = ->(edit_others_notes_permission, edit_own_notes_permission) {
+ user.allowed_in_project?(edit_others_notes_permission, project) ||
+ (user.allowed_in_work_package?(edit_own_notes_permission, self) && journal.user_id == user.id)
+ }
+
+ if journal.restricted?
+ can_edit_journal_note.call(:edit_others_comments_with_restricted_visibility,
+ :edit_own_comments_with_restricted_visibility)
+ else
+ can_edit_journal_note.call(:edit_work_package_notes,
+ :edit_own_work_package_notes)
+ end
end
# Returns a scope for the projects
diff --git a/config/initializers/permissions.rb b/config/initializers/permissions.rb
index 96187a1ed4a..ca1ed7fd3c2 100644
--- a/config/initializers/permissions.rb
+++ b/config/initializers/permissions.rb
@@ -320,21 +320,21 @@ Rails.application.reloader.to_prepare do
{},
permissible_on: %i[work_package project],
require: :loggedin,
- dependencies: :view_work_packages,
+ dependencies: %i[view_work_packages view_comments_with_restricted_visibility],
visible: -> { OpenProject::FeatureDecisions.comments_with_restricted_visibility_active? }
wpt.permission :edit_own_comments_with_restricted_visibility,
{},
permissible_on: %i[work_package project],
require: :loggedin,
- dependencies: :view_work_packages,
+ dependencies: %i[view_work_packages view_comments_with_restricted_visibility],
visible: -> { OpenProject::FeatureDecisions.comments_with_restricted_visibility_active? }
wpt.permission :edit_others_comments_with_restricted_visibility,
{},
permissible_on: %i[work_package project],
require: :loggedin,
- dependencies: :view_work_packages,
+ dependencies: %i[view_work_packages view_comments_with_restricted_visibility],
visible: -> { OpenProject::FeatureDecisions.comments_with_restricted_visibility_active? }
# WP attachments can be added with :edit_work_packages, this permission allows it without Edit WP as well.
diff --git a/spec/features/activities/work_package/restricted_visibility_comments_spec.rb b/spec/features/activities/work_package/restricted_visibility_comments_spec.rb
new file mode 100644
index 00000000000..dffd23c7e79
--- /dev/null
+++ b/spec/features/activities/work_package/restricted_visibility_comments_spec.rb
@@ -0,0 +1,182 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require "spec_helper"
+
+RSpec.describe "Work package comments with restricted visibility", :js, :with_cuprite do
+ let(:project) { create(:project) }
+ let(:admin) { create(:admin) }
+ let(:viewer) { create_user_with_restricted_comments_view_permissions }
+ let(:viewer_with_commenting_permission) { create_user_with_restricted_comments_view_and_write_permissions }
+ let(:project_admin) { create_user_as_project_admin }
+
+ let(:work_package) { create(:work_package, project:, author: admin) }
+ let(:first_comment) do
+ create(:work_package_journal, user: admin, notes: "A (restricted) comment by admin",
+ journable: work_package, version: 2, restricted: true)
+ end
+
+ let(:wp_page) { Pages::FullWorkPackage.new(work_package, project) }
+ let(:activity_tab) { Components::WorkPackages::Activities.new(work_package) }
+
+ context "with an admin user" do
+ current_user { admin }
+
+ before do
+ wp_page.visit!
+ wp_page.wait_for_activity_tab
+ end
+
+ it "allows adding a comment with restricted visibility" do
+ activity_tab.expect_input_field
+
+ activity_tab.add_comment(text: "First (restricted) comment by admin", restricted: true)
+
+ activity_tab.expect_journal_notes(text: "First (restricted) comment by admin")
+ end
+ end
+
+ context "with a user that is only allowed to view comments with restricted visibility" do
+ current_user { viewer }
+
+ before do
+ first_comment
+
+ wp_page.visit!
+ wp_page.wait_for_activity_tab
+ end
+
+ it "does show restricted comments but does NOT enable editing or quoting comments" do
+ activity_tab.expect_journal_notes(text: "A (restricted) 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
+ end
+ end
+
+ context "with a user that is allowed to view, create and edit own comments" do
+ current_user { viewer_with_commenting_permission }
+
+ before do
+ first_comment
+
+ wp_page.visit!
+ wp_page.wait_for_activity_tab
+ end
+
+ it "allows adding a comment with restricted visibility" do
+ activity_tab.expect_input_field
+
+ activity_tab.add_comment(text: "First (restricted) comment by member", restricted: true)
+
+ activity_tab.expect_journal_notes(text: "First (restricted) comment by member")
+
+ created_comment = work_package.journals.reload.last
+
+ activity_tab.within_journal_entry(created_comment) do
+ page.find_test_selector("op-wp-journal-#{created_comment.id}-action-menu").click
+
+ expect(page).to have_test_selector("op-wp-journal-#{created_comment.id}-edit")
+ end
+ end
+
+ it "does show restricted comments but does NOT enable editing other users comments" do
+ activity_tab.expect_journal_notes(text: "A (restricted) 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 restricted 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
+ end
+ end
+
+ context "with a user that is allowed to view, create and edit all comments" do
+ current_user { project_admin }
+
+ before do
+ first_comment
+
+ wp_page.visit!
+ wp_page.wait_for_activity_tab
+ end
+
+ it "allows editing and quoting restricted comments" do
+ activity_tab.expect_journal_notes(text: "A (restricted) 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).to have_test_selector("op-wp-journal-#{first_comment.id}-edit")
+ expect(page).to have_test_selector("op-wp-journal-#{first_comment.id}-quote")
+ end
+ end
+ end
+
+ def create_user_as_project_admin
+ member_role = create(:project_role,
+ permissions: %i[view_work_packages add_work_package_notes
+ edit_own_work_package_notes
+ view_comments_with_restricted_visibility
+ write_comments_with_restricted_visibility
+ edit_own_comments_with_restricted_visibility
+ edit_others_comments_with_restricted_visibility])
+ create(:user, firstname: "Project", lastname: "Admin",
+ member_with_roles: { project => member_role })
+ end
+
+ def create_user_with_restricted_comments_view_permissions
+ viewer_role = create(:project_role, permissions: %i[view_work_packages view_comments_with_restricted_visibility])
+ create(:user,
+ firstname: "A",
+ lastname: "Viewer",
+ member_with_roles: { project => viewer_role })
+ end
+
+ def create_user_with_restricted_comments_view_and_write_permissions
+ viewer_role_with_commenting_permission = create(:project_role,
+ permissions: %i[view_work_packages add_work_package_notes
+ edit_own_work_package_notes
+ view_comments_with_restricted_visibility
+ write_comments_with_restricted_visibility
+ edit_own_comments_with_restricted_visibility])
+ create(:user,
+ firstname: "A",
+ lastname: "Viewer",
+ member_with_roles: { project => viewer_role_with_commenting_permission })
+ end
+end
diff --git a/spec/support/components/work_packages/activities.rb b/spec/support/components/work_packages/activities.rb
index 7cd28668ed1..3eb4c076207 100644
--- a/spec/support/components/work_packages/activities.rb
+++ b/spec/support/components/work_packages/activities.rb
@@ -232,7 +232,7 @@ module Components
page.find_test_selector("op-submit-work-package-journal-form").click
end
- def add_comment(text: nil, save: true)
+ def add_comment(text: nil, save: true, restricted: false)
if page.find_test_selector("op-open-work-package-journal-form-trigger")
open_new_comment_editor
else
@@ -241,6 +241,7 @@ module Components
page.within_test_selector("op-work-package-journal-form-element") do
get_editor_form_field_element.set_value(text)
+ page.check("Restricted visibility") if restricted
page.find_test_selector("op-submit-work-package-journal-form").click if save
end