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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
validation and default values on share creation
ulferts76bbdfd379b4
diff --git a/app/contracts/work_package_members/base_contract.rb b/app/contracts/work_package_members/base_contract.rbnew file mode 100644index 00000000000..9b88db121eb--- /dev/null+++ b/app/contracts/work_package_members/base_contract.rb@@ -0,0 +1,77 @@+# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2010-2023 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.+# ++++module WorkPackageMembers+ class BaseContract < ::ModelContract+ delegate :project,+ to: :model++ attribute :roles++ validate :user_allowed_to_manage+ validate :role_grantable+ validate :single_role+ validate :principal_assignable+ validate :project_set++ private++ def user_allowed_to_manage+ errors.add :base, :error_unauthorized unless user_allowed_to_manage?+ end++ def user_allowed_to_manage?+ user.allowed_to?(:share_work_packages,+ model.project)+ end++ def single_role+ errors.add(:roles, :more_than_one) if active_roles.count > 1+ end++ def role_grantable+ errors.add(:roles, :ungrantable) unless active_roles.all? { _1.is_a?(WorkPackageRole) }+ end++ def principal_assignable+ return if principal.nil?++ if principal.builtin? || principal.locked?+ errors.add(:principal, :unassignable)+ end+ end++ def project_set+ errors.add(:project, :blank) if project.nil?+ end++ def active_roles+ model.member_roles.reject(&:marked_for_destruction?).map(&:role)+ end+ end+enddiff --git a/app/contracts/work_package_members/create_contract.rb b/app/contracts/work_package_members/create_contract.rbnew file mode 100644index 00000000000..8827de669ed--- /dev/null+++ b/app/contracts/work_package_members/create_contract.rb@@ -0,0 +1,36 @@+# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2010-2023 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.+# ++++module WorkPackageMembers+ class CreateContract < BaseContract+ attribute :principal+ attribute :entity_id+ attribute :entity_type+ attribute :user_id+ end+enddiff --git a/app/controllers/work_packages/shares_controller.rb b/app/controllers/work_packages/shares_controller.rbindex 5dfcc2aa644..a3c2c930c61 100644--- a/app/controllers/work_packages/shares_controller.rb+++ b/app/controllers/work_packages/shares_controller.rb@@ -38,10 +38,12 @@ class WorkPackages::SharesController < ApplicationControllerenddef create- # Todo: Role selection, validation, error handling?- Member.create(entity: @work_package,- user_id: params[:member][:user_id],- roles: Role.where(builtin: Role::BUILTIN_WORK_PACKAGE_VIEWER))+ # Todo: Role selection, error handling?+ WorkPackageMembers::CreateService+ .new(user: current_user)+ .call(entity: @work_package,+ user_id: params[:member][:user_id],+ roles: Role.where(builtin: Role::BUILTIN_WORK_PACKAGE_VIEWER))replace_via_turbo_stream(component: WorkPackages::Share::ModalComponent.new(work_package: @work_package)diff --git a/app/services/work_package_members/create_service.rb b/app/services/work_package_members/create_service.rbnew file mode 100644index 00000000000..3cc5ac68d8c--- /dev/null+++ b/app/services/work_package_members/create_service.rb@@ -0,0 +1,35 @@+# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2010-2023 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 WorkPackageMembers::CreateService < BaseServices::Create+ private++ def instance_class+ Member+ end+enddiff --git a/app/services/work_package_members/set_attributes_service.rb b/app/services/work_package_members/set_attributes_service.rbnew file mode 100644index 00000000000..af27502afdb--- /dev/null+++ b/app/services/work_package_members/set_attributes_service.rb@@ -0,0 +1,41 @@+# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2010-2023 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.+# ++++module WorkPackageMembers+ class SetAttributesService < ::BaseServices::SetAttributes+ private++ def set_attributes(params)+ super++ model.change_by_system do+ model.project = model.entity&.project+ end+ end+ end+enddiff --git a/config/locales/en.yml b/config/locales/en.ymlindex adfa627cc6c..c385c8c4793 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -979,6 +979,7 @@ en:attributes:roles:ungrantable: "has an unassignable role."+ more_than_one: "has more than one role."principal:unassignable: "cannot be assigned to a project."version:diff --git a/spec/contracts/work_package_members/create_contract_spec.rb b/spec/contracts/work_package_members/create_contract_spec.rbnew file mode 100644index 00000000000..fbb48b27022--- /dev/null+++ b/spec/contracts/work_package_members/create_contract_spec.rb@@ -0,0 +1,51 @@+# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2010-2023 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 'shared_contract_examples'++RSpec.describe WorkPackageMembers::CreateContract do+ include_context 'ModelContract shared context'++ it_behaves_like 'work package member contract' do+ let(:member) do+ Member.new(roles: member_roles,+ principal: member_principal,+ entity: member_entity) do |member|+ member.extend(OpenProject::ChangedBySystem)++ # Done in the SetAttributesService+ member.change_by_system do+ member.project = member_project+ end+ end+ end++ let(:contract) { described_class.new(member, current_user) }+ end+enddiff --git a/spec/contracts/work_package_members/shared_contract_examples.rb b/spec/contracts/work_package_members/shared_contract_examples.rbnew file mode 100644index 00000000000..de2eade1c71--- /dev/null+++ b/spec/contracts/work_package_members/shared_contract_examples.rb@@ -0,0 +1,123 @@+# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2010-2023 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 'contracts/shared/model_contract_shared_context'++RSpec.shared_examples_for 'work package member contract' do+ let(:current_user) do+ build_stubbed(:user, admin: current_user_admin) do |user|+ allow(user)+ .to receive(:allowed_to?) do |permission, permission_project|+ permissions.include?(permission) && member_project == permission_project+ end+ end+ end+ let(:member_entity) do+ build_stubbed(:work_package)+ end+ let(:member_project) do+ member_entity.project+ end+ let(:member_roles) do+ [role]+ end+ let(:member_principal) do+ build_stubbed(:user)+ end+ let(:role) do+ build_stubbed(:view_work_package_role)+ end+ let(:permissions) { [:share_work_packages] }+ let(:current_user_admin) { false }++ describe 'validation' do+ it_behaves_like 'contract is valid'++ context 'if the principal is nil' do+ let(:member_principal) { nil }++ it_behaves_like 'contract is invalid', principal: :blank+ end++ context 'if the principal is a builtin user' do+ let(:member_principal) { build_stubbed(:anonymous) }++ it_behaves_like 'contract is invalid', principal: :unassignable+ end++ context 'if the principal is a locked user' do+ let(:member_principal) { build_stubbed(:locked_user) }++ it_behaves_like 'contract is invalid', principal: :unassignable+ end++ context 'if the roles are nil' do+ let(:member_roles) { [] }++ it_behaves_like 'contract is invalid', roles: :role_blank+ end++ context 'if any role is not assignable (e.g. builtin)' do+ let(:member_roles) do+ [build_stubbed(:role)]+ end++ it_behaves_like 'contract is invalid', roles: :ungrantable+ end++ context 'if more than one role is assigned' do+ let(:member_roles) do+ [build_stubbed(:view_work_package_role), build_stubbed(:comment_work_package_role)]+ end++ it_behaves_like 'contract is invalid', roles: :more_than_one+ end++ context 'if the user lacks :share_work_packages permission in the project' do+ # This permission would work for a project member.+ let(:permissions) { [:manage_members] }++ it_behaves_like 'contract is invalid', base: :error_unauthorized+ end++ context 'if the project is nil' do+ let(:member_project) { nil }++ it_behaves_like 'contract is invalid', project: :blank+ end++ context 'if the project is not set by the system' do+ before do+ member.project = build_stubbed(:project)+ end++ it_behaves_like 'contract is invalid', project_id: :error_readonly+ end+ end+enddiff --git a/spec/factories/member_factory.rb b/spec/factories/member_factory.rbindex 1e2a272c1bb..45fc7c9722a 100644--- a/spec/factories/member_factory.rb+++ b/spec/factories/member_factory.rb@@ -56,4 +56,9 @@ FactoryBot.define doproject { nil }entity { nil }end++ factory :work_package_member, parent: :member do+ entity factory: %i[work_package]+ project { entity.project }+ endenddiff --git a/spec/services/work_package_members/create_service_spec.rb b/spec/services/work_package_members/create_service_spec.rbnew file mode 100644index 00000000000..f6ff70d372e--- /dev/null+++ b/spec/services/work_package_members/create_service_spec.rb@@ -0,0 +1,48 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2023 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 'services/base_services/behaves_like_create_service'++RSpec.describe WorkPackageMembers::CreateService do+ let(:user) { build_stubbed(:user) }+ let(:role) { build_stubbed(:view_work_package_role) }+ let(:work_package) { build_stubbed(:work_package) }+ let(:instance) { described_class.new(user:) }++ let(:params) { { principal: user, roles: [role], entity: work_package, project: work_package.project } }++ subject(:service_call) { instance.call(params) }++ it_behaves_like 'BaseServices create service' do+ let(:model_class) { Member }+ let(:call_attributes) { { principal: user, roles: [role], entity: work_package, project: work_package.project } }+ end+enddiff --git a/spec/services/work_package_members/set_attributes_service_spec.rb b/spec/services/work_package_members/set_attributes_service_spec.rbnew file mode 100644index 00000000000..4a6404843ba--- /dev/null+++ b/spec/services/work_package_members/set_attributes_service_spec.rb@@ -0,0 +1,146 @@+# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2010-2023 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 WorkPackageMembers::SetAttributesService, type: :model do+ let(:user) { build_stubbed(:user) }+ let(:work_package) { build_stubbed(:work_package) }+ let(:member) do+ new_member+ end+ let(:new_member) { Member.new }+ let(:existing_member) { build_stubbed(:work_package_member) }++ let(:contract_class) do+ allow(WorkPackageMembers::CreateContract)+ .to receive(:new)+ .with(member, user, options: {})+ .and_return(contract_instance)++ WorkPackageMembers::CreateContract+ end+ let(:contract_instance) do+ instance_double(WorkPackageMembers::CreateContract, validate: contract_valid, errors: contract_errors)+ end+ let(:contract_valid) { true }+ let(:contract_errors) do+ instance_double(ActiveModel::Errors)+ end+ let(:instance) do+ described_class.new(user:,+ model: member,+ contract_class:)+ end++ describe 'call' do+ let(:call_attributes) do+ {+ user_id: 3,+ entity: work_package+ }+ end++ before do+ allow(contract_instance)+ .to receive(:validate)+ .and_return(contract_valid)++ allow(member)+ .to receive(:save)+ .and_return true+ end++ subject { instance.call(call_attributes) }++ it 'is successful' do+ expect(subject).to be_success+ end++ it 'does not persist the member' do+ subject++ expect(member)+ .not_to have_received(:save)+ end++ context 'for a new record' do+ it 'sets the attributes and also takes the project_id from the work package' do+ subject++ expect(member.attributes.slice(*member.changed).symbolize_keys)+ .to eql(user_id: 3, entity_id: work_package.id, entity_type: 'WorkPackage', project_id: work_package.project_id)+ end++ it 'marks the project_id to be changed by the system' do+ subject++ expect(member.changed_by_system)+ .to eql('project_id' => [nil, member.project_id])+ end+ end++ # Changing the entity should not really happen in reality but if it does, this is what happens.+ context 'for a persisted record' do+ let(:member) { existing_member }++ it 'sets the attributes and also takes the project_id from the work package' do+ subject++ expect(member.attributes.slice(*member.changed).symbolize_keys)+ .to eql(user_id: 3, entity_id: work_package.id, project_id: work_package.project_id)+ end++ it 'marks the project_id to be changed by the system' do+ subject++ expect(member.changed_by_system)+ .to eql('project_id' => [member.project_id_was, member.project_id])+ end+ end++ context 'if the contract is invalid' do+ let(:contract_valid) { false }++ it 'is unsuccessful' do+ expect(subject).not_to be_success+ end++ it 'returns the errors of the contract' do+ expect(subject.errors).to eql contract_errors+ end++ it 'does not persist the member' do+ subject++ expect(member)+ .not_to have_received(:save)+ end+ end+ end+end