oficina / sheets Public

Clone
git clone https://amadan.net/oficina/sheets

Plain git — no account needed to clone.

Download

Download this file

1package sheets
2
3import "time"
4
5// Member is one person who may hold a session on this instance, and the
6// stable uint64 an edit records as its actor.
7//
8// Address is plaintext, deliberately. Spec §2.2 lists "membership,
9// grants, and roles — including readable email addresses" among the
10// things this design does NOT seal, and publishes that in the README
11// rather than leaving it to be discovered. Sealing it would also make
12// the uniqueIndex below impossible: seal uses a random nonce per call,
13// so the same address seals to different ciphertext every time and no
14// index could ever match one.
15//
16// This table exists because the session carries an address and every
17// authored column — Sheet.CreatedBy, Cell.UpdatedBy, Event.ActorID — is
18// a uint64. sessions.UserID cannot bridge the two: it returns
19// (0, false) for an email Subject, and dropping that ok scopes every
20// query to uid 0 (AGENTS.md / Mechanical rules).
21type Member struct {
22 ID uint64 `gorm:"primaryKey"`
23 Address string `gorm:"uniqueIndex;size:320"`
24 // TeamRole is this person's standing on the instance. It defaults
25 // to guest for anyone who arrives by being granted a single sheet;
26 // SHEETS_OWNERS promotes to owner at first sight. A guest is the
27 // safe default because the wrong direction is unrecoverable in the
28 // way that matters: a guest wrongly made a member sees every sheet.
29 TeamRole string `gorm:"size:16"`
30 CreatedAt time.Time
31
32 // team is the request's roster standing in team mode (team_access.go),
33 // attached by actor and withStanding and never stored: unexported, so
34 // GORM does not map it. Nil outside team mode.
35 team *teamAccess
36}
37
38// TeamRole is a person's standing on this instance as a whole.
39//
40// Spec §8.2: owner, member, guest. A guest holds no team-wide rights —
41// they see exactly the sheets granted to them and learn nothing about
42// the team beyond §9.5's disclosure. That is the whole point of the
43// distinction, and it is why a guest cannot be given a team role by
44// accident: guests arrive by being granted one sheet.
45type TeamRole string
46
47const (
48 TeamOwner TeamRole = "owner"
49 TeamMember TeamRole = "member"
50 TeamGuest TeamRole = "guest"
51)
52
53// SheetRole is a person's standing on one sheet.
54//
55// Spec §8.2: owner, editor, commenter, viewer. Ordered, and the order
56// is load-bearing — see Allows, which is how every route asks its
57// question.
58type SheetRole string
59
60const (
61 SheetOwner SheetRole = "owner"
62 SheetEditor SheetRole = "editor"
63 SheetCommenter SheetRole = "commenter"
64 SheetViewer SheetRole = "viewer"
65)
66
67// rank orders sheet roles from most to least able. Unknown roles rank
68// below every real one, so a role this build does not recognise — a row
69// written by a newer version — grants nothing rather than everything.
70func (r SheetRole) rank() int {
71 switch r {
72 case SheetOwner:
73 return 4
74 case SheetEditor:
75 return 3
76 case SheetCommenter:
77 return 2
78 case SheetViewer:
79 return 1
80 }
81 return 0
82}
83
84// Allows reports whether this role is at least as able as want.
85//
86// Every authorization question in the app is this one function, so
87// there is one place to read and one place to get it wrong. Callers ask
88// `role.Allows(SheetEditor)`, never `role == SheetEditor` — the second
89// spelling silently excludes owners, and would do it on whichever route
90// somebody forgot.
91func (r SheetRole) Allows(want SheetRole) bool { return r.rank() >= want.rank() }
92
93// Valid reports whether a role is one this build knows.
94func (r SheetRole) Valid() bool { return r.rank() > 0 }
95
96// Grant is one person's access to one sheet.
97//
98// This table is the authority. Spec §9.1.1: every instance re-checks
99// authorization on every request, whatever the directory says — the
100// directory answers "where might I have things", never "may I have
101// this".
102//
103// Plaintext, per spec §2.2, which lists membership, grants and roles
104// among what this design deliberately does not seal.
105//
106// (SheetID, MemberID) is a uniqueIndex: a person has one role on a
107// sheet. Two rows would make "what may they do" depend on which the
108// query happened to return first.
109type Grant struct {
110 ID uint64 `gorm:"primaryKey"`
111 SheetID uint64 `gorm:"uniqueIndex:idx_grant_sheet_member;index"`
112 MemberID uint64 `gorm:"uniqueIndex:idx_grant_sheet_member"`
113 Role string `gorm:"size:16"`
114 GrantedBy uint64
115 CreatedAt time.Time
116 UpdatedAt time.Time
117}
118
119// ShareLink is a capability: whoever holds the token may read the
120// sheet, with no account and no session.
121//
122// **TokenHash, never the token.** Spec §8.3, following Pegamento's
123// service contract: stored plaintext, the replicated database becomes a
124// bundle of live read capabilities to sealed sheets — §0 defeated
125// through a side door for the second time. So the token is shown once,
126// at creation, and never again; what is stored is only enough to
127// recognise it coming back.
128//
129// The hash is SHA-256 rather than bcrypt or argon2, and that is a
130// deliberate difference from how a password would be stored. A password
131// is low-entropy and needs a slow hash to survive being guessed. This
132// token is 32 bytes from crypto/rand, so guessing is not a threat model
133// and a slow hash would buy nothing while making every page load do
134// key-stretching work. The property that matters here — the stored form
135// does not yield the token — a fast hash gives just as completely.
136//
137// RevokedAt rather than a delete, because "this link was turned off"
138// and "this link never existed" are different answers to give somebody
139// holding a stale URL, and the first is the kinder one.
140type ShareLink struct {
141 ID uint64 `gorm:"primaryKey"`
142 SheetID uint64 `gorm:"index"`
143 TokenHash string `gorm:"uniqueIndex;size:64"`
144 // Role is viewer or commenter, never editor (ruling 12). Every
145 // version row carries an actor, and an anonymous editor would write
146 // unattributed rows into version history.
147 Role string `gorm:"size:16"`
148 CreatedBy uint64
149 CreatedAt time.Time
150 RevokedAt *time.Time
151}
152
153// Comment is a note somebody wrote about one cell.
154//
155// **Anchored on stable row and column ids, never on A1 coordinates.**
156// Spec §4.2 names this as one of the three things stable identity is
157// load-bearing for: a comment stays attached to the cell it was written
158// about when a row is inserted above it. An anchor of "B7" would slide
159// onto a different cell the first time anybody inserted a row.
160//
161// BodySealed because comment text is user content — spec §2.1 lists it
162// beside cell values and sheet titles.
163//
164// A thread is every comment sharing a cell, ordered by id. That is the
165// **local** implementation §13 pre-decided as the fallback: comments
166// were to consume Docs' comment-thread primitive, Docs owns that design
167// because its anchoring case is harder, and it has not shipped. Deciding
168// the fallback in advance is what makes this a choice rather than a
169// scramble; adopting theirs later replaces the rendering, not the
170// anchor.
171//
172// ResolvedAt marks a thread dealt with. Nulled rather than deleted:
173// "we discussed this and settled it" is worth keeping, and a resolved
174// thread is the most useful thing to read when the same question comes
175// back.
176type Comment struct {
177 ID uint64 `gorm:"primaryKey"`
178 TabID uint64 `gorm:"index"`
179 RowID, ColID uint64
180 AuthorID uint64
181 BodySealed []byte // seal purpose "comment.body"
182 CreatedAt time.Time
183 ResolvedAt *time.Time
184 ResolvedBy uint64
185}
186
187// TabView is a tab's persisted sort and filter.
188//
189// SpecSealed, because a filter value IS a cell value — spec §2.1 lists
190// "persisted sort and filter criteria" among what this design seals,
191// for exactly that reason. A filter reading `> 50000` against a salary
192// column is the number it is filtering on.
193//
194// One row per tab (uniqueIndex): a tab has one view, and two rows would
195// make "how is this sorted" depend on which the query returned first.
196type TabView struct {
197 ID uint64 `gorm:"primaryKey"`
198 TabID uint64 `gorm:"uniqueIndex"`
199 SpecSealed []byte // seal purpose "tab.view"
200 UpdatedBy uint64
201 UpdatedAt time.Time
202}
203