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
fix contracts
Klaus Zanders4f85b450c845
diff --git a/app/models/work_packages/costs.rb b/app/models/work_packages/costs.rbindex 617aa805410..2ac797d5903 100644--- a/app/models/work_packages/costs.rb+++ b/app/models/work_packages/costs.rb@@ -31,7 +31,7 @@ module WorkPackages::Costsincluded dobelongs_to :budget, inverse_of: :work_packages, optional: true- has_many :cost_entries, dependent: :delete_all+ has_many :cost_entries, dependent: :delete_all, inverse_of: :entity# disabled for now, implements part of ticket blockingvalidate :validate_budget@@ -82,7 +82,7 @@ module WorkPackages::Costsreturn unless saved_change_to_project_id?CostEntry- .where(work_package_id: id)+ .where(entity: self).update_all(project_id:)endend@@ -125,13 +125,13 @@ module WorkPackages::Costsfalseelse- condition = "work_package_id = #{reassign_to.id}, project_id = #{reassign_to.project_id}"- ::WorkPackage.update_cost_entries(work_packages.map(&:id), condition)+ condition = "entity_type='WorkPackage', entity_id = #{reassign_to.id}, project_id = #{reassign_to.project_id}"+ ::WorkPackage.update_cost_entries(work_packages, condition)endenddef update_cost_entries(work_packages, action)- CostEntry.where(work_package_id: work_packages).update_all(action)+ CostEntry.where(entity: work_packages).update_all(action)endendenddiff --git a/modules/costs/app/components/time_entries/work_package_form.rb b/modules/costs/app/components/time_entries/entity_form.rbsimilarity index 95%rename from modules/costs/app/components/time_entries/work_package_form.rbrename to modules/costs/app/components/time_entries/entity_form.rbindex e0d460237f1..ec53256469c 100644--- a/modules/costs/app/components/time_entries/work_package_form.rb+++ b/modules/costs/app/components/time_entries/entity_form.rb@@ -29,7 +29,7 @@#++module TimeEntries- class WorkPackageForm < ApplicationForm+ class EntityForm < ApplicationFormdef initialize(visible: true, limit_to_project_id: nil)super()@visible = visible@@ -39,9 +39,11 @@ module TimeEntriesform do |f|f.hidden name: :show_work_package, value: @visiblef.hidden name: :limit_to_project_id, value: @limit_to_project_id+ # TODO: Use global ID in the autocompleter+ f.hidden name: :entity_type, value: "WorkPackage"if show_work_package_field?- f.work_package_autocompleter name: :work_package_id,+ f.work_package_autocompleter name: :entity_id,label: TimeEntry.human_attribute_name(:work_package),required: work_package_required?,validation_message: work_package_validation_error,diff --git a/modules/costs/app/components/time_entries/time_entry_form_component.html.erb b/modules/costs/app/components/time_entries/time_entry_form_component.html.erbindex 58cdec812cf..7f677c8015b 100644--- a/modules/costs/app/components/time_entries/time_entry_form_component.html.erb+++ b/modules/costs/app/components/time_entries/time_entry_form_component.html.erb@@ -36,7 +36,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.body.with_row { render(TimeEntries::DaysAndHoursForm.new(form)) }body.with_row(display: show_work_package ? :block : :none) dorender(- TimeEntries::WorkPackageForm.new(form, visible: show_work_package, limit_to_project_id: limit_to_project_id)+ TimeEntries::EntityForm.new(form, visible: show_work_package, limit_to_project_id: limit_to_project_id))endbody.with_row { render(TimeEntries::ActivityForm.new(form)) }diff --git a/modules/costs/app/contracts/time_entries/base_contract.rb b/modules/costs/app/contracts/time_entries/base_contract.rbindex 6bb5e6b6d1b..adaf2400124 100644--- a/modules/costs/app/contracts/time_entries/base_contract.rb+++ b/modules/costs/app/contracts/time_entries/base_contract.rb@@ -33,7 +33,7 @@ module TimeEntriesinclude AssignableValuesContractinclude AssignableCustomFieldValues- delegate :work_package,+ delegate :entity,:project,:available_custom_fields,:new_record?,@@ -54,7 +54,9 @@ module TimeEntriesunless: Proc.new { spent_on.blank? }attribute :project_id- attribute :work_package_id+ attribute :entity+ attribute :entity_id+ attribute :entity_typeattribute :activity_id dovalidate_activity_activeend@@ -85,21 +87,18 @@ module TimeEntries# Necessary for custom fields of type version.def assignable_versions(only_open: true)- work_package.try(:assignable_versions, only_open:) || project.try(:assignable_versions, only_open:) || []+ entity.try(:assignable_versions, only_open:) || project.try(:assignable_versions, only_open:) || []endprivatedef validate_entity- return unless model.entity || model.entity_changed?-if model.entity.is_a?(WorkPackage)if work_package_invisible? || work_package_not_in_project?errors.add :entity, :invalidend- elsif model.entity.is_a?(TimeEntry)- # TODO: Add validation for time entry- true+ elsif model.entity.is_a?(Meeting)+ # TODO: Add validation for meetingendenddiff --git a/modules/costs/app/contracts/time_entries/create_contract.rb b/modules/costs/app/contracts/time_entries/create_contract.rbindex 8aa141a151d..cfaeeca9900 100644--- a/modules/costs/app/contracts/time_entries/create_contract.rb+++ b/modules/costs/app/contracts/time_entries/create_contract.rb@@ -37,7 +37,7 @@ module TimeEntriesreturn if allowed_to_log_for_others?return if allowed_to_log_to_himself?- errors.add :work_package, :cannot_log_for_this_work_package+ errors.add :entity, :cannot_log_for_this_work_packageerrors.add :base, :error_unauthorizedend@@ -46,7 +46,15 @@ module TimeEntriesenddef allowed_to_log_to_himself?- model.user == user && user.allowed_in_work_package?(:log_own_time, model.work_package)+ model.user == user && allowed_to_log_own?+ end++ def allowed_to_log_own?+ case model.entity+ when WorkPackage then user.allowed_in_work_package?(:log_own_time, model.entity)+ when Meeting then user.allowed_in_project?(:log_own_time, model.project)+ else false+ endendendenddiff --git a/modules/costs/app/contracts/time_entries/delete_contract.rb b/modules/costs/app/contracts/time_entries/delete_contract.rbindex 23ff697b2f5..7efa66e810f 100644--- a/modules/costs/app/contracts/time_entries/delete_contract.rb+++ b/modules/costs/app/contracts/time_entries/delete_contract.rb@@ -30,8 +30,17 @@ module TimeEntriesclass DeleteContract < ::DeleteContractdelete_permission -> {edit_all = user.allowed_in_project?(:edit_time_entries, model.project)- edit_own = user.allowed_in_work_package?(:edit_own_time_entries, model.work_package)- edit_ongoing = model.ongoing && user.allowed_in_work_package?(:log_own_time, model.work_package)+ edit_own = if model.entity.is_a?(WorkPackage)+ user.allowed_in_work_package?(:edit_own_time_entries, model.entity)+ else+ user.allowed_in_project?(:edit_own_time_entries, model.project)+ end+ edit_ongoing = if model.entity.is_a?(WorkPackage)+ model.ongoing && user.allowed_in_work_package?(:log_own_time, model.entity)+ else+ # TODO: Ongoing on meeting?+ false+ endif model.user == useredit_own || edit_all || edit_ongoingdiff --git a/modules/costs/app/contracts/time_entries/update_contract.rb b/modules/costs/app/contracts/time_entries/update_contract.rbindex 7084f2ddd88..1dbb41bd739 100644--- a/modules/costs/app/contracts/time_entries/update_contract.rb+++ b/modules/costs/app/contracts/time_entries/update_contract.rb@@ -57,15 +57,25 @@ module TimeEntriesprivatedef user_allowed_to_update_in?(time_entry)- user.allowed_in_project?(:edit_time_entries, time_entry.project) ||- (model.user == user && user.allowed_in_work_package?(:edit_own_time_entries, time_entry.work_package))+ user.allowed_in_project?(:edit_time_entries, time_entry.project) || (model.user == user && edit_own?(time_entry))enddef user_allowed_to_modify_ongoing?(time_entry)- model.user == user && (- user.allowed_in_project?(:log_time, time_entry.project) ||- user.allowed_in_work_package?(:log_own_time, time_entry.work_package)- )+ model.user == user && (user.allowed_in_project?(:log_time, time_entry.project) || log_own?(time_entry))+ end++ def edit_own?(time_entry)+ case time_entry.entity+ when WorkPackage then user.allowed_in_work_package?(:edit_own_time_entries, time_entry.entity)+ when Meeting then user.allowed_in_project?(:edit_own_time_entries, time_entry.project)+ end+ end++ def log_own?(time_entry)+ case time_entry.entity+ when WorkPackage then user.allowed_in_work_package?(:log_own_time, time_entry.entity)+ when Meeting then user.allowed_in_project?(:log_own_time, time_entry.project)+ endendendenddiff --git a/modules/costs/lib/costs/patches/permitted_params_patch.rb b/modules/costs/lib/costs/patches/permitted_params_patch.rbindex 6a770885345..1783584042d 100644--- a/modules/costs/lib/costs/patches/permitted_params_patch.rb+++ b/modules/costs/lib/costs/patches/permitted_params_patch.rb@@ -77,7 +77,8 @@ module Costs::Patches::PermittedParamsPatch:hours,:comments,:spent_on,- :work_package_id,+ :entity_type,+ :entity_id,:activity_id,:project_id,:issue_id,diff --git a/modules/costs/spec/contracts/time_entries/create_contract_spec.rb b/modules/costs/spec/contracts/time_entries/create_contract_spec.rbindex 8f189c446ad..df2379d5ba5 100644--- a/modules/costs/spec/contracts/time_entries/create_contract_spec.rb+++ b/modules/costs/spec/contracts/time_entries/create_contract_spec.rb@@ -37,7 +37,7 @@ RSpec.describe TimeEntries::CreateContract dolet(:time_entry) doTimeEntry.new(project: time_entry_project,- work_package: time_entry_work_package,+ entity: time_entry_entity,user: time_entry_user,activity: time_entry_activity,spent_on: time_entry_spent_on,@@ -63,7 +63,7 @@ RSpec.describe TimeEntries::CreateContract doit "is invalid" doexpect_valid(false,- work_package: %i(cannot_log_for_this_work_package),+ entity: %i(cannot_log_for_this_work_package),base: %i(error_unauthorized))endend@@ -90,7 +90,7 @@ RSpec.describe TimeEntries::CreateContract doit "is invalid" doexpect_valid(false,- work_package: %i(cannot_log_for_this_work_package),+ entity: %i(cannot_log_for_this_work_package),base: %i(error_unauthorized))endend@@ -109,7 +109,7 @@ RSpec.describe TimeEntries::CreateContract doit "is invalid" doexpect_valid(false,- work_package: %i(cannot_log_for_this_work_package),+ entity: %i(cannot_log_for_this_work_package),base: %i(error_unauthorized))endend@@ -136,7 +136,7 @@ RSpec.describe TimeEntries::CreateContract doit "is invalid" doexpect_valid(false,- work_package: %i(cannot_log_for_this_work_package),+ entity: %i(cannot_log_for_this_work_package),base: %i(error_unauthorized))endenddiff --git a/modules/costs/spec/contracts/time_entries/delete_contract_spec.rb b/modules/costs/spec/contracts/time_entries/delete_contract_spec.rbindex 30a25890992..0bba14c6291 100644--- a/modules/costs/spec/contracts/time_entries/delete_contract_spec.rb+++ b/modules/costs/spec/contracts/time_entries/delete_contract_spec.rb@@ -31,7 +31,7 @@ require "spec_helper"RSpec.describe TimeEntries::DeleteContract dolet(:current_user) { build_stubbed(:user) }let(:other_user) { build_stubbed(:user) }- let(:time_entry_work_package) { build_stubbed(:work_package, project: time_entry_project) }+ let(:time_entry_entity) { build_stubbed(:work_package, project: time_entry_project) }let(:time_entry_project) { build_stubbed(:project) }let(:time_entry_user) { current_user }let(:time_entry_activity) { build_stubbed(:time_entry_activity) }@@ -44,7 +44,7 @@ RSpec.describe TimeEntries::DeleteContract dolet(:time_entry) dobuild_stubbed(:time_entry,project: time_entry_project,- work_package: time_entry_work_package,+ entity: time_entry_entity,user: time_entry_user,ongoing: time_entry_ongoing,activity: time_entry_activity,@@ -58,7 +58,7 @@ RSpec.describe TimeEntries::DeleteContract domock.allow_in_project *permissions, project: time_entry_projectend- allow(time_entry_work_package)+ allow(time_entry_entity).to receive(:visible?).with(current_user).and_return(work_package_visible)diff --git a/modules/costs/spec/contracts/time_entries/shared_contract_examples.rb b/modules/costs/spec/contracts/time_entries/shared_contract_examples.rbindex bba2f60b038..02358bd2bdd 100644--- a/modules/costs/spec/contracts/time_entries/shared_contract_examples.rb+++ b/modules/costs/spec/contracts/time_entries/shared_contract_examples.rb@@ -37,15 +37,10 @@ RSpec.shared_examples_for "time entry contract" doendlet(:other_user) { build_stubbed(:user) }- let(:time_entry_work_package) do- build_stubbed(:work_package,- project: time_entry_project)- end+ let(:time_entry_entity) { build_stubbed(:work_package, project: time_entry_project) }let(:time_entry_project) { build_stubbed(:project) }let(:time_entry_user) { current_user }- let(:time_entry_activity) do- build_stubbed(:time_entry_activity)- end+ let(:time_entry_activity) { build_stubbed(:time_entry_activity) }let(:time_entry_activity_active) { true }let(:time_entry_spent_on) { Time.zone.today }let(:time_entry_hours) { 5 }@@ -68,8 +63,8 @@ RSpec.shared_examples_for "time entry contract" doendbefore do- if time_entry_work_package- allow(time_entry_work_package)+ if time_entry_entity+ allow(time_entry_entity).to receive(:visible?).with(current_user).and_return(work_package_visible)@@ -120,17 +115,20 @@ RSpec.shared_examples_for "time entry contract" doit_behaves_like "is valid"context "when the work_package is within a different project than the provided project" do- let(:time_entry_work_package) { build_stubbed(:work_package) }+ let(:another_project) { build_stubbed(:project) }+ let(:time_entry_entity) { build_stubbed(:work_package, project: another_project) }it "is invalid" do- expect_valid(false, work_package_id: %i(invalid))+ expect_valid(false, entity: %i(invalid))endendcontext "when the work_package is nil" do- let(:time_entry_work_package) { nil }+ let(:time_entry_entity) { nil }- it_behaves_like "is valid"+ it "is invalid" do+ expect_valid(false, entity: %i(blank))+ endendcontext "when the project is nil" do@@ -269,8 +267,8 @@ RSpec.shared_examples_for "time entry contract" do.and_return project_versionsend- if time_entry_work_package- allow(time_entry_work_package)+ if time_entry_entity+ allow(time_entry_entity).to receive(:assignable_versions).and_return wp_versionsend@@ -278,7 +276,7 @@ RSpec.shared_examples_for "time entry contract" docontext "if no project and no work package is set" dolet(:time_entry_project) { nil }- let(:time_entry_work_package) { nil }+ let(:time_entry_entity) { nil }it "is empty" doexpect(contract.assignable_versions)@@ -287,7 +285,7 @@ RSpec.shared_examples_for "time entry contract" doendcontext "if a project is set but no work package" do- let(:time_entry_work_package) { nil }+ let(:time_entry_entity) { nil }it "returns assignable_versions of the project" doexpect(contract.assignable_versions)diff --git a/modules/costs/spec/contracts/time_entries/update_contract_spec.rb b/modules/costs/spec/contracts/time_entries/update_contract_spec.rbindex db5ebc848fb..a3d6193dca1 100644--- a/modules/costs/spec/contracts/time_entries/update_contract_spec.rb+++ b/modules/costs/spec/contracts/time_entries/update_contract_spec.rb@@ -34,7 +34,7 @@ RSpec.describe TimeEntries::UpdateContract dolet(:time_entry) dobuild_stubbed(:time_entry,project: time_entry_project,- work_package: time_entry_work_package,+ entity: time_entry_entity,user: time_entry_user,activity: time_entry_activity,spent_on: time_entry_spent_on,@@ -73,11 +73,11 @@ RSpec.describe TimeEntries::UpdateContract doendend- time_entry_work_package.project = new_project+ time_entry_entity.project = new_projectmock_permissions_for(current_user) do |mock|- mock.allow_in_project *new_project_permissions, project: new_project- mock.allow_in_project *permissions, project: time_entry_project+ mock.allow_in_project(*new_project_permissions, project: new_project)+ mock.allow_in_project(*permissions, project: time_entry_project)endendenddiff --git a/modules/costs/spec/factories/cost_entry_factory.rb b/modules/costs/spec/factories/cost_entry_factory.rbindex 7620571be93..32c86bb81f3 100644--- a/modules/costs/spec/factories/cost_entry_factory.rb+++ b/modules/costs/spec/factories/cost_entry_factory.rb@@ -32,7 +32,7 @@ FactoryBot.define douserentity factory: :work_packagecost_type- spent_on { Date.today }+ spent_on { Date.current }units { 1 }comments { "" }