| 1 | package sheets |
| 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; Fable's deviation 7 on oficina/sheets #52). The |
| 6 | // handler's roleFor decides whether the write may start; this decides |
| 7 | // whether it may commit, so a lease that lapses between the two fails the |
| 8 | // write instead of landing it. It never asks the roster: it reads the |
| 9 | // Access teamRequire put on the request, whose lease check never waits, |
| 10 | // so it is safe under the one-connection SQLite writer. |
| 11 | // |
| 12 | // Outside team mode every guard here is nil, and a nil store.Guard is the |
| 13 | // write path exactly as it was. |
| 14 | |
| 15 | import ( |
| 16 | "context" |
| 17 | "errors" |
| 18 | "net/http" |
| 19 | |
| 20 | "gorm.io/gorm" |
| 21 | |
| 22 | "amadan.net/oficina/home/roster" |
| 23 | "amadan.net/oficina/home/teammode" |
| 24 | |
| 25 | "amadan.net/oficina/sheets/internal/sheets/store" |
| 26 | ) |
| 27 | |
| 28 | // writeGuard is the guard for m writing to a sheet with a role of at |
| 29 | // least need, which roleFor has already granted. When a share somebody |
| 30 | // else gave reaches need on its own, the write does not rest on |
| 31 | // standing, and Unknown may still make it (the rule teammode leaves to |
| 32 | // the app). Otherwise the role came from the team rung — standing, the |
| 33 | // creator's own grant, or a roster owner's orphaned sheet — and every one |
| 34 | // of those holds only while the roster says Member. |
| 35 | func (a *App) writeGuard(ctx context.Context, sheetID uint64, m Member, need SheetRole) store.Guard { |
| 36 | if a.team == nil { |
| 37 | return nil |
| 38 | } |
| 39 | authorize := teammode.RequireMember |
| 40 | if role, ok := a.roleFor(sheetID, a.asGuest(ctx, m)); ok && role.Allows(need) { |
| 41 | authorize = func(roster.State) error { return nil } |
| 42 | } |
| 43 | return func(write func() error) error { return teamGuard(ctx, authorize, write) } |
| 44 | } |
| 45 | |
| 46 | // memberGuard is the guard for a write only team members may make at |
| 47 | // all (create, import). |
| 48 | func (a *App) memberGuard(ctx context.Context) store.Guard { |
| 49 | if a.team == nil { |
| 50 | return nil |
| 51 | } |
| 52 | return func(write func() error) error { return teamGuard(ctx, teammode.RequireMember, write) } |
| 53 | } |
| 54 | |
| 55 | // teamGuard is teammode.Guard. A variable only so a test can let the lease |
| 56 | // run out between the write and its commit on every write route. |
| 57 | var teamGuard = teammode.Guard |
| 58 | |
| 59 | // guarded runs write against the app's database exactly as it always |
| 60 | // has when g is nil, and otherwise in one transaction, inside g, against |
| 61 | // that transaction (never the outer handle: the writer pool has one |
| 62 | // connection). |
| 63 | func (a *App) guarded(g store.Guard, write func(db *gorm.DB) error) error { |
| 64 | if g == nil { |
| 65 | return write(a.db) |
| 66 | } |
| 67 | return a.db.Transaction(func(tx *gorm.DB) error { |
| 68 | return g(func() error { return write(tx) }) |
| 69 | }) |
| 70 | } |
| 71 | |
| 72 | // guardRefused answers a write the guard refused, and reports whether it |
| 73 | // did. Standing lapsed between the handler's check and the commit: the |
| 74 | // same retryable 503 as Unknown anywhere else, and a 404 for a definite |
| 75 | // NotMember (which one request's Access cannot reach once it has said |
| 76 | // Member, but costs nothing to answer correctly). Outside team mode no |
| 77 | // error is ever one of these. |
| 78 | func (a *App) guardRefused(w http.ResponseWriter, r *http.Request, err error) bool { |
| 79 | switch { |
| 80 | case errors.Is(err, teammode.ErrUnavailable): |
| 81 | a.unavailable(w, r) |
| 82 | case errors.Is(err, teammode.ErrNotMember): |
| 83 | a.answer(w, r, http.StatusNotFound, kindSheet, "Not found.") |
| 84 | default: |
| 85 | return false |
| 86 | } |
| 87 | return true |
| 88 | } |
| 89 | |