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.

Add one owner for finding a ticket for a token holder

Guido Boyke0130af05ee99
diff --git a/app/services/tickets/readable_for_token.rb b/app/services/tickets/readable_for_token.rb
new file mode 100644
index 0000000..9e7265f
--- /dev/null
+++ b/app/services/tickets/readable_for_token.rb
@@ -0,0 +1,49 @@
+# frozen_string_literal: true
+
+# The one place that answers "which tickets can this holder find": the rows of `organisation`,
+# inside the policy scope of `user`, inside the reach of `token`. Missing, foreign, unreadable
+# and out of reach are all the same absence here, so a caller cannot tell them apart.
+#
+# It decides what can be FOUND, never the answer: no authorize, no 404, no wording. Those stay
+# with the caller.
+#
+# token: nil is legitimate. The assistant calls the same tools as MCP and has no token; its reach
+# is the person's own, which is the policy scope alone. nil therefore means "no second limit",
+# and a caller that holds a token must pass it. A token's reach of nil means every project of the
+# organisation, [] means none, also for an organisation outside the token.
+#
+# Built under `organisation` as the tenant, like TicketRelation.visible_to, and bound by
+# organisation_id, so the rows and the rights do not depend on the tenant current at the call.
+# ⚠️ That holds for the rows only: a preload chained onto the relation runs at execution, under
+# the tenant current THEN, and finds nothing under another one. The caller runs the query inside
+# ActsAsTenant.with_tenant(organisation).
+module Tickets
+ class ReadableForToken
+ def self.call(**)
+ new(**).call
+ end
+
+ def initialize(user:, organisation:, relation: Ticket, token: nil)
+ @user = user
+ @organisation = organisation
+ @relation = relation
+ @token = token
+ end
+
+ def call
+ ActsAsTenant.with_tenant(@organisation) do
+ readable = Pundit.policy_scope!(@user, @relation.where(organisation_id: @organisation.id))
+ within_reach(readable)
+ end
+ end
+
+ private
+
+ def within_reach(readable)
+ return readable if @token.nil?
+
+ reached = @token.reach.project_ids_for(@organisation.id)
+ reached.nil? ? readable : readable.where(project_id: reached)
+ end
+ end
+end
diff --git a/spec/services/tickets/readable_for_token_spec.rb b/spec/services/tickets/readable_for_token_spec.rb
new file mode 100644
index 0000000..bbd247a
--- /dev/null
+++ b/spec/services/tickets/readable_for_token_spec.rb
@@ -0,0 +1,200 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+# What a token holder can FIND: the rows of one organisation, inside the policy scope, inside the
+# token's reach. Missing, foreign, unreadable and out of reach must all look the same, so they run
+# through one list of cases; the positive cases next to them keep the negatives from being green
+# in a vacuum.
+RSpec.describe Tickets::ReadableForToken, :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_b) { new_ticket(project_b, 'Readable, out of reach') }
+ let(:hidden_ticket) { new_ticket(hidden_project, 'Unreadable') }
+ let(:foreign_ticket) { new_ticket(new_project(foreign_organisation), 'Foreign') }
+ let(:all_tickets) { [ticket_a, ticket_b, hidden_ticket] }
+
+ 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 service 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 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_for(user, **scopes)
+ Prosopite.pause { create(:personal_access_token, user: user, **scopes) }
+ end
+
+ def token_reaching_a(user)
+ token_for(user, organisations: [organisation], project_scopes: { organisation => [project_a] })
+ end
+
+ def token_reaching_everything(user)
+ token_for(user, organisations: [organisation])
+ end
+
+ def found(user, id, token: nil, relation: Ticket, tenant: organisation)
+ ActsAsTenant.with_tenant(tenant) do
+ described_class.call(user: user, organisation: organisation, relation: relation, token: token).find_by(id: id)
+ end
+ end
+
+ # One query for several rows: asking once per ticket would read as an N+1 to Prosopite.
+ def found_tickets(user, tickets, token: nil)
+ ActsAsTenant.with_tenant(organisation) do
+ described_class.call(user: user, organisation: organisation, token: token).where(id: tickets.map(&:id)).to_a
+ end
+ end
+
+ # A relation that carries no tenant condition of its own, so nothing but the service can keep a
+ # foreign row out of it.
+ def relation_without_tenant
+ ActsAsTenant.without_tenant { Ticket.all }
+ end
+
+ describe 'what cannot be found' do
+ # The reader reads A and B, the token reaches A and the hidden project: every row but A's is
+ # hidden for one reason alone, so the policy scope and the reach each carry their own case.
+ let(:token) { token_for(reader, organisations: [organisation], project_scopes: { organisation => [project_a, hidden_project] }) }
+
+ {
+ 'a missing id' => -> { SecureRandom.uuid },
+ 'a ticket of a foreign organisation' => -> { foreign_ticket.id },
+ 'a ticket the user may not read' => -> { hidden_ticket.id },
+ 'a ticket the user may read and the token does not reach' => -> { ticket_b.id }
+ }.each do |name, id|
+ it "gives the same nil for #{name}" do
+ expect(found(reader, instance_exec(&id), token: token)).to be_nil
+ end
+
+ it "gives the same nil for #{name} on a relation without a tenant" do
+ expect(found(reader, instance_exec(&id), token: token, relation: relation_without_tenant)).to be_nil
+ end
+ end
+
+ it 'finds the ticket in reach through the same call' do
+ expect(found(reader, ticket_a.id, token: token)).to eq(ticket_a)
+ end
+ end
+
+ describe 'with a token' do
+ it 'narrows an account admin to the projects the token reaches' do
+ token = token_reaching_a(admin)
+
+ expect(found(admin, ticket_a.id, token: token)).to eq(ticket_a)
+ expect(found(admin, ticket_b.id, token: token)).to be_nil
+ end
+
+ it 'narrows nothing for a token that reaches every project' do
+ token = token_reaching_everything(admin)
+
+ expect(found_tickets(admin, all_tickets, token: token)).to match_array(all_tickets)
+ end
+
+ it 'never widens what the person may read' do
+ token = token_reaching_everything(reader)
+
+ expect(found(reader, ticket_b.id, token: token)).to eq(ticket_b)
+ expect(found(reader, hidden_ticket.id, token: token)).to be_nil
+ end
+
+ it 'finds nothing when the token does not reach the organisation, never everything' do
+ token = token_for(admin, organisations: [foreign_organisation])
+
+ expect(found_tickets(admin, all_tickets, token: token)).to be_empty
+ expect(found(admin, ticket_a.id, token: token_reaching_everything(admin))).to eq(ticket_a)
+ end
+ end
+
+ describe 'without a token' do
+ it 'gives a reader what the policy scope gives' do
+ expect(found(reader, ticket_b.id)).to eq(ticket_b)
+ expect(found(reader, hidden_ticket.id)).to be_nil
+ end
+
+ it 'gives an account admin every project' do
+ expect(found_tickets(admin, all_tickets)).to match_array(all_tickets)
+ end
+ end
+
+ # An account admin's policy scope is scope.all, so here the organisation binding stands alone.
+ describe 'the organisation binding' do
+ it 'keeps a foreign ticket out for an account admin, with and without a token' do
+ expect(found(admin, ticket_a.id, relation: relation_without_tenant)).to eq(ticket_a)
+ expect(found(admin, foreign_ticket.id, relation: relation_without_tenant)).to be_nil
+
+ token = token_reaching_everything(admin)
+ expect(found(admin, ticket_a.id, token: token, relation: relation_without_tenant)).to eq(ticket_a)
+ expect(found(admin, foreign_ticket.id, token: token, relation: relation_without_tenant)).to be_nil
+ end
+
+ it 'gives the same rows whatever tenant is current when it is built and run' do
+ ticket_a
+
+ expect(found(admin, ticket_a.id, tenant: foreign_organisation)).to eq(ticket_a)
+ expect(found(admin, ticket_a.id, tenant: nil)).to eq(ticket_a)
+ expect(found(admin, foreign_ticket.id, tenant: foreign_organisation)).to be_nil
+ end
+
+ it 'never checks the rights in the current tenant instead of the organisation' do
+ Prosopite.pause do
+ ActsAsTenant.with_tenant(foreign_organisation) do
+ role = create(:admin_role, organisation: foreign_organisation)
+ create(:membership, user: reader, organisation: foreign_organisation, role: role, active: true)
+ end
+ end
+
+ expect(found(reader, hidden_ticket.id, tenant: foreign_organisation)).to be_nil
+ expect(found(reader, ticket_a.id, tenant: foreign_organisation)).to eq(ticket_a)
+ end
+ end
+
+ describe 'a narrower relation' do
+ it 'stays narrowed' do
+ relation = ActsAsTenant.with_tenant(organisation) { project_a.tickets }
+
+ expect(found(admin, ticket_a.id, relation: relation)).to eq(ticket_a)
+ expect(found(admin, ticket_b.id, relation: relation)).to be_nil
+ end
+ end
+
+ it 'answers with a relation, so the caller can narrow it further and nothing raises' do
+ relation = ActsAsTenant.with_tenant(organisation) { described_class.call(user: reader, organisation: organisation) }
+
+ expect(relation).to be_a(ActiveRecord::Relation)
+ expect(ActsAsTenant.with_tenant(organisation) { relation.where(id: ticket_a.id).first }).to eq(ticket_a)
+ expect(ActsAsTenant.with_tenant(organisation) { relation.where(id: SecureRandom.uuid).first }).to be_nil
+ end
+end