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.

Find relations through one object, destroy the found one

Guido Boyke4cba50117c88
diff --git a/app/controllers/organisations/tickets/relations_controller.rb b/app/controllers/organisations/tickets/relations_controller.rb
index 431a5f7..35326ff 100644
--- a/app/controllers/organisations/tickets/relations_controller.rb
+++ b/app/controllers/organisations/tickets/relations_controller.rb
@@ -7,10 +7,10 @@
# The actors check no rights. This controller does, before anything is written:
# 1. write on tickets in the project of the shown ticket: the area gate of ProjectController
# and authorize @ticket, :update? as the first line of every action;
-# 2. read on the other ticket through policy_scope(Ticket): a ticket in a project the user
-# cannot read, of another organisation, or deleted meanwhile is a 404;
-# 3. removing needs the relation to be visible (TicketRelation.visible_to): what the page
-# never showed cannot be removed by its id either.
+# 2. read on the other ticket through Tickets::RelationAccess#other_ticket: a ticket in a
+# project the user cannot read, of another organisation, or deleted meanwhile is a 404;
+# 3. removing needs the relation to be visible (Tickets::RelationAccess#relation): what the
+# page never showed cannot be removed by its id either.
# Those 404s are streams, not a bare error page: inside the ticket frame a page without the
# frame would leave "Content missing" (review round 4 of #944). They say the same for "never
# existed", "not readable" and "gone meanwhile", so they tell nothing about other tickets.
@@ -51,7 +51,7 @@ module Organisations
return refuse([t('views.organisations.tickets.shared.relations.modal.pick_ticket')]) if other_ticket_id.blank?
# Not readable, of another organisation, or deleted since it was picked.
- @other_ticket = policy_scope(Ticket).find_by(id: other_ticket_id)
+ @other_ticket = relation_access.other_ticket(other_ticket_id)
return refuse([t('views.organisations.tickets.shared.relations.modal.ticket_unavailable')], status: :not_found) if @other_ticket.nil?
result = ::Actors::CreateTicketRelation.call(ticket: @ticket, other_ticket: @other_ticket, kind: @kind,
@@ -67,10 +67,10 @@ module Organisations
# Not visible, not of this ticket, or removed by someone else on a page that was not
# told (no broadcast, plan §8.3): the section as it is now, and the reason.
- relation = TicketRelation.visible_to(current_user, current_organisation).touching(@ticket).find_by(id: params[:id])
+ relation = relation_access.relation(@ticket, params[:id])
return changed(t('services.actors.destroy_ticket_relation.not_found'), type: :alert, status: :not_found) if relation.nil?
- result = ::Actors::DestroyTicketRelation.call(ticket: @ticket, relation_id: relation.id, current_user: current_user)
+ result = ::Actors::DestroyTicketRelation.call(ticket: @ticket, relation: relation, current_user: current_user)
return changed(t('flash.ticket_relations.removed')) if result.success?
return changed(failed_message, type: :alert, status: :internal_server_error) if internal?(result)
@@ -90,6 +90,12 @@ module Organisations
@ticket = @project.tickets.find(params[:ticket_id])
end
+ # A person on a page: no token, so the reach is their own.
+ def relation_access
+ @relation_access ||= ::Tickets::RelationAccess.new(user: current_user, organisation: current_organisation,
+ token: nil)
+ end
+
def internal?(result)
INTERNAL_CODES.include?(result.code)
end
diff --git a/app/services/actors/destroy_ticket_relation.rb b/app/services/actors/destroy_ticket_relation.rb
index ccaf825..edf3007 100644
--- a/app/services/actors/destroy_ticket_relation.rb
+++ b/app/services/actors/destroy_ticket_relation.rb
@@ -2,20 +2,22 @@
# app/services/actors/destroy_ticket_relation.rb
#
-# Removes one relation of `ticket` (doc/ticket_relations_plan.md, revision 4, §6.1). The
-# relation is looked up from either end, since "blocked by" on this page is the other ticket's
-# "blocks" row; a relation that does not touch `ticket` is not found. Removing an edge cannot
-# close a cycle, so no lock.
+# Removes one relation of `ticket` (doc/ticket_relations_plan.md, revision 4, §6.1). The caller
+# hands in the relation it found, and this actor does not trust that object: destroy! on a row
+# someone else removed succeeds silently, so it reads the row again, from either end, since
+# "blocked by" on this page is the other ticket's "blocks" row. A relation that does not touch
+# `ticket`, or is gone, is not found. Removing an edge cannot close a cycle, so no lock.
#
# No rights checks here: the caller makes sure the user may write tickets in the project of
-# `ticket` (PR 2, controller). No activity and no notification (plan §6.2).
+# `ticket` and may see the relation (Tickets::RelationAccess#relation). No activity and no
+# notification (plan §6.2).
module Actors
class DestroyTicketRelation < BaseActor
- def initialize(ticket:, relation_id:, current_user:)
+ def initialize(ticket:, relation:, current_user:)
super()
@ticket = ticket
- @relation_id = relation_id
+ @relation = relation
@current_user = current_user
end
@@ -25,8 +27,7 @@ module Actors
end
def perform
- relation = TicketRelation.where(source_ticket: @ticket).or(TicketRelation.where(target_ticket: @ticket))
- .find_by(id: @relation_id)
+ relation = TicketRelation.touching(@ticket).find_by(id: @relation.id)
raise ActorError.new(I18n.t('services.actors.destroy_ticket_relation.not_found'), code: :not_found) if relation.nil?
relation.destroy!
diff --git a/app/services/tickets/relation_access.rb b/app/services/tickets/relation_access.rb
new file mode 100644
index 0000000..a87e31e
--- /dev/null
+++ b/app/services/tickets/relation_access.rb
@@ -0,0 +1,28 @@
+# frozen_string_literal: true
+
+# Entry point: other_ticket
+#
+# Read object: what a relation write may touch for one holder. #other_ticket finds the ticket at
+# the other end, #relation a relation of the shown ticket. Each gives the row or nil, so missing,
+# foreign, unreadable and out of reach look the same to the caller.
+#
+# It only finds: no rights decision, no wording, no raising. Those stay with the caller. token:
+# means what it means in Tickets::ReadableForToken: nil is no second limit, and a caller that
+# holds a token must pass it.
+module Tickets
+ class RelationAccess
+ def initialize(user:, organisation:, token: nil)
+ @user = user
+ @organisation = organisation
+ @token = token
+ end
+
+ def other_ticket(id)
+ ReadableForToken.call(user: @user, organisation: @organisation, token: @token).find_by(id: id)
+ end
+
+ def relation(ticket, id)
+ TicketRelation.visible_to(@user, @organisation, token: @token).touching(ticket).find_by(id: id)
+ end
+ end
+end
diff --git a/spec/requests/organisations/tickets/relations_spec.rb b/spec/requests/organisations/tickets/relations_spec.rb
index a7ee31f..7f46f74 100644
--- a/spec/requests/organisations/tickets/relations_spec.rb
+++ b/spec/requests/organisations/tickets/relations_spec.rb
@@ -457,7 +457,7 @@ RSpec.describe 'Organisations::Tickets::Relations', :ci_guard, type: :request do
it 'answers a stale page with the section as it is now' do
sign_in writer
ActsAsTenant.with_tenant(organisation) do
- Actors::DestroyTicketRelation.call(ticket: ticket, relation_id: outgoing.id, current_user: writer)
+ Actors::DestroyTicketRelation.call(ticket: ticket, relation: outgoing, current_user: writer)
end
delete destroy_path(outgoing), as: :turbo_stream
diff --git a/spec/services/actors/destroy_ticket_relation_spec.rb b/spec/services/actors/destroy_ticket_relation_spec.rb
index 42ea537..feddb90 100644
--- a/spec/services/actors/destroy_ticket_relation_spec.rb
+++ b/spec/services/actors/destroy_ticket_relation_spec.rb
@@ -20,7 +20,7 @@ RSpec.describe Actors::DestroyTicketRelation do
let!(:neighbour) { TicketRelation.create!(source_ticket: ticket_a, target_ticket: ticket_c, relation_type: :relates_to) }
def unlink(ticket, relation_id)
- described_class.call(ticket: ticket, relation_id: relation_id, current_user: user)
+ described_class.call(ticket: ticket, relation: TicketRelation.find_or_initialize_by(id: relation_id), current_user: user)
end
it 'removes the relation from its source ticket, and only that row' do
diff --git a/spec/services/actors/ticket_relation_actors_tenant_boundary_spec.rb b/spec/services/actors/ticket_relation_actors_tenant_boundary_spec.rb
index ba0307f..0daff8c 100644
--- a/spec/services/actors/ticket_relation_actors_tenant_boundary_spec.rb
+++ b/spec/services/actors/ticket_relation_actors_tenant_boundary_spec.rb
@@ -42,7 +42,7 @@ RSpec.describe 'Ticket relation actors across organisations', :ci_guard do
results = ActsAsTenant.with_tenant(organisation) do
Prosopite.pause do
[ticket, foreign_ticket].map do |shown|
- Actors::DestroyTicketRelation.call(ticket: shown, relation_id: foreign_relation.id, current_user: user).code
+ Actors::DestroyTicketRelation.call(ticket: shown, relation: foreign_relation, current_user: user).code
end
end
end
diff --git a/spec/services/tickets/relation_access_spec.rb b/spec/services/tickets/relation_access_spec.rb
new file mode 100644
index 0000000..2ee1765
--- /dev/null
+++ b/spec/services/tickets/relation_access_spec.rb
@@ -0,0 +1,163 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+# What a relation write may touch. The caller decides the answer; this object only finds: the
+# other ticket through Tickets::ReadableForToken, the relation through TicketRelation.visible_to.
+# With a token both stop at its reach, so an account admin who reads every project and holds a
+# token for A alone cannot name a ticket or a relation in B. Each nil stands next to a positive
+# case, so none of them is green in a vacuum.
+RSpec.describe Tickets::RelationAccess, :ci_guard do
+ let(:organisation) { create(:organisation) }
+ let(:foreign_organisation) { create(:organisation) }
+ let(:project_a) { new_project(organisation) }
+ let(:project_b) { new_project(organisation) }
+ let(:hidden_project) { new_project(organisation) }
+
+ let(:reader) { member_reading(project_a, project_b) }
+ let(:admin) { account_admin }
+
+ let(:ticket_a) { new_ticket(project_a, 'In reach') }
+ let(:ticket_a2) { new_ticket(project_a, 'Also in reach') }
+ let(:ticket_b) { new_ticket(project_b, 'Readable, out of reach') }
+ let(:hidden_ticket) { new_ticket(hidden_project, 'Unreadable') }
+
+ after { ActsAsTenant.current_tenant = nil }
+
+ # Setup only: each create loads its organisation for the belongs_to check, which Prosopite
+ # would report from the second record on. What the object itself asks stays scanned.
+ def new_project(org)
+ Prosopite.pause { ActsAsTenant.with_tenant(org) { create(:project, organisation: org) } }
+ end
+
+ def new_ticket(project, title)
+ Prosopite.pause { ActsAsTenant.with_tenant(project.organisation) { create(:ticket, project: project, title: title) } }
+ end
+
+ def link(source, target, type = :blocks)
+ Prosopite.pause do
+ ActsAsTenant.with_tenant(source.organisation) do
+ TicketRelation.create!(source_ticket: source, target_ticket: target, relation_type: type)
+ end
+ end
+ end
+
+ def member_reading(*projects)
+ user = create(:user)
+ ActsAsTenant.with_tenant(organisation) do
+ role = create(:role, organisation: organisation)
+ create(:membership, user: user, organisation: organisation, role: role, active: true)
+ projects.each { |project| create(:project_permission, user: user, project: project, tickets_access: 'read_only') }
+ end
+ user
+ end
+
+ def account_admin
+ user = create(:user)
+ ActsAsTenant.with_tenant(organisation) do
+ role = create(:admin_role, organisation: organisation)
+ create(:membership, user: user, organisation: organisation, role: role, active: true)
+ end
+ user
+ end
+
+ def token_reaching_a(user)
+ Prosopite.pause do
+ create(:personal_access_token, user: user, organisations: [organisation],
+ project_scopes: { organisation => [project_a] })
+ end
+ end
+
+ def access(user, token: nil)
+ described_class.new(user: user, organisation: organisation, token: token)
+ end
+
+ def other_ticket(user, id, token: nil)
+ ActsAsTenant.with_tenant(organisation) { access(user, token: token).other_ticket(id) }
+ end
+
+ def relation(user, ticket, id, token: nil)
+ ActsAsTenant.with_tenant(organisation) { access(user, token: token).relation(ticket, id) }
+ end
+
+ describe '#other_ticket' do
+ it 'finds a readable ticket' do
+ expect(other_ticket(reader, ticket_b.id)).to eq(ticket_b)
+ end
+
+ {
+ 'a missing id' => -> { SecureRandom.uuid },
+ 'a ticket of a foreign organisation' => -> { new_ticket(new_project(foreign_organisation), 'Foreign').id },
+ 'a ticket the user may not read' => -> { hidden_ticket.id }
+ }.each do |name, id|
+ it "gives nil for #{name}" do
+ expect(other_ticket(reader, instance_exec(&id))).to be_nil
+ end
+ end
+
+ it 'gives nil for a ticket the user may read and the token does not reach' do
+ token = token_reaching_a(reader)
+
+ expect(other_ticket(reader, ticket_a.id, token: token)).to eq(ticket_a)
+ expect(other_ticket(reader, ticket_b.id, token: token)).to be_nil
+ end
+
+ it 'stops an account admin at the reach of a token for project A' do
+ expect(other_ticket(admin, ticket_b.id, token: token_reaching_a(admin))).to be_nil
+ end
+
+ it 'finds project B for the account admin without a token' do
+ expect(other_ticket(admin, ticket_b.id)).to eq(ticket_b)
+ end
+ end
+
+ describe '#relation' do
+ it 'finds a visible relation from both of its ends' do
+ edge = link(ticket_a, ticket_b)
+
+ expect(relation(reader, ticket_a, edge.id)).to eq(edge)
+ expect(relation(reader, ticket_b, edge.id)).to eq(edge)
+ end
+
+ it 'gives nil for a relation that does not touch the ticket' do
+ edge = link(ticket_a, ticket_b)
+
+ expect(relation(reader, ticket_a2, edge.id)).to be_nil
+ end
+
+ it 'gives nil when the other end is hidden, from either end' do
+ outgoing = link(ticket_a, hidden_ticket)
+ incoming = link(hidden_ticket, ticket_a, :relates_to)
+
+ expect(relation(reader, ticket_a, outgoing.id)).to be_nil
+ expect(relation(reader, ticket_a, incoming.id)).to be_nil
+ # The rows exist: the account admin sees both, so the hiding is the rule.
+ expect(relation(admin, ticket_a, outgoing.id)).to eq(outgoing)
+ expect(relation(admin, ticket_a, incoming.id)).to eq(incoming)
+ end
+
+ it 'stops an account admin at the reach of a token for project A' do
+ edge = link(ticket_a, ticket_b)
+ inside = link(ticket_a, ticket_a2)
+ token = token_reaching_a(admin)
+
+ expect(relation(admin, ticket_a, inside.id, token: token)).to eq(inside)
+ expect(relation(admin, ticket_a, edge.id, token: token)).to be_nil
+ end
+
+ it 'finds the relation A-B for the account admin without a token' do
+ edge = link(ticket_a, ticket_b)
+
+ expect(relation(admin, ticket_a, edge.id)).to eq(edge)
+ end
+
+ it 'gives nil for the relation of a foreign organisation, even from one of its tickets' do
+ foreign_project = new_project(foreign_organisation)
+ foreign_ticket = new_ticket(foreign_project, 'Foreign A')
+ foreign = link(foreign_ticket, new_ticket(foreign_project, 'Foreign B'))
+
+ expect(relation(admin, ticket_a, foreign.id)).to be_nil
+ expect(relation(admin, foreign_ticket, foreign.id)).to be_nil
+ end
+ end
+end
diff --git a/spec/system/ticket_relations_spec.rb b/spec/system/ticket_relations_spec.rb
index 48acd2e..2154d80 100644
--- a/spec/system/ticket_relations_spec.rb
+++ b/spec/system/ticket_relations_spec.rb
@@ -152,7 +152,7 @@ RSpec.describe 'Ticket relations on the ticket page', type: :system do
visit lane_path
within(section_css) { expect(page).to have_content('Zahlungsanbieter wechseln') }
- Actors::DestroyTicketRelation.call(ticket: ticket, relation_id: stale.id, current_user: user)
+ Actors::DestroyTicketRelation.call(ticket: ticket, relation: stale, current_user: user)
within("#{section_css} li[data-relation-id='#{stale.id}']") do
accept_confirm { find("button[aria-label='#{I18n.t("#{rs}.remove")}']").click }
end