| 1 | package calls |
| 2 | |
| 3 | // Team mode's write guard: the standing a write was admitted on, checked |
| 4 | // again inside the transaction that commits it, before and after |
| 5 | // (teammode.Guard; Home implementation/teammode-package.md §3, invariant |
| 6 | // 13). The route's gate (team_access.go) decides whether a write may |
| 7 | // start; this decides whether it may commit, so a lease that lapses |
| 8 | // between the two fails the write instead of landing it. It never asks |
| 9 | // the roster: it reads the Access the context carries, whose lease check |
| 10 | // never waits, so it is safe under the one-connection SQLite writer. |
| 11 | // |
| 12 | // Whose standing a write rests on is the caller's to put on the context: |
| 13 | // |
| 14 | // - a member's own write (a meeting, a link, an ending, settings, an |
| 15 | // agent token, a device): the request's Access, put there by |
| 16 | // requirePerson; |
| 17 | // - an agent's write: its operator's, put there by requireAgent; |
| 18 | // - a guest's grant, minted by following a link: the meeting HOST's |
| 19 | // (team_host.go), because a link stands on its host. |
| 20 | // |
| 21 | // Writes nobody's standing makes — a meeting ended because its host left |
| 22 | // the team, sign-in bookkeeping, identity rows — are systemWrite or never |
| 23 | // reach the guard. Outside team mode store.team is false and every helper |
| 24 | // here is the write path exactly as it was. |
| 25 | |
| 26 | import ( |
| 27 | "context" |
| 28 | |
| 29 | "gorm.io/gorm" |
| 30 | |
| 31 | "amadan.net/oficina/home/teammode" |
| 32 | ) |
| 33 | |
| 34 | // teamGuard is teammode.Guard. A variable only so a test can let the |
| 35 | // lease run out between a write and its commit on every write route. |
| 36 | var teamGuard = teammode.Guard |
| 37 | |
| 38 | type systemWriteKey struct{} |
| 39 | |
| 40 | // systemWrite marks ctx as carrying a write that nobody's standing makes: |
| 41 | // the instance ending a meeting whose host the roster says has left. |
| 42 | func systemWrite(ctx context.Context) context.Context { |
| 43 | return context.WithValue(ctx, systemWriteKey{}, true) |
| 44 | } |
| 45 | |
| 46 | func isSystemWrite(ctx context.Context) bool { |
| 47 | v, _ := ctx.Value(systemWriteKey{}).(bool) |
| 48 | return v |
| 49 | } |
| 50 | |
| 51 | // guard runs write under the standing ctx carries. Call it INSIDE the |
| 52 | // write's transaction, with write using that transaction. Outside team |
| 53 | // mode, and for a systemWrite, it is write alone. A context with no |
| 54 | // Access reads as Unknown (teammode.Standing), so a write that lost its |
| 55 | // standing on the way here is refused. |
| 56 | func (s *store) guard(ctx context.Context, write func() error) error { |
| 57 | if !s.team || isSystemWrite(ctx) { |
| 58 | return write() |
| 59 | } |
| 60 | return teamGuard(ctx, teammode.RequireMember, write) |
| 61 | } |
| 62 | |
| 63 | // guarded runs write against the store's database exactly as it always |
| 64 | // has outside team mode (and for a systemWrite), and in team mode in one |
| 65 | // transaction, inside guard, against that transaction (never the outer |
| 66 | // handle: the writer pool has one connection). |
| 67 | func (s *store) guarded(ctx context.Context, write func(db *gorm.DB) error) error { |
| 68 | if !s.team || isSystemWrite(ctx) { |
| 69 | return write(s.g.WithContext(ctx)) |
| 70 | } |
| 71 | return s.g.WithContext(ctx).Transaction(func(tx *gorm.DB) error { |
| 72 | return s.guard(ctx, func() error { return write(tx) }) |
| 73 | }) |
| 74 | } |
| 75 | |