Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

Hold publication, deletion and booking edits to the authorized project

Guido Boyke5a072473926d
diff --git a/app/controllers/organisations/meetings_controller.rb b/app/controllers/organisations/meetings_controller.rb
index aaf492d..c22f5bb 100644
--- a/app/controllers/organisations/meetings_controller.rb
+++ b/app/controllers/organisations/meetings_controller.rb
@@ -183,7 +183,7 @@ module Organisations
def destroy
authorize @meeting, :destroy?
- if @meeting.destroy
+ if destroy_in_project
respond_to do |format|
format.turbo_stream do
# closeLane (application.js): the meeting is gone, so is its lane and
@@ -474,6 +474,18 @@ module Organisations
organisation_project_meetings_path(@current_organisation, target, meeting_id: @meeting.id)
end
+ # The delete was authorized against this project. Actors::MoveMeeting holds
+ # the meeting's row lock while it moves it: under the same lock a meeting
+ # that moved meanwhile is gone from here, as a deleted one would be.
+ def destroy_in_project
+ Meeting.transaction do
+ @meeting.lock!
+ raise ActiveRecord::RecordNotFound unless @meeting.project_id == @project.id
+
+ @meeting.destroy
+ end
+ end
+
def set_meeting
# Scoped to @project.meetings: a foreign-project (or foreign-tenant) id is
# simply not found → 404, which rejects cross-project/tenant access.
diff --git a/app/models/meeting.rb b/app/models/meeting.rb
index fcf7a32..bf00128 100644
--- a/app/models/meeting.rb
+++ b/app/models/meeting.rb
@@ -220,8 +220,8 @@ class Meeting < ApplicationRecord
# The live summary stream names the project: after a move (Actors::MoveMeeting)
# a page still open in the old project hears nothing of what is written in
# the new one. The view subscribes through the same method.
- def summary_stream_name
- "#{dom_id(self)}_#{project_id}_summary"
+ def summary_stream_name(for_project_id = project_id)
+ "#{dom_id(self)}_#{for_project_id}_summary"
end
def summary_source
@@ -271,8 +271,10 @@ class Meeting < ApplicationRecord
return unless saved_change_to_generated_summary? || saved_change_to_summary_creation_flag? ||
saved_change_to_summary_creation_error?
+ # The persisted project, not this instance's: a summary job loads the
+ # meeting, a move commits, and the job finishes on its stale copy.
broadcast_replace_to(
- summary_stream_name,
+ summary_stream_name(Meeting.unscoped.where(id: id).pick(:project_id) || project_id),
target: "#{dom_id(self)}_summary_section",
partial: 'organisations/meetings/summary_section',
locals: { meeting: self }
diff --git a/app/services/actors/save_meeting_analysis.rb b/app/services/actors/save_meeting_analysis.rb
index 112020f..d68636a 100644
--- a/app/services/actors/save_meeting_analysis.rb
+++ b/app/services/actors/save_meeting_analysis.rb
@@ -28,6 +28,8 @@ module Actors
def initialize(meeting:, current_user:, params: {}, id: nil)
super()
@meeting = meeting
+ # The project the caller authorized the write against, before any lock.
+ @authorized_project_id = meeting.project_id
@current_user = current_user
@params = params
@id = id
@@ -40,6 +42,7 @@ module Actors
end
def perform
+ guard_meeting_in_project!
if @id.blank?
create_analysis
elsif boolean(@params[:rerun])
@@ -196,6 +199,17 @@ module Actors
# the UPDATE below, and this run then writes its search release onto an analysis that
# has meanwhile become private. `perform` already runs inside BaseActor's transaction,
# so the lock lives exactly as long as it must.
+ # Actors::MoveMeeting holds the meeting's row lock while it moves it and
+ # checks the published analyses. Under the same lock a meeting that moved
+ # meanwhile is gone from the project this write was authorized against,
+ # so nothing is created, rerun or published there.
+ def guard_meeting_in_project!
+ @meeting.lock!
+ return if @meeting.project_id == @authorized_project_id
+
+ raise ActorError, I18n.t('services.meetings.analysis.not_found')
+ end
+
def load_analysis!
@meeting.meeting_analyses.lock.find_by(id: @id) ||
raise(ActorError, I18n.t('services.meetings.analysis.not_found'))
diff --git a/app/services/actors/save_time_session.rb b/app/services/actors/save_time_session.rb
index 102b7b2..eb80f08 100644
--- a/app/services/actors/save_time_session.rb
+++ b/app/services/actors/save_time_session.rb
@@ -25,6 +25,9 @@ module Actors
def initialize(time_session:, attributes:, group_change:, current_user:)
super()
@time_session = time_session
+ # The project the caller authorized the write against (a requested move
+ # included), before any reload.
+ @authorized_project_id = time_session.project_id
@attributes = attributes.to_h # decouple from ActionController::Parameters
@group_change = group_change
@current_user = current_user
@@ -34,8 +37,13 @@ module Actors
@time_session
end
+ # Lock order as in Actors::MoveMeeting, meeting before booking: the other
+ # way round, a move and an edit of one of its bookings would wait on each
+ # other.
def perform
+ lock_meeting
refresh_persisted_state
+ guard_authorized_project!
guard_meeting_project!
guard_group_transition!
@time_session.assign_attributes(@attributes)
@@ -55,19 +63,29 @@ module Actors
return unless @time_session.persisted?
pending = @time_session.changes.transform_values(&:last)
- @time_session.reload
+ @time_session.reload(lock: true)
@time_session.assign_attributes(pending)
end
+ # A move of the booking's meeting that committed after the caller's check
+ # has taken the booking into a project this editor was never checked for.
+ def guard_authorized_project!
+ return if @time_session.project_id == @authorized_project_id
+
+ raise ActorError.new(I18n.t("#{ERROR_SCOPE}.moved_meanwhile"), code: :moved_meanwhile)
+ end
+
# A booking on a meeting takes the meeting's row lock, the one
- # Actors::MoveMeeting holds while it moves the meeting and its bookings. A
- # move that committed meanwhile has taken the meeting into another project;
- # the booking would land in the old one, so it is refused.
+ # Actors::MoveMeeting holds while it moves the meeting and its bookings.
+ def lock_meeting
+ @time_session.meeting&.lock!
+ end
+
+ # A move that committed meanwhile has taken the meeting into another
+ # project; the booking would land in the old one, so it is refused.
def guard_meeting_project!
meeting = @time_session.meeting
return unless meeting
-
- meeting.lock!
return if meeting.project_id == @time_session.project_id
raise ActorError.new(I18n.t("#{ERROR_SCOPE}.meeting_moved"), code: :meeting_moved)
diff --git a/config/locales/services.de.yml b/config/locales/services.de.yml
index 0318e0b..09f3d81 100644
--- a/config/locales/services.de.yml
+++ b/config/locales/services.de.yml
@@ -87,6 +87,7 @@ de:
session_billed: "Diese Zeit-Session ist bereits abgerechnet und kann nicht mehr geändert oder aus ihrer Abrechnung entfernt werden."
generic_error: "Die Zeit-Session konnte nicht gespeichert werden. Bitte versuchen Sie es später erneut."
meeting_moved: "Das Meeting liegt inzwischen in einem anderen Projekt. Bitte buchen Sie die Zeit dort."
+ moved_meanwhile: "Die Zeit-Session liegt inzwischen in einem anderen Projekt. Bitte laden Sie die Seite neu."
bulk_customer_description_generator:
empty_result: "Zeit-Session %{id}: Die KI hat keine Beschreibung geliefert."
embedding_reprocessing:
diff --git a/config/locales/services.en.yml b/config/locales/services.en.yml
index ff3dca7..3042d9b 100644
--- a/config/locales/services.en.yml
+++ b/config/locales/services.en.yml
@@ -87,6 +87,7 @@ en:
session_billed: "This time session is already invoiced and can no longer be changed or removed from its billing group."
generic_error: "The time session could not be saved. Please try again later."
meeting_moved: "The meeting is in another project now. Please book the time there."
+ moved_meanwhile: "The time session is in another project now. Please reload the page."
bulk_customer_description_generator:
empty_result: "Time session %{id}: The AI returned no description."
embedding_reprocessing:
diff --git a/spec/models/meeting_spec.rb b/spec/models/meeting_spec.rb
index 99cb76e..52529d8 100644
--- a/spec/models/meeting_spec.rb
+++ b/spec/models/meeting_spec.rb
@@ -399,6 +399,18 @@ RSpec.describe Meeting, type: :model do
meeting.update!(summary_creation_flag: true)
end
+ # A summary job loads the meeting, a move commits, the job finishes on its
+ # stale instance. The stream must name the project the row is in now:
+ # the old one would hand the new summary to readers of the old project.
+ it 'sends to the stream of the persisted project from a stale instance' do
+ elsewhere = create(:project, organisation: organisation)
+ Meeting.where(id: meeting.id).update_all(project_id: elsewhere.id)
+ moved_stream = "#{ActionView::RecordIdentifier.dom_id(meeting)}_#{elsewhere.id}_summary"
+
+ expect(meeting).to receive(:broadcast_replace_to).with(moved_stream, anything)
+ meeting.update!(summary_creation_flag: true)
+ end
+
it 'does not broadcast on an unrelated update (title only)' do
expect(meeting).not_to receive(:broadcast_replace_to)
meeting.update!(title: 'Renamed meeting')
diff --git a/spec/requests/organisations/meeting_move_spec.rb b/spec/requests/organisations/meeting_move_spec.rb
index c65302b..5e50101 100644
--- a/spec/requests/organisations/meeting_move_spec.rb
+++ b/spec/requests/organisations/meeting_move_spec.rb
@@ -178,6 +178,18 @@ RSpec.describe 'Organisations::Meetings move', :ci_guard, type: :request do
expect(response.body).not_to include('Erstgespräch')
end
+ it 'does not delete a meeting that moved away after the delete was authorized' do
+ allow_any_instance_of(Meeting).to receive(:lock!).and_wrap_original do |original, *args|
+ Meeting.where(id: original.receiver.id).update_all(project_id: target.id)
+ original.call(*args)
+ end
+ meeting
+ sign_in mover
+ delete organisation_project_meeting_path(organisation, source, meeting)
+
+ expect(ActsAsTenant.without_tenant { Meeting.exists?(meeting.id) }).to be(true)
+ end
+
context 'with a target the move refuses' do
def expect_refused(target_id, message)
sign_in mover
diff --git a/spec/services/actors/save_meeting_analysis_spec.rb b/spec/services/actors/save_meeting_analysis_spec.rb
index d733ccb..d5f567f 100644
--- a/spec/services/actors/save_meeting_analysis_spec.rb
+++ b/spec/services/actors/save_meeting_analysis_spec.rb
@@ -236,6 +236,23 @@ RSpec.describe Actors::SaveMeetingAnalysis do
describe 'flip (publish/unpublish)' do
let(:analysis) { create(:meeting_analysis, meeting: meeting, creator: user, is_private: true) }
+ # The write was authorized against the meeting's project. A move
+ # (Actors::MoveMeeting) that committed meanwhile has taken the meeting into
+ # another one; under the meeting's row lock the analysis is not found here.
+ it 'does not publish once the meeting has moved to another project' do
+ elsewhere = create(:project, organisation: organisation)
+ analysis
+ allow(meeting).to receive(:lock!).and_wrap_original do |original, *args|
+ Meeting.where(id: meeting.id).update_all(project_id: elsewhere.id)
+ original.call(*args)
+ end
+
+ result = described_class.call(meeting: meeting, current_user: user, id: analysis.id, params: { is_private: 'false' })
+
+ expect(result.success).to be(false)
+ expect(analysis.reload).to be_is_private
+ end
+
it 'publishes a private analysis (visible_to_user nulled)' do
result = described_class.call(meeting: meeting, current_user: user,
id: analysis.id, params: { is_private: 'false' })
diff --git a/spec/services/actors/save_time_session_spec.rb b/spec/services/actors/save_time_session_spec.rb
index 0c209ce..b6638b4 100644
--- a/spec/services/actors/save_time_session_spec.rb
+++ b/spec/services/actors/save_time_session_spec.rb
@@ -66,6 +66,21 @@ RSpec.describe Actors::SaveTimeSession, type: :service do
expect(call_actor(build_session(meeting: meeting))).to be_success
end
+ # The edit was authorized against the booking's project. A move of its
+ # meeting (Actors::MoveMeeting) that committed meanwhile has taken the
+ # booking into another project, where this editor was never checked.
+ it 'refuses to edit a booking that moved to another project after it was authorized' do
+ session = create_session(description: 'vorher')
+ elsewhere = create(:project, organisation: organisation)
+ TimeSession.where(id: session.id).update_all(project_id: elsewhere.id)
+
+ result = call_actor(session, attributes: { description: 'nachher' })
+
+ expect(result).not_to be_success
+ expect(result.code).to eq(:moved_meanwhile)
+ expect(session.reload.description).to eq('vorher')
+ end
+
it 'saves a new record and assigns the requested unbilled group without billing it' do
group = create(:time_session_group, organisation: organisation, creator: user)
session = build_session