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.

Ask a token's reach before the policy scope

Guido Boykee30fcbcd4ead
diff --git a/app/services/tickets/readable_for_token.rb b/app/services/tickets/readable_for_token.rb
index 9e7265f..a8a9391 100644
--- a/app/services/tickets/readable_for_token.rb
+++ b/app/services/tickets/readable_for_token.rb
@@ -12,6 +12,10 @@
# 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.
#
+# The reach comes before the policy scope. An organisation the token no longer reaches (a
+# deactivated membership, say) is not asked about at all: an empty reach answers with an empty
+# relation and no rights are looked up.
+#
# 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
@@ -32,18 +36,20 @@ module Tickets
def call
ActsAsTenant.with_tenant(@organisation) do
- readable = Pundit.policy_scope!(@user, @relation.where(organisation_id: @organisation.id))
- within_reach(readable)
+ rows = @relation.where(organisation_id: @organisation.id)
+ reached = reached_project_ids
+ next rows.none if reached == []
+
+ readable = Pundit.policy_scope!(@user, rows)
+ reached.nil? ? readable : readable.where(project_id: reached)
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)
+ # nil: no limit from a token, either because there is none or because it reaches every project.
+ def reached_project_ids
+ @token&.reach&.project_ids_for(@organisation.id)
end
end
end
diff --git a/spec/services/tickets/readable_for_token_spec.rb b/spec/services/tickets/readable_for_token_spec.rb
index bbd247a..a444d28 100644
--- a/spec/services/tickets/readable_for_token_spec.rb
+++ b/spec/services/tickets/readable_for_token_spec.rb
@@ -138,6 +138,31 @@ RSpec.describe Tickets::ReadableForToken, :ci_guard do
end
end
+ describe 'an empty reach' do
+ {
+ 'a token outside the organisation' => -> { token_for(admin, organisations: [foreign_organisation]) },
+ 'a token of the organisation with an empty project list' => lambda {
+ token_for(admin, organisations: [organisation], project_scopes: { organisation => [] })
+ }
+ }.each do |name, build_token|
+ it "asks no rights for #{name} and finds nothing" do
+ token = instance_exec(&build_token)
+ tickets = all_tickets
+ expect(Pundit).not_to receive(:policy_scope!)
+
+ expect(found_tickets(admin, tickets, token: token)).to be_empty
+ end
+ end
+
+ it 'does ask the rights for a token that reaches something' do
+ token = token_reaching_a(admin)
+ tickets = all_tickets
+ expect(Pundit).to receive(:policy_scope!).once.and_call_original
+
+ expect(found_tickets(admin, tickets, token: token)).to contain_exactly(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)