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

Add optimistic locking to meetings and agenda items

Oliver Güntherad03c52cfb0b
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 3e69e844065..e9b3c89a1ce 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -706,6 +706,7 @@ en:
error_enterprise_only: "%{action} is only available in the OpenProject Enterprise edition"
error_unauthorized: "may not be accessed."
error_readonly: "was attempted to be written but is not writable."
+ error_conflict: "Information has been updated by at least one other user in the meantime."
email: "is not a valid email address."
empty: "can't be empty."
even: "must be even."
diff --git a/db/migrate/20231119192222_add_locking_to_meeting.rb b/db/migrate/20231119192222_add_locking_to_meeting.rb
new file mode 100644
index 00000000000..e113c0bce58
--- /dev/null
+++ b/db/migrate/20231119192222_add_locking_to_meeting.rb
@@ -0,0 +1,6 @@
+class AddLockingToMeeting < ActiveRecord::Migration[7.0]
+ def change
+ add_column :meetings, :lock_version, :integer, default: 0, null: false
+ add_column :meeting_agenda_items, :lock_version, :integer, default: 0, null: false
+ end
+end
diff --git a/modules/meeting/app/components/base_errors_component.rb b/modules/meeting/app/components/base_errors_component.rb
new file mode 100644
index 00000000000..af785ac5df3
--- /dev/null
+++ b/modules/meeting/app/components/base_errors_component.rb
@@ -0,0 +1,55 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-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 BaseErrorsComponent < ApplicationComponent
+ include ApplicationHelper
+ include OpTurbo::Streamable
+ include OpPrimer::ComponentHelpers
+
+ def initialize(object, keys: %w[base])
+ super
+
+ @errors = object.errors
+ @keys = keys
+ end
+
+ def render?
+ @keys.any? { |key| @errors[key].present? }
+ end
+
+ def call
+ render(Primer::Beta::Flash.new(scheme: :danger, icon: :stop, spacious: true)) do
+ joined_messages
+ end
+ end
+
+ def joined_messages
+ messages = @keys.map { |key| @errors.full_messages_for(key) }.flatten
+ helpers.safe_join(messages, "<br />".html_safe)
+ end
+end
diff --git a/modules/meeting/app/components/meetings/sidebar/details_form_component.html.erb b/modules/meeting/app/components/meetings/sidebar/details_form_component.html.erb
index b5271d00ac8..4931236d2be 100644
--- a/modules/meeting/app/components/meetings/sidebar/details_form_component.html.erb
+++ b/modules/meeting/app/components/meetings/sidebar/details_form_component.html.erb
@@ -8,6 +8,10 @@
component_collection do |collection|
collection.with_component(Primer::Alpha::Dialog::Body.new) do
flex_layout(my: 3) do |modal_body|
+ modal_body.with_row do
+ render(BaseErrorsComponent.new(@meeting))
+ end
+
modal_body.with_row do
render(Meeting::StartDate.new(f, initial_value: start_date_initial_value))
end
@@ -23,6 +27,10 @@
modal_body.with_row(mt: 3) do
render(Meeting::Location.new(f))
end
+
+ modal_body.with_row do
+ render(Meeting::LockVersion.new(f))
+ end
end
end
diff --git a/modules/meeting/app/contracts/meeting_agenda_items/update_contract.rb b/modules/meeting/app/contracts/meeting_agenda_items/update_contract.rb
index fa2a86edc86..5a2b559c72e 100644
--- a/modules/meeting/app/contracts/meeting_agenda_items/update_contract.rb
+++ b/modules/meeting/app/contracts/meeting_agenda_items/update_contract.rb
@@ -30,6 +30,12 @@ module MeetingAgendaItems
class UpdateContract < BaseContract
validate :user_allowed_to_edit
+ attribute :lock_version do
+ if model.lock_version.nil? || model.lock_version_changed?
+ errors.add :base, :error_conflict
+ end
+ end
+
##
# Meeting agenda items can currently be only edited
# through the project permission :manage_agendas
diff --git a/modules/meeting/app/contracts/meetings/base_contract.rb b/modules/meeting/app/contracts/meetings/base_contract.rb
new file mode 100644
index 00000000000..f6f86053961
--- /dev/null
+++ b/modules/meeting/app/contracts/meetings/base_contract.rb
@@ -0,0 +1,44 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-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 Meetings
+ class BaseContract < ::ModelContract
+ def self.model
+ Meeting
+ end
+
+ attribute :title
+ attribute :author_id
+ attribute :project_id
+ attribute :location
+ attribute :duration
+ attribute :state
+ attribute :start_date
+ attribute :start_time_hour
+ end
+end
diff --git a/modules/meeting/app/contracts/meetings/update_contract.rb b/modules/meeting/app/contracts/meetings/update_contract.rb
new file mode 100644
index 00000000000..fa3b1d048d4
--- /dev/null
+++ b/modules/meeting/app/contracts/meetings/update_contract.rb
@@ -0,0 +1,45 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-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 Meetings
+ class UpdateContract < BaseContract
+ validate :user_allowed_to_edit
+
+ attribute :lock_version do
+ if model.lock_version.nil? || model.lock_version_changed?
+ errors.add :base, :error_conflict
+ end
+ end
+
+ def user_allowed_to_edit
+ unless user.allowed_in_project?(:edit_meetings, model.project)
+ errors.add :base, :error_unauthorized
+ end
+ end
+ end
+end
diff --git a/modules/meeting/app/controllers/meeting_agenda_items_controller.rb b/modules/meeting/app/controllers/meeting_agenda_items_controller.rb
index f7f9bed4ba1..56482fc44a8 100644
--- a/modules/meeting/app/controllers/meeting_agenda_items_controller.rb
+++ b/modules/meeting/app/controllers/meeting_agenda_items_controller.rb
@@ -195,7 +195,9 @@ class MeetingAgendaItemsController < ApplicationController
end
def meeting_agenda_item_params
- params.require(:meeting_agenda_item).permit(:title, :duration_in_minutes, :notes, :work_package_id)
+ params
+ .require(:meeting_agenda_item)
+ .permit(:title, :duration_in_minutes, :notes, :work_package_id, :lock_version)
end
def generic_call_failure_response(call)
diff --git a/modules/meeting/app/controllers/meetings_controller.rb b/modules/meeting/app/controllers/meetings_controller.rb
index 907ecc4a4fb..41ef478cd64 100644
--- a/modules/meeting/app/controllers/meetings_controller.rb
+++ b/modules/meeting/app/controllers/meetings_controller.rb
@@ -167,17 +167,19 @@ class MeetingsController < ApplicationController
end
def update_details
- @meeting.update(structured_meeting_params)
+ call = ::Meetings::UpdateService
+ .new(user: current_user, model: @meeting)
+ .call(structured_meeting_params)
- if @meeting.errors.any?
- update_sidebar_details_form_component_via_turbo_stream
- else
+ if call.success?
update_header_component_via_turbo_stream
update_sidebar_details_component_via_turbo_stream
# the list needs to be updated if the start time has changed
# in order to update the agenda item time slots
update_list_via_turbo_stream if @meeting.previous_changes[:start_time].present?
+ else
+ update_sidebar_details_form_component_via_turbo_stream
end
respond_with_turbo_streams
@@ -307,7 +309,9 @@ class MeetingsController < ApplicationController
def structured_meeting_params
if params[:structured_meeting].present?
- params.require(:structured_meeting).permit(:title, :location, :start_time_hour, :duration, :start_date, :state)
+ params
+ .require(:structured_meeting)
+ .permit(:title, :location, :start_time_hour, :duration, :start_date, :state, :lock_version)
end
end
diff --git a/modules/meeting/app/forms/meeting/lock_version.rb b/modules/meeting/app/forms/meeting/lock_version.rb
new file mode 100644
index 00000000000..0f9963914d7
--- /dev/null
+++ b/modules/meeting/app/forms/meeting/lock_version.rb
@@ -0,0 +1,33 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-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 Meeting::LockVersion < ApplicationForm
+ form do |meeting_form|
+ meeting_form.hidden(name: :lock_version)
+ end
+end
diff --git a/modules/meeting/app/forms/meeting_agenda_item/submit.rb b/modules/meeting/app/forms/meeting_agenda_item/submit.rb
index ead95703510..fc337229ee6 100644
--- a/modules/meeting/app/forms/meeting_agenda_item/submit.rb
+++ b/modules/meeting/app/forms/meeting_agenda_item/submit.rb
@@ -29,6 +29,7 @@
class MeetingAgendaItem::Submit < ApplicationForm
form do |agenda_item_form|
agenda_item_form.group(layout: :horizontal) do |button_group|
+ button_group.hidden(name: :lock_version)
button_group.hidden(name: :type, value: @type, scope_name_to_model: false)
button_group.submit(name: :submit_button, label: I18n.t("button_save"), scheme: :primary)
end
diff --git a/modules/meeting/app/services/meetings/set_attributes_service.rb b/modules/meeting/app/services/meetings/set_attributes_service.rb
new file mode 100644
index 00000000000..a800e7b4a88
--- /dev/null
+++ b/modules/meeting/app/services/meetings/set_attributes_service.rb
@@ -0,0 +1,37 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-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 Meetings
+ class SetAttributesService < ::BaseServices::SetAttributes
+ def set_default_attributes(_params)
+ model.change_by_system do
+ model.author = user
+ end
+ end
+ end
+end
diff --git a/modules/meeting/app/services/meetings/update_service.rb b/modules/meeting/app/services/meetings/update_service.rb
new file mode 100644
index 00000000000..c0e9470be03
--- /dev/null
+++ b/modules/meeting/app/services/meetings/update_service.rb
@@ -0,0 +1,32 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-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 Meetings
+ class UpdateService < ::BaseServices::Update
+ end
+end
diff --git a/modules/meeting/spec/contracts/meetings/update_contract_spec.rb b/modules/meeting/spec/contracts/meetings/update_contract_spec.rb
new file mode 100644
index 00000000000..4687c05478a
--- /dev/null
+++ b/modules/meeting/spec/contracts/meetings/update_contract_spec.rb
@@ -0,0 +1,66 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-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.describe Meetings::UpdateContract do
+ include_context 'ModelContract shared context'
+
+ shared_let(:project) { create(:project) }
+ shared_let(:meeting) { create(:structured_meeting, project:) }
+ let(:contract) { described_class.new(meeting, user) }
+
+ context 'with permission' do
+ let(:user) do
+ create(:user, member_with_permissions: { project => [:edit_meetings] })
+ end
+
+ it_behaves_like 'contract is valid'
+
+ context 'when lock_version is changed' do
+ before do
+ allow(meeting).to receive(:lock_version_changed?).and_return(true)
+ end
+
+ it_behaves_like 'contract is invalid', base: :error_conflict
+ end
+ end
+
+ context 'without permission' do
+ let(:user) { build_stubbed(:user) }
+
+ it_behaves_like 'contract is invalid', base: :error_unauthorized
+ end
+
+ include_examples 'contract reuses the model errors' do
+ let(:user) { build_stubbed(:user) }
+ end
+end
diff --git a/modules/meeting/spec/features/structured_meetings/structured_meeting_crud_spec.rb b/modules/meeting/spec/features/structured_meetings/structured_meeting_crud_spec.rb
index aad918e021d..6e60f575116 100644
--- a/modules/meeting/spec/features/structured_meetings/structured_meeting_crud_spec.rb
+++ b/modules/meeting/spec/features/structured_meetings/structured_meeting_crud_spec.rb
@@ -226,6 +226,30 @@ RSpec.describe 'Structured meetings CRUD',
end
end
+ it 'shows an error toast trying to update an outdated item' do
+ show_page.expect_toast(message: 'Successful creation')
+
+ # Can add and edit a single item
+ show_page.add_agenda_item do
+ fill_in 'Title', with: 'My agenda item'
+ fill_in 'Duration (min)', with: '25'
+ end
+
+ show_page.expect_agenda_item title: 'My agenda item'
+ show_page.cancel_add_form
+
+ item = MeetingAgendaItem.find_by!(title: 'My agenda item')
+ show_page.edit_agenda_item(item) do
+ # Side effect: update the item
+ item.update!(title: 'Updated title')
+
+ fill_in 'Title', with: 'My agenda item edited'
+ click_button 'Save'
+ end
+
+ expect(page).to have_css('.flash', text: I18n.t('activerecord.errors.messages.error_conflict'))
+ end
+
context 'with a work package reference to another' do
let!(:meeting) { create(:structured_meeting, project:, author: current_user) }
let!(:other_project) { create(:project) }
diff --git a/modules/meeting/spec/services/meetings/update_service_spec.rb b/modules/meeting/spec/services/meetings/update_service_spec.rb
new file mode 100644
index 00000000000..1d06e388983
--- /dev/null
+++ b/modules/meeting/spec/services/meetings/update_service_spec.rb
@@ -0,0 +1,36 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-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_update_service'
+
+RSpec.describe Meetings::UpdateService, type: :model do
+ it_behaves_like 'BaseServices update service' do
+ let(:factory) { :meeting }
+ end
+end