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.

fix: review round 1 of #113, lock a term before deleting it and pin four rules

Guido Boykea59dd01d931d
diff --git a/app/services/actors/destroy_glossary_term.rb b/app/services/actors/destroy_glossary_term.rb
index 0d59c7e..b00f84e 100644
--- a/app/services/actors/destroy_glossary_term.rb
+++ b/app/services/actors/destroy_glossary_term.rb
@@ -4,6 +4,12 @@
#
# Deletes one term of the glossary with its heard forms (K4a, plan §3.3).
# Rights are the caller's (GlossaryPolicy, manage_glossary).
+#
+# The term row is locked first, the order Actors::MergeGlossaryTerms locks in:
+# a delete and a merge of the same term at once then wait for each other.
+# Deleting the heard forms first held their rows while waiting for the term
+# the merge held, and the merge waited for those rows (deadlock; review round 1
+# of #113). A term deleted in the meantime is refused as gone.
module Actors
class DestroyGlossaryTerm < BaseActor
def initialize(term:, current_user:)
@@ -18,11 +24,18 @@ module Actors
end
def perform
+ lock_term
@term.destroy!
end
private
attr_reader :current_user
+
+ def lock_term
+ @term.lock!
+ rescue ActiveRecord::RecordNotFound
+ raise ActorError.new(I18n.t('controllers.glossary.not_found'), code: :not_found)
+ end
end
end
diff --git a/spec/requests/organisations/glossary_spec.rb b/spec/requests/organisations/glossary_spec.rb
index 9a2cca0..51ff2cf 100644
--- a/spec/requests/organisations/glossary_spec.rb
+++ b/spec/requests/organisations/glossary_spec.rb
@@ -166,6 +166,30 @@ RSpec.describe 'Organisations::Glossary', :ci_guard, type: :request do
expect(response.body).to include(I18n.t('views.organisations.glossary.show.read_only'))
end
+ it 'opens no form with ?new=term on a full visit' do
+ get organisation_glossary_terms_path(organisation, new: 'term')
+
+ expect(response).to have_http_status(:ok)
+ expect(response.body).to include('Lalabase')
+ expect(response.body).not_to include('id="glossary_term_term"')
+ end
+
+ it 'opens no form with ?new=term in the lane frame' do
+ get organisation_glossary_terms_path(organisation, new: 'term'), headers: frame_headers
+
+ expect(response).to have_http_status(:ok)
+ expect(response.body).to include('glossary-term-placeholder')
+ expect(response.body).not_to include('id="glossary_term_term"')
+ end
+
+ # The write right comes before the heard form is looked up: 403, not 404.
+ it 'is refused deleting a heard form of another term with 403' do
+ delete correction_organisation_glossary_term_path(organisation, proposal, correction_id: open_correction.id)
+
+ expect(response).to have_http_status(:forbidden)
+ expect(TermCorrection.where(id: open_correction.id)).to exist
+ end
+
it 'shows a term in the lane frame' do
get organisation_glossary_term_path(organisation, term), headers: frame_headers
@@ -269,6 +293,57 @@ RSpec.describe 'Organisations::Glossary', :ci_guard, type: :request do
expect(page.css(%(a[href="#{organisation_people_path(organisation, person_id: speaker.id)}"])).text).to eq('Erika Beispiel')
end
+ # Review round 1 of #113: the link needs view_people, also for a member
+ # speaker (whom MembershipPolicy::Scope shows without it), and only to a
+ # row or member the reader may see on the people page.
+ it 'does not link a member speaker for a reader without view_people' do
+ # The reader would see the speaker on the members page (a shared project),
+ # so only view_people can keep the link away.
+ speaker_member = member_with(role_with)
+ create(:project_permission, user: viewer, project: open_project, tickets_access: :read_only)
+ create(:project_permission, user: speaker_member, project: open_project, tickets_access: :read_only)
+ create(:term_correction, organisation: organisation, glossary_term: term, heard: 'lalabeis', user: speaker_member)
+
+ page = lane_for(viewer)
+
+ expect(page.css('.glossary-heard__speaker').map { |node| node.text.strip }).to include(speaker_member.display_name)
+ expect(page.css(%(a[href*="member_id=#{speaker_member.id}"]))).to be_empty
+ end
+
+ describe 'with view_people but without any project' do
+ let(:reader) { member_with(role_with('view_glossary', 'people:view_people')) }
+ let(:hidden_member) { member_with(role_with) }
+
+ it 'names a member it may not see on the people page, without a link' do
+ create(:term_correction, organisation: organisation, glossary_term: term, heard: 'lalabeis', user: hidden_member)
+
+ page = lane_for(reader)
+
+ expect(page.css('.glossary-heard__speaker').map { |node| node.text.strip }).to include(hidden_member.display_name)
+ expect(page.css(%(a[href*="member_id=#{hidden_member.id}"]))).to be_empty
+ end
+
+ it 'names the row of a member it may not see, without a link' do
+ row = create(:person, organisation: organisation, user: hidden_member, name: 'Paula Platzhalter')
+ create(:term_correction, organisation: organisation, glossary_term: term, heard: 'lalabeis', person: row)
+
+ page = lane_for(reader)
+
+ expect(page.text).to include('Paula Platzhalter')
+ expect(page.css(%(a[href*="person_id=#{row.id}"]))).to be_empty
+ end
+
+ it 'links a member it may see on the people page (a shared project)' do
+ create(:project_permission, user: reader, project: open_project, tickets_access: :read_only)
+ create(:project_permission, user: hidden_member, project: open_project, tickets_access: :read_only)
+ create(:term_correction, organisation: organisation, glossary_term: term, heard: 'lalabeis', user: hidden_member)
+
+ page = lane_for(reader)
+
+ expect(page.css(%(a[href="#{organisation_people_path(organisation, member_id: hidden_member.id)}"]))).to be_present
+ end
+ end
+
it 'names a member speaker through the name display, linked to the member' do
member = member_with(role_with)
create(:term_correction, organisation: organisation, glossary_term: term, heard: 'lalabeis', user: member)
@@ -459,14 +534,16 @@ RSpec.describe 'Organisations::Glossary', :ci_guard, type: :request do
include_examples 'may keep the glossary'
end
+ # The base controller sends a deactivated member away before any glossary
+ # action runs; that the policy refuses them as well is pinned in
+ # glossary_policy_spec.rb and inactive_membership_scope_guard_spec.rb.
describe 'a deactivated member who still holds the right' do
- it 'gets nothing of the glossary' do
+ it 'is sent to the organisation picker before the glossary' do
sign_in member_with(role_with('manage_glossary'), active: false)
get organisation_glossary_terms_path(organisation)
- expect(response).to have_http_status(:redirect)
- expect(response.body).not_to include('Lalabase')
+ expect(response).to redirect_to(select_organisation_path)
end
end
@@ -498,13 +575,29 @@ RSpec.describe 'Organisations::Glossary', :ci_guard, type: :request do
expect(GlossaryTerm.unscoped.exists?(foreign_term.id)).to be(true)
end
- it 'answers 404 for confirm, reject and merge of a term of another organisation' do
+ it 'answers 404 for confirm of a term of another organisation' do
+ ActsAsTenant.with_tenant(other_organisation) { foreign_term.update!(status: 'proposed') }
+
+ patch confirm_organisation_glossary_term_path(organisation, foreign_term)
+
+ expect(response).to have_http_status(:not_found)
+ expect(GlossaryTerm.unscoped.find(foreign_term.id).status).to eq('proposed')
+ end
+
+ it 'answers 404 for reject of a term of another organisation' do
patch reject_organisation_glossary_term_path(organisation, foreign_term)
expect(response).to have_http_status(:not_found)
expect(GlossaryTerm.unscoped.find(foreign_term.id).status).to eq('confirmed')
end
+ it 'answers 404 for merging a term of another organisation into this glossary' do
+ post merge_organisation_glossary_term_path(organisation, foreign_term), params: { target_id: term.id }
+
+ expect(response).to have_http_status(:not_found)
+ expect(GlossaryTerm.unscoped.exists?(foreign_term.id)).to be(true)
+ end
+
it 'sends the page of an organisation the user is not in to the organisation picker' do
foreign_term
get organisation_glossary_terms_path(other_organisation)
diff --git a/spec/services/actors/destroy_glossary_term_spec.rb b/spec/services/actors/destroy_glossary_term_spec.rb
index b82ef91..de313b4 100644
--- a/spec/services/actors/destroy_glossary_term_spec.rb
+++ b/spec/services/actors/destroy_glossary_term_spec.rb
@@ -23,4 +23,35 @@ RSpec.describe Actors::DestroyGlossaryTerm, type: :service do
expect(TermCorrection.where(id: correction.id)).not_to exist
expect(TermCorrection.where(id: kept.id)).to exist
end
+
+ # Review round 1 of #113: a delete and a merge of the same term at once must
+ # take their locks in the same order (the term first), or they deadlock:
+ # the delete held the heard forms and waited for the term the merge held.
+ it 'locks the term before it deletes the heard forms' do
+ term = create(:glossary_term, organisation: organisation)
+ create(:term_correction, organisation: organisation, glossary_term: term)
+ statements = []
+ callback = ->(*, payload) { statements << payload[:sql] }
+
+ ActiveSupport::Notifications.subscribed(callback, 'sql.active_record') do
+ described_class.call(term: term, current_user: writer)
+ end
+
+ lock = statements.index { |sql| sql.include?('FROM "glossary_terms"') && sql.include?('FOR UPDATE') }
+ delete = statements.index { |sql| sql.start_with?('DELETE FROM "term_corrections"') }
+ expect(lock).not_to be_nil
+ expect(delete).not_to be_nil
+ expect(lock).to be < delete
+ end
+
+ it 'answers a term deleted in the meantime as gone' do
+ term = create(:glossary_term, organisation: organisation)
+ GlossaryTerm.where(id: term.id).delete_all
+
+ result = described_class.call(term: term, current_user: writer)
+
+ expect(result).not_to be_success
+ expect(result.code).to eq(:not_found)
+ expect(result.errors).to eq([I18n.t('controllers.glossary.not_found')])
+ end
end