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

Prevent story editing if user has no permissions to do so

Christophe Bliard6221c791e115
diff --git a/frontend/src/stimulus/controllers/dynamic/backlogs/model.js b/frontend/src/stimulus/controllers/dynamic/backlogs/model.js
index b9ecb9c8156..359a37e44a6 100644
--- a/frontend/src/stimulus/controllers/dynamic/backlogs/model.js
+++ b/frontend/src/stimulus/controllers/dynamic/backlogs/model.js
@@ -304,14 +304,16 @@ RB.Model = (function ($) {
},
handleClick: function (e) {
- var field, model, j, editor;
-
- field = $(this);
- model = field.parents('.model').first().data('this');
- j = model.$;
-
- if (!j.hasClass('editing') && !j.hasClass('dragging') && !j.hasClass('prevent_edit') && !$(e.target).hasClass('prevent_edit')) {
- editor = model.edit();
+ const field = $(this);
+ const model = field.parents('.model').first().data('this');
+ const j = model.$;
+
+ if (!j.hasClass('editing')
+ && !j.hasClass('dragging')
+ && !j.hasClass('prevent_edit')
+ && !$(e.target).hasClass('prevent_edit')
+ && e.target.closest('.editable').getAttribute('fieldeditable') !== 'false' ) {
+ const editor = model.edit();
var input = editor.find('.' + $(e.currentTarget).attr('fieldname') + '.editor');
input.focus();
diff --git a/modules/backlogs/spec/features/backlogs/change_status_spec.rb b/modules/backlogs/spec/features/backlogs/change_status_spec.rb
new file mode 100644
index 00000000000..b332b60ea90
--- /dev/null
+++ b/modules/backlogs/spec/features/backlogs/change_status_spec.rb
@@ -0,0 +1,146 @@
+# 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'
+require_relative '../../support/pages/backlogs'
+
+RSpec.describe 'Backlogs context menu', :js, :with_cuprite do
+ shared_let(:story_type) { create(:type_feature) }
+ shared_let(:task_type) { create(:type_task) }
+ shared_let(:project) { create(:project, types: [story_type, task_type]) }
+ shared_let(:role) do
+ create(:project_role,
+ permissions: %i[edit_work_packages
+ change_work_package_status
+ view_master_backlog
+ view_work_packages])
+ end
+
+ shared_let(:user) do
+ create(:user,
+ member_with_roles: { project => role })
+ end
+ shared_let(:sprint) do
+ create(:version,
+ project:,
+ name: 'Sprint')
+ end
+ shared_let(:new_status) { create(:default_status, name: 'New') }
+ shared_let(:in_progress_status) { create(:status, name: 'In progress') }
+ shared_let(:default_priority) { create(:default_priority) }
+ shared_let(:story) do
+ create(:work_package,
+ type: story_type,
+ project:,
+ status: new_status,
+ priority: default_priority,
+ story_points: 3,
+ version: sprint)
+ end
+ shared_let(:workflow) do
+ create(:workflow,
+ old_status: new_status,
+ new_status: in_progress_status,
+ role:,
+ type: story_type)
+ end
+
+ let(:backlogs_page) { Pages::Backlogs.new(project) }
+
+ before do
+ allow(Setting)
+ .to receive(:plugin_openproject_backlogs)
+ .and_return('story_types' => [story_type.id.to_s],
+ 'task_type' => task_type.id.to_s)
+ login_as(user)
+ end
+
+ def expect_fields(enabled: [], disabled: [], none: [])
+ enabled.each do |field|
+ expect(page).to have_field(WorkPackage.human_attribute_name(field))
+ end
+
+ disabled.each do |field|
+ expect(page).to have_field(WorkPackage.human_attribute_name(field), disabled: true)
+ end
+
+ none.each do |field|
+ expect(page).to have_no_field(WorkPackage.human_attribute_name(field), visible: :all)
+ end
+ end
+
+ # this test acts as a control for the other tests in this file as it's easy to
+ # expect a field to not be present, and have the test still pass when the
+ # field is renamed.
+ context 'when the user has edit_work_packages permission' do
+ it 'is possible to edit any story field' do
+ backlogs_page.visit!
+ backlogs_page.enter_edit_story_mode(story)
+
+ expect_fields(enabled: %i[type subject status story_points])
+
+ backlogs_page.alter_attributes_in_edit_story_mode(story, subject: 'Hello subject')
+ backlogs_page.save_story_from_edit_mode(story)
+
+ expect(story.reload.subject).to eq('Hello subject')
+ end
+ end
+
+ context 'when the user has only change_work_package_status permission' do
+ before do
+ RolePermission.where(permission: 'edit_work_packages').delete_all
+ end
+
+ it 'is only possible to edit status field of stories' do
+ backlogs_page.visit!
+ backlogs_page.enter_edit_story_mode(story, text: story.status.name)
+
+ expect_fields(enabled: %i[status], disabled: %i[type subject story_points])
+
+ backlogs_page.alter_attributes_in_edit_story_mode(story, status: in_progress_status.name)
+ backlogs_page.save_story_from_edit_mode(story)
+
+ expect(story.reload.status).to eq(in_progress_status)
+ end
+ end
+
+ context 'when the user has neither change_work_package_status nor edit_work_packages permission' do
+ before do
+ RolePermission.where(permission: ['change_work_package_status', 'edit_work_packages']).delete_all
+ end
+
+ it 'is not possible to edit any story field' do
+ backlogs_page.visit!
+ backlogs_page.enter_edit_story_mode(story)
+
+ expect_fields(none: %i[type subject status story_points])
+ end
+ end
+end
diff --git a/modules/backlogs/spec/support/pages/backlogs.rb b/modules/backlogs/spec/support/pages/backlogs.rb
index 430ccc06efd..d17bbd6a49c 100644
--- a/modules/backlogs/spec/support/pages/backlogs.rb
+++ b/modules/backlogs/spec/support/pages/backlogs.rb
@@ -37,9 +37,10 @@ module Pages
@project = project
end
- def enter_edit_story_mode(story)
+ def enter_edit_story_mode(story, text: nil)
+ text ||= story.subject
within_story(story) do
- find('*', text: story.subject).click
+ find(:css, '.editable', text:).click
end
end
@@ -52,15 +53,12 @@ module Pages
def alter_attributes_in_edit_story_mode(story, attributes)
edit_proc = ->(*) do
attributes.each do |key, value|
+ field_name = WorkPackage.human_attribute_name(key)
case key
- when :subject
- fill_in 'Subject', with: value
- when :story_points
- fill_in 'Story Points', with: value
- when :status
- select value, from: 'Status'
- when :type
- select value, from: 'Type'
+ when :subject, :story_points
+ fill_in field_name, with: value
+ when :status, :type
+ select value, from: field_name
else
raise NotImplementedError
end
@@ -93,10 +91,10 @@ module Pages
def save_story_from_edit_mode(story)
save_proc = ->(*) do
- find('input[name=subject]').native.send_key :return
+ # for some reason, have to send 2 return keys for the select field
+ find_field(disabled: false, match: :first).send_keys(:return, :return)
- expect(page)
- .to have_no_css('input[name=subject]')
+ expect(page).to have_no_field(WorkPackage.human_attribute_name(:subject))
end
if story
@@ -104,6 +102,7 @@ module Pages
else
save_proc.call
end
+ wait_for_save_completion
end
def save_backlog_from_edit_mode(backlog)
@@ -115,6 +114,10 @@ module Pages
end
end
+ def wait_for_save_completion
+ expect(page).to have_no_css('.ajax-indicator')
+ end
+
def edit_backlog(backlog, attributes)
enter_edit_backlog_mode(backlog)