Apply account permissions consistently to imports, rules and transaction references

DG
11d ago
+1753-151

Stacked on #5413; review after it.

Every write is now checked once, with the permissions of whoever owns it.

  • Rules have an owner (rules.user_id; admins can reassign it). A rule changes only accounts its owner may change (annotate access if it only sets category, merchant or tags, write access otherwise). Admin-owned and ownerless rules act on the whole family, as before.
  • Only the owner or an admin can edit, switch, apply or delete a rule. Running or deleting all rules is admin-only. A leaving member's rules are deleted; a leaving admin's become ownerless. The rules API and get_rules show the owner; owner changes go to the security audit log.
  • Imports: publishing (web, API, sessions) needs write access to every account touched, and an admin when the import contains rules. Publishing and wizard edits share a lock on the import; a published import can't be edited. Only a session's creator or an admin can add to or publish it. Exports carry the rule owner; an unwritable transfer action is dropped and counted. rules.csv leaves member-owned rules out, as the export dialog notes.
  • Transactions: category, merchant and tags must belong to the entry's family.
  • Guests don't create rules from Categorize.

Summary by cubic

Closes write-path gaps where imports, rules, and transaction references skipped the account checks the web UI makes.

Rules

A rule now has an owner and changes only transactions in accounts the owner can write — annotate level when every action only annotates (including entry attributes like notes), write level otherwise; admin-owned or ownerless rules act on the whole family as before. Only the owner or an admin can manage a rule; running or deleting all rules is admin-only, and admins can reassign the owner in the form. When a member leaves the family their rules are deleted; an admin's rules lose their owner and stay acting on the whole family. Owner changes are logged, and the rules API, the assistant, and the form report the owner and scope.

Imports and references

Publishing (web, API, sessions) requires write access to every account touched, and an admin when the import contains rules; edits and publishing share a lock on the import, so a late edit can't write after it's claimed — either side gives up with a try-again message, and the publisher is recorded. Only a session's creator or an admin can add chunks or publish; sessions opened before creators were recorded are admin-only. An API publish that is refused or fails to enqueue keeps the import pending and returns its import_id, so a retrying client doesn't duplicate it. Exports carry the owner and scope; imports give a member what they import, an admin keeps the exported owner when in the family, a restricted rule for a missing owner is skipped and counted, and a transfer target the owner can't write is dropped and counted. Category, merchant, and tags must belong to the transaction's family, and guests no longer create rules from Categorize; the transfer editor looks its category up through the family, refuses foreign ones, and clears and locks when none is submitted.

Summary by CodeRabbit

  • New Features
    • Rules created or enabled by family members are limited to accounts they can edit. Admins can widen restricted rules to the whole family.
    • Rules needing review are identified and excluded from rule runs until addressed.
    • Import publishing checks account access and rule-related permissions. Import sessions can only be managed by their creator or an admin.
  • Bug Fixes
    • Published imports can no longer be changed.
    • Prevented transaction categories, merchants, and tags from being assigned across families.
    • Improved account access checks for bank statement imports and transfer rule actions.
    • Corrected merchant selection to exclude unrelated family merchants and support clearing a transfer’s category.