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

remove WorkPackagePolicy

ulfertse904e530d8eb
diff --git a/app/contracts/work_packages/base_contract.rb b/app/contracts/work_packages/base_contract.rb
index 55b20e962f4..a6561859f9c 100644
--- a/app/contracts/work_packages/base_contract.rb
+++ b/app/contracts/work_packages/base_contract.rb
@@ -162,12 +162,6 @@ module WorkPackages
validate :validate_duration_and_dates_are_not_derivable
- def initialize(work_package, user, options: {})
- super
-
- @can = WorkPackagePolicy.new(user)
- end
-
def assignable_statuses(include_default: false)
# Do not allow skipping statuses without intermediately saving the work package.
# We therefore take the original status of the work_package, while preserving all
@@ -226,8 +220,6 @@ module WorkPackages
private
- attr_reader :can
-
def validate_after_soonest_start(date_attribute)
return if model.schedule_manually?
return if model.children.any?
diff --git a/app/contracts/work_packages/create_note_contract.rb b/app/contracts/work_packages/create_note_contract.rb
index 21d8d7700cb..3747186d7cc 100644
--- a/app/contracts/work_packages/create_note_contract.rb
+++ b/app/contracts/work_packages/create_note_contract.rb
@@ -33,7 +33,7 @@ module WorkPackages
def self.model = WorkPackage
attribute :journal_notes do
- errors.add(:journal_notes, :error_unauthorized) unless can?(:comment)
+ errors.add(:journal_notes, :error_unauthorized) unless adding_notes_allowed?
errors.add(:journal_notes, :blank) if model.journal_notes.blank?
end
@@ -45,14 +45,12 @@ module WorkPackages
private
- def can?(permission)
- policy.allowed?(model, permission)
+ def adding_notes_allowed?
+ allowed_in_work_package?(:add_work_package_notes) || allowed_in_work_package?(:edit_work_packages)
end
- attr_writer :policy
-
- def policy
- @policy ||= WorkPackagePolicy.new(user)
+ def allowed_in_work_package?(permission)
+ user.allowed_in_work_package?(permission, model)
end
end
end
diff --git a/app/contracts/work_packages/update_contract.rb b/app/contracts/work_packages/update_contract.rb
index 7d325f4bdc7..40182352139 100644
--- a/app/contracts/work_packages/update_contract.rb
+++ b/app/contracts/work_packages/update_contract.rb
@@ -49,18 +49,18 @@ module WorkPackages
attribute_permission :project_id, :move_work_packages
def can_set_parent?
- @can.allowed?(model, :manage_subtasks)
+ allowed_in_project?(:manage_subtasks)
end
private
def user_allowed_to_edit
with_unchanged_project_id do
- next if @can.allowed?(model, :edit) ||
- @can.allowed?(model, :assign_version) ||
- @can.allowed?(model, :change_status) ||
- @can.allowed?(model, :manage_subtasks) ||
- @can.allowed?(model, :move)
+ next if allowed_in_work_package?(:edit_work_packages) ||
+ allowed_in_project?(:assign_versions) ||
+ allowed_in_project?(:change_work_package_status) ||
+ allowed_in_project?(:manage_subtasks) ||
+ allowed_in_project?(:move_work_packages)
next if allowed_journal_addition?
errors.add :base, :error_unauthorized
@@ -74,7 +74,7 @@ module WorkPackages
end
def allowed_journal_addition?
- model.changes.empty? && model.journal_notes && can.allowed?(model, :comment)
+ model.changes.empty? && model.journal_notes && allowed_in_work_package?(:add_work_package_notes)
end
def can_move_to_milestone
@@ -93,5 +93,13 @@ module WorkPackages
errors.add :parent_id, :error_unauthorized
end
end
+
+ def allowed_in_project?(permission)
+ user.allowed_in_project?(permission, model.project)
+ end
+
+ def allowed_in_work_package?(permission)
+ user.allowed_in_work_package?(permission, model)
+ end
end
end
diff --git a/app/policies/work_package_policy.rb b/app/policies/work_package_policy.rb
deleted file mode 100644
index c30b4316f1c..00000000000
--- a/app/policies/work_package_policy.rb
+++ /dev/null
@@ -1,106 +0,0 @@
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) 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.
-#++
-
-class WorkPackagePolicy < BasePolicy
- private
-
- def cache(work_package)
- @cache ||= Hash.new do |wp_hash, wp|
- wp_hash[wp] = allowed_hash(wp)
- end
-
- @cache[work_package]
- end
-
- def allowed_hash(work_package)
- # copy checks for the move_work_packages permission. This makes
- # sense only because the work_packages/moves controller handles
- # copying multiple work packages.
- {
- edit: edit_allowed?(work_package),
- move: move_allowed?(work_package),
- copy: move_allowed?(work_package),
- duplicate: copy_allowed?(work_package), # duplicating is another form of copying
- delete: delete_allowed?(work_package),
- manage_subtasks: manage_subtasks_allowed?(work_package),
- comment: comment_allowed?(work_package),
- change_status: change_status_allowed?(work_package),
- assign_version: assign_version_allowed?(work_package)
- }
- end
-
- def edit_allowed?(work_package)
- work_package.persisted? && user.allowed_in_work_package?(:edit_work_packages, work_package)
- end
-
- def move_allowed?(work_package)
- user.allowed_in_project?(:move_work_packages, work_package.project)
- end
-
- def copy_allowed?(work_package)
- type_active_in_project?(work_package) && add_allowed?(work_package)
- end
-
- def delete_allowed?(work_package)
- user.allowed_in_project?(:delete_work_packages, work_package.project)
- end
-
- def add_allowed?(work_package)
- user.allowed_in_project?(:add_work_packages, work_package.project)
- end
-
- def type_active_in_project?(work_package)
- return false unless work_package.project
-
- @type_active_cache ||= Hash.new do |hash, project|
- hash[project] = project.types.pluck(:id)
- end
-
- @type_active_cache[work_package.project].include?(work_package.type_id)
- end
-
- def manage_subtasks_allowed?(work_package)
- user.allowed_in_project?(:manage_subtasks, work_package.project)
- end
-
- def comment_allowed?(work_package)
- user.allowed_in_work_package?(:add_work_package_notes, work_package) || edit_allowed?(work_package)
- end
-
- def assign_version_allowed?(work_package)
- user.allowed_in_project?(:assign_versions, work_package.project)
- end
-
- def change_status_allowed?(work_package)
- @change_status_cache ||= Hash.new do |hash, project|
- hash[project] = user.allowed_in_project?(%i[edit_work_packages change_work_package_status], work_package.project)
- end
-
- @change_status_cache[work_package.project]
- end
-end
diff --git a/spec/contracts/work_packages/create_note_contract_spec.rb b/spec/contracts/work_packages/create_note_contract_spec.rb
index eeacb2dabb7..57846d03fb0 100644
--- a/spec/contracts/work_packages/create_note_contract_spec.rb
+++ b/spec/contracts/work_packages/create_note_contract_spec.rb
@@ -28,11 +28,15 @@
# See COPYRIGHT and LICENSE files for more details.
require "spec_helper"
+require "contracts/shared/model_contract_shared_context"
RSpec.describe WorkPackages::CreateNoteContract do
+ include_context "ModelContract shared context"
+
+ let(:project) { build_stubbed(:project) }
let(:work_package) do
# As we only want to test the contract, we mock checking whether the work_package is valid
- wp = build_stubbed(:work_package)
+ wp = build_stubbed(:work_package, project:)
# we need to clear the changes information because otherwise the
# contract will complain about all the changes to read_only attributes
wp.send(:clear_changes_information)
@@ -41,99 +45,83 @@ RSpec.describe WorkPackages::CreateNoteContract do
wp
end
let(:user) { build_stubbed(:user) }
- let(:policy_instance) { instance_double(WorkPackagePolicy) }
-
- subject(:contract) do
- contract = described_class.new(work_package, user)
+ let(:permissions) { %i[add_work_package_notes] }
- contract.send(:"policy=", policy_instance)
-
- contract
+ before do
+ mock_permissions_for(user) do |mock|
+ mock.allow_in_project(*permissions, project:)
+ end
end
- describe "note" do
- before do
- work_package.journal_notes = "blubs"
- end
+ subject(:contract) do
+ described_class.new(work_package, user)
+ end
- context "if the user has the permissions" do
+ describe "validations" do
+ describe "journal_notes" do
before do
- allow(policy_instance).to receive(:allowed?).with(work_package, :comment).and_return true
-
- contract.validate
+ work_package.journal_notes = "blubs"
end
- it("is valid") { expect(contract.errors).to be_empty }
- end
-
- context "if the user lacks the permissions" do
- before do
- allow(policy_instance).to receive(:allowed?).with(work_package, :comment).and_return false
+ context "if the user has only the add_work_package_notes permission" do
+ let(:permissions) { %i[add_work_package_notes] }
- contract.validate
+ it_behaves_like "contract is valid"
end
- it "is invalid" do
- expect(contract.errors.symbols_for(:journal_notes))
- .to contain_exactly(:error_unauthorized)
+ context "if the user has only the edit_work_packages permission" do
+ let(:permissions) { %i[edit_work_packages] }
+
+ it_behaves_like "contract is valid"
end
- end
- end
- describe "journal_restricted" do
- before do
- allow(policy_instance).to receive(:allowed?).and_return(true)
+ context "if the user lacks the permissions" do
+ let(:permissions) { [] }
+
+ it_behaves_like "contract is invalid", journal_notes: :error_unauthorized
+ end
end
- context "with a blank note" do
+ describe "journal_restricted" do
before do
- work_package.journal_notes = ""
- work_package.journal_restricted = true
-
- contract.validate
+ # Setting the journal_notes to not trigger a :blank error
+ work_package.journal_notes = "blubs"
end
context "and journal_restricted is true, and comments_with_restricted_visibility_active? is disabled",
with_flag: { comments_with_restricted_visibility_active: false } do
- it "is invalid" do
- expect(contract.errors.full_messages).to eq(["Comment can't be blank.", "Restricted Journal is not available."])
+ before do
+ work_package.journal_restricted = true
end
+
+ it_behaves_like "contract is invalid", journal_restricted: :feature_disabled
end
context "and journal_restricted is true, and comments_with_restricted_visibility_active? is enabled",
with_flag: { comments_with_restricted_visibility_active: true } do
- it "is invalid" do
- expect(contract.errors.full_messages).to eq(["Comment can't be blank."])
+ before do
+ work_package.journal_restricted = true
end
+
+ it_behaves_like "contract is valid"
end
- end
- context "with a note" do
- context "and journal_restricted is true", with_flag: { comments_with_restricted_visibility_active: true } do
+ context "and journal_restricted is false, and comments_with_restricted_visibility_active? is disabled",
+ with_flag: { comments_with_restricted_visibility_active: false } do
before do
- work_package.journal_notes = "blubs"
- work_package.journal_restricted = true
-
- contract.validate
+ work_package.journal_restricted = false
end
- it("is valid") { expect(contract.errors).to be_empty }
+ it_behaves_like "contract is valid"
end
end
- end
-
- describe "subject" do
- before do
- work_package.subject = "blubs"
- allow(policy_instance).to receive(:allowed?).and_return true
-
- contract.validate
- end
+ describe "another attribute of work package" do
+ before do
+ work_package.subject = "blubs"
+ end
- it "is invalid" do
- expect(contract.errors.symbols_for(:subject))
- .to contain_exactly(:error_readonly)
+ it_behaves_like "contract is invalid", subject: :error_readonly
end
end
end
diff --git a/spec/contracts/work_packages/shared_base_contract.rb b/spec/contracts/work_packages/shared_base_contract.rb
index bd987abaa10..7e02af9c51c 100644
--- a/spec/contracts/work_packages/shared_base_contract.rb
+++ b/spec/contracts/work_packages/shared_base_contract.rb
@@ -29,7 +29,6 @@
RSpec.shared_examples_for "work package contract" do
let(:user) { build_stubbed(:user) }
let(:other_user) { build_stubbed(:user) }
- let(:policy) { double(WorkPackagePolicy, allowed?: true) }
subject(:contract) { described_class.new(work_package, user) }
@@ -39,12 +38,6 @@ RSpec.shared_examples_for "work package contract" do
contract
end
- before do
- allow(WorkPackagePolicy)
- .to receive(:new)
- .and_return(policy)
- end
-
let(:possible_assignees) { [] }
let!(:assignable_assignees_scope) do
scope = double "assignable assignees scope"
diff --git a/spec/policies/work_package_policy_spec.rb b/spec/policies/work_package_policy_spec.rb
deleted file mode 100644
index abc4a8d4f05..00000000000
--- a/spec/policies/work_package_policy_spec.rb
+++ /dev/null
@@ -1,127 +0,0 @@
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) 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 WorkPackagePolicy, type: :controller do
- let(:user) { build_stubbed(:user) }
- let(:project) { build_stubbed(:project) }
- let(:work_package) { build_stubbed(:work_package, project:) }
-
- describe "#allowed?" do
- let(:subject) { described_class.new(user) }
-
- context "for edit" do
- it "is false if the user has no permissions" do
- mock_permissions_for(user, &:forbid_everything)
- expect(subject).not_to be_allowed(work_package, :edit)
- end
-
- it "is true if the user has the edit_work_package permission" do
- mock_permissions_for(user) do |mock|
- mock.allow_in_project :edit_work_packages, project:
- end
-
- expect(subject).to be_allowed(work_package, :edit)
- end
-
- it "is true if the user has the edit_work_package permission on the work packge" do
- mock_permissions_for(user) do |mock|
- mock.allow_in_work_package :edit_work_packages, work_package:
- end
-
- expect(subject).to be_allowed(work_package, :edit)
- end
-
- it "is false if the user has only the add_work_package_notes permission" do
- mock_permissions_for(user) do |mock|
- mock.allow_in_project :add_work_package_notes, project:
- end
-
- expect(subject).not_to be_allowed(work_package, :edit)
- end
-
- it "is false if the user has the permissions but the work package is unpersisted" do
- mock_permissions_for(user) do |mock|
- mock.allow_in_project :edit_work_packages, :add_work_package_notes, project:
- end
-
- allow(work_package).to receive(:persisted?).and_return false
-
- expect(subject).not_to be_allowed(work_package, :edit)
- end
- end
-
- context "for manage_subtasks" do
- it "is true if the user has the manage_subtasks permission in the project" do
- mock_permissions_for(user) do |mock|
- mock.allow_in_project :manage_subtasks, project:
- end
-
- expect(subject).to be_allowed(work_package, :manage_subtasks)
- end
- end
-
- context "for comment" do
- it "is false if the user lacks permission" do
- expect(subject).not_to be_allowed(work_package, :comment)
- end
-
- it "is true if the user has the add_work_package_notes permission" do
- mock_permissions_for(user) do |mock|
- mock.allow_in_project :add_work_package_notes, project:
- end
- expect(subject).to be_allowed(work_package, :comment)
- end
-
- it "is true if the user has the add_work_package_notes permission on the work package" do
- mock_permissions_for(user) do |mock|
- mock.allow_in_work_package :add_work_package_notes, work_package:
- end
- expect(subject).to be_allowed(work_package, :comment)
- end
-
- it "is true if the user has the edit_work_packages permission" do
- mock_permissions_for(user) do |mock|
- mock.allow_in_project :edit_work_packages, project:
- end
-
- expect(subject).to be_allowed(work_package, :comment)
- end
-
- it "is false if the user has the edit_work_packages permission but the work_package is unpersisted" do
- mock_permissions_for(user) do |mock|
- mock.allow_in_project :edit_work_packages, project:
- end
- allow(work_package).to receive(:persisted?).and_return false
-
- expect(subject).not_to be_allowed(work_package, :comment)
- end
- end
- end
-end