Store the access groups records inherit through related record paths

Merged
CH
24d ago
+1758-300

First step of listing records through related record paths. Records now store the access groups they inherit through a path, next to their own. Nothing reads the inherited groups yet: listings will in a later PR.

What changes

  • Inherited groups are stored in _accessGroupIds. When a record is recomputed, each path leading to its object adds the groups of the linked source records.
    • A regular path passes on only the groups the source owns (the groups created for the source's own object).
    • An inherent path passes on every group of the source, so access chains through inherent paths only.
    • A private or soft-deleted source passes on nothing.
  • Only objects that need it are computed. An object is computed when a role has an RLS rule on it, or when a path leads to it from a computed object (through any path if the source has a rule, through an inherent path otherwise). A workspace without RLS rules computes nothing.
  • Known record ids follow the same paths. canAccessRecords, filterGrantedRecordIds, findRecordsActions and record events now ignore a path whose source object is not computed, and need IS_ACCESS_GROUPS_ENABLED on top of IS_RELATED_RECORD_PATHS_ENABLED. This keeps them aligned with what listings will return.
  • Full recomputes propagate. After all records of an object are recomputed, the objects following it are queued. A path being created, changed or deleted, or one of the flags changing, queues every path target.
  • Record writes propagate. When a written record's groups change, or it is soft deleted, restored or destroyed, the records following it through a path are queued for recompute. Writing a hop record (a noteTarget for example) queues the records it leads to, both before and after the write. A record whose own relation changes is recomputed in its transaction.
  • Propagation stops on cycles. Recompute jobs carry the objects already recomputed and never queue one of them again.

Behaviour change to roll out deliberately

Access through a related record path now needs both flags and an RLS rule above the path:

  • IS_RELATED_RECORD_PATHS_ENABLED alone no longer grants anything through paths. IS_ACCESS_GROUPS_ENABLED must be on too, so turn access groups on first.
  • A path whose source object has no RLS rule (and is not fed by one through an inherent path) grants nothing, for known ids as for listings.
flowchart TD
  A[Group sync, path change or flag change] --> B[Recompute all records of an object]
  W[Record created or updated] --> C
  B --> C[Recompute a batch of records]
  C --> D[Own groups: RLS rules of the object]
  C --> E{Paths leading to the object<br/>from a computed source}
  E -->|regular| F[Groups the source record owns]
  E -->|inherent| G[Every group of the source record]
  D --> H[_accessGroupIds]
  F --> H
  G --> H
  B --> I[Queue the objects following it]
  I -->|own groups changed: every path<br/>inherited only: inherent paths| B
  C -->|records whose groups changed| J[Queue the records following them]
  W -->|hop record, soft delete, restore| J
  J --> C

Before / after

BeforeAfter
_accessGroupIds on a recordGroups of the RLS rules it matchesSame, plus the groups inherited through paths
Objects computedObjects with an RLS ruleObjects with an RLS rule, and objects fed by one through a path
Regular pathNot storedPasses on the source's own groups
Inherent pathNot storedPasses on all of the source's groups
Path from an object without RLS above itFollowed for known idsNot followed
Known ids with access groups offPaths followedPaths not followed
Path created, changed or deletedNothing recomputedPath targets recomputed in the background
Source record changes groups, is deleted or restoredNothing recomputedRecords following it recomputed by a job
Hop record created, moved or deletedNothing recomputedRecords it leads to recomputed by a job
Listings (find many, group by, search)Paths not followedUnchanged

Not in this PR

  • Listings do not read the inherited groups yet.
  • Following records are recomputed by a job, so they are stale until it runs, like records reached through an RLS rule relation today.
  • Making a record private goes through raw SQL and does not queue its followers yet.
  • Inherited groups left on records of an object that is no longer fed by any path are not cleaned. They cannot match: the listing will only bring a source's groups while a path feeds the object.

Tests

  • Unit: which paths feed an object, the recompute query with a regular and an inherent path, the known-ids condition with and without a source rule.
  • Integration: a task stores the rule group of its project; it loses and regains it when the project leaves and re-enters the rule, when the task is linked or unlinked, and when the project is soft deleted and restored; the path stops granting once deleted and grants again once recreated, and stops once no RLS rule restricts the source.