| 1 | package slides |
| 2 | |
| 3 | // Team mode's write guard: the request's standing checked again inside |
| 4 | // the transaction that commits a write, before and after it |
| 5 | // (teammode.Guard; Home implementation/teammode-package.md §3, |
| 6 | // invariant 13, and Fable's deviation 7 on oficina/sheets #52). The |
| 7 | // handler's deckFor or ownedDeckFor decides whether the write may start; |
| 8 | // this decides whether it may commit, so a lease that lapses between the |
| 9 | // two fails the write instead of landing it. It never asks the roster: it |
| 10 | // reads the Access the request's user carries, whose lease check never |
| 11 | // waits, so it is safe under the one-connection SQLite writer. |
| 12 | // |
| 13 | // Outside team mode every guard here is nil, and a nil writeGuard is the |
| 14 | // write path exactly as it was. |
| 15 | |
| 16 | import ( |
| 17 | "context" |
| 18 | "errors" |
| 19 | "net/http" |
| 20 | |
| 21 | "gorm.io/gorm" |
| 22 | |
| 23 | "amadan.net/oficina/home/roster" |
| 24 | "amadan.net/oficina/home/teammode" |
| 25 | ) |
| 26 | |
| 27 | // writeGuard runs one write inside that write's own transaction, and may |
| 28 | // refuse it, before the write or after it. A refusal rolls the |
| 29 | // transaction back. Nil is no guard. |
| 30 | type writeGuard func(write func() error) error |
| 31 | |
| 32 | // run is write under g, or write alone when there is no guard. |
| 33 | func (g writeGuard) run(write func() error) error { |
| 34 | if g == nil { |
| 35 | return write() |
| 36 | } |
| 37 | return g(write) |
| 38 | } |
| 39 | |
| 40 | // teamGuard is teammode.Guard. A variable only so a test can let the |
| 41 | // lease run out between a write and its commit on every write route. |
| 42 | var teamGuard = teammode.Guard |
| 43 | |
| 44 | // guardFor is the guard for u under authorize, reading u's own Access: the |
| 45 | // one the handler's deckFor latched. A user with none reads Unknown. |
| 46 | func (a *app) guardFor(ctx context.Context, u User, authorize func(roster.State) error) writeGuard { |
| 47 | if a.team == nil { |
| 48 | return nil |
| 49 | } |
| 50 | gctx := teammode.WithAccess(ctx, u.team.access()) |
| 51 | return func(write func() error) error { return teamGuard(gctx, authorize, write) } |
| 52 | } |
| 53 | |
| 54 | // deckGuard is the guard for u writing to d, a write any reader of d may |
| 55 | // make (the document, a presentation, an upload). When a grant names u on |
| 56 | // d, the write does not rest on standing, and Unknown or NotMember may |
| 57 | // still make it (the rule teammode leaves to the app). Otherwise d was |
| 58 | // reached through the team rung — u's own deck, or one on the team rung |
| 59 | // — and that holds only while the roster says Member. |
| 60 | // |
| 61 | // The grant is read here, before any transaction, never under the |
| 62 | // writer. |
| 63 | func (a *app) deckGuard(ctx context.Context, u User, d Deck) writeGuard { |
| 64 | if a.team == nil { |
| 65 | return nil |
| 66 | } |
| 67 | if a.grantedTo(ctx, u, d) { |
| 68 | return a.guardFor(ctx, u, func(roster.State) error { return nil }) |
| 69 | } |
| 70 | return a.guardFor(ctx, u, teammode.RequireMember) |
| 71 | } |
| 72 | |
| 73 | // memberGuard is the guard for a write that rests on standing alone: |
| 74 | // making a deck, and everything an owner does (settings, delete, share |
| 75 | // links), since ownership in team mode is the team rung's too — a deck's |
| 76 | // owner owns it only while Member, and a roster owner's hold on an |
| 77 | // orphaned deck is the roster's (team_owner.go). |
| 78 | func (a *app) memberGuard(ctx context.Context, u User) writeGuard { |
| 79 | return a.guardFor(ctx, u, teammode.RequireMember) |
| 80 | } |
| 81 | |
| 82 | // grantedTo reports whether a grant names u on d. A read failure is "no": |
| 83 | // the write then needs standing, which is the stricter answer. |
| 84 | func (a *app) grantedTo(ctx context.Context, u User, d Deck) bool { |
| 85 | var n int64 |
| 86 | if err := a.db.G.WithContext(ctx).Model(&DeckGrant{}). |
| 87 | Where("deck_id = ? AND user_id = ?", d.ID, u.ID).Count(&n).Error; err != nil { |
| 88 | return false |
| 89 | } |
| 90 | return n > 0 |
| 91 | } |
| 92 | |
| 93 | // guarded runs write against the app's database exactly as it always has |
| 94 | // when g is nil, and otherwise in one transaction, inside g, against that |
| 95 | // transaction (never the outer handle: the writer pool has one |
| 96 | // connection). |
| 97 | func (a *app) guarded(ctx context.Context, g writeGuard, write func(db *gorm.DB) error) error { |
| 98 | if g == nil { |
| 99 | return write(a.db.G.WithContext(ctx)) |
| 100 | } |
| 101 | return a.db.G.WithContext(ctx).Transaction(func(tx *gorm.DB) error { |
| 102 | return g(func() error { return write(tx) }) |
| 103 | }) |
| 104 | } |
| 105 | |
| 106 | // guardRefused answers a write the guard refused, and reports whether it |
| 107 | // did. Standing lapsed between the handler's check and the commit: the |
| 108 | // same retryable 503 as Unknown anywhere else, and a 404 for a definite |
| 109 | // NotMember (which one request's Access cannot reach once it has said |
| 110 | // Member, but costs nothing to answer correctly). Outside team mode no |
| 111 | // error is ever one of these. |
| 112 | func (a *app) guardRefused(w http.ResponseWriter, r *http.Request, err error) bool { |
| 113 | switch { |
| 114 | case errors.Is(err, teammode.ErrUnavailable): |
| 115 | a.teamUnavailable(w, r) |
| 116 | case errors.Is(err, teammode.ErrNotMember): |
| 117 | a.notFound(w, r) |
| 118 | default: |
| 119 | return false |
| 120 | } |
| 121 | return true |
| 122 | } |
| 123 | |
| 124 | // guardRefusedJSON is guardRefused for the drive API. |
| 125 | func guardRefusedJSON(w http.ResponseWriter, err error) bool { |
| 126 | switch { |
| 127 | case errors.Is(err, teammode.ErrUnavailable): |
| 128 | teamUnavailableJSON(w) |
| 129 | case errors.Is(err, teammode.ErrNotMember): |
| 130 | apiFail(w, http.StatusNotFound, errNotFound) |
| 131 | default: |
| 132 | return false |
| 133 | } |
| 134 | return true |
| 135 | } |
| 136 | |