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
3// Team mode's per-request standing. With a.team nil nothing here runs:
4// every helper below returns today's answer unchanged.
5//
6// Standing itself is home/teammode's Access. Identity is resolved from the
7// actual session (a local address, an account link, or a Home-only
8// member); the roster is asked about the Home subject, if there is one;
9// and the answer is one of three:
10//
11// - Member: the team rung of roleFor. The roster's role says owner or
12// member; the members.team_role column is never read in team mode, so
13// a removed owner cannot keep standing through a stale row.
14// - NotMember: a guest. Only sheets shared with them by somebody else.
15// - Unknown: the roster could not be read within its lease. Shares still
16// work, and the sheet list shows them with a "partial" notice (Paul,
17// 2026-10-28); anything else that would have needed standing answers a
18// retryable 503 rather than a 404 that would claim the sheet is not
19// theirs.
20//
21// A Member decision carries a lease. teamAccess.state() checks it every
22// time it is asked, and a write checks it again inside its own
23// transaction, before and after the write (team_guard.go).
24
25import (
26 "context"
27 "errors"
28 "net/http"
29 "net/url"
30 "strconv"
31 "time"
32
33 "amadan.net/oficina/home/connect"
34 "amadan.net/oficina/home/connect/linkstore"
35 "amadan.net/oficina/home/roster"
36 "amadan.net/oficina/home/teammode"
37 "amadan.net/rastrillo/rastrillo/sessions"
38
39 "amadan.net/oficina/sheets/internal/sheets/teamstate"
40)
41
42type teamAccessKey struct{}
43
44// teamAccess is one request's standing: the member the session resolved
45// to and teammode's Access for their Home subject (Member, NotMember or
46// Unknown, with the lease re-checked and Unknown latched every time it
47// is asked). Never stored; a stream asks for a new one before each
48// delivery. A nil Access is Unknown.
49type teamAccess struct {
50 member Member
51 *teammode.Access
52}
53
54var errNoIdentity = errors.New("sheets: no team identity")
55
56func (t *teamAccess) state() roster.State {
57 if t == nil {
58 return roster.Unknown
59 }
60 return t.State()
61}
62
63// role is the team rung the roster gives: owner or member for a Member,
64// guest for anybody else, Unknown included (it reaches shares only).
65func (t *teamAccess) role() TeamRole {
66 if t == nil {
67 return TeamGuest
68 }
69 switch t.Role() {
70 case "":
71 return TeamGuest
72 case "owner":
73 return TeamOwner
74 }
75 return TeamMember
76}
77
78func teamAccessFrom(r *http.Request) (*teamAccess, bool) {
79 t, ok := r.Context().Value(teamAccessKey{}).(*teamAccess)
80 return t, ok && t != nil
81}
82
83// teamRole is roleFor's and visibleSheets' team rung. Outside team mode
84// it is the stored column, exactly as before. In team mode it is only
85// ever the roster's answer, and a member read without one (memberByID)
86// has no team standing until withStanding asks.
87func (a *App) teamRole(m Member) TeamRole {
88 if a.team == nil {
89 return TeamRole(m.TeamRole)
90 }
91 if m.team == nil {
92 return TeamGuest
93 }
94 return m.team.role()
95}
96
97// ownGrantIgnored is the Docs owner_id rule in Sheets' terms. A sheet's
98// creator holds it through a grant they gave themselves (createSheet), so
99// in team mode a self-given grant is the team rung's, not a share: it
100// counts only while the roster says Member. Grants somebody else gave
101// are shares, and keep working whatever the roster says.
102func (a *App) ownGrantIgnored(g Grant, m Member) bool {
103 return a.team != nil && g.GrantedBy == g.MemberID && a.teamRole(m) == TeamGuest
104}
105
106// homeMemberID is the member a Home subject signs in as: an account link
107// first, then a Home-only member (connect's Resolve, with the HomeOnly
108// hook ConfigureHome gives it in team mode). It never creates either.
109func (a *App) homeMemberID(ctx context.Context, subject string) (int64, error) {
110 p, err := a.home.Resolve(ctx, subject)
111 if errors.Is(err, linkstore.ErrStorage) {
112 return 0, errTeamUnavailable
113 }
114 return p.ID, err
115}
116
117// teamIdentify resolves the actual session to a member and the Home
118// subject the roster knows them by ("" when there is none: a local
119// account with no link, which is no team standing).
120func (a *App) teamIdentify(ctx context.Context, s sessions.Session) (Member, string, error) {
121 switch {
122 case connect.IsHomeDerived(s.Method):
123 // A browser, native or native-web sign-in through Home. The two
124 // native kinds end seven days after they were minted, however
125 // recently used (connect.Expired); this resolves by subject, not
126 // through ResolveSession, so it refuses what ResolveSession would.
127 if connect.Expired(connect.Session{Subject: s.Subject, Method: s.Method, AuthTime: s.AuthTime, At: s.At}, time.Now()) {
128 return Member{}, "", errNoIdentity
129 }
130 id, err := a.homeMemberID(ctx, s.Subject)
131 if err != nil {
132 if errors.Is(err, errTeamUnavailable) {
133 return Member{}, "", err
134 }
135 return Member{}, "", errNoIdentity
136 }
137 m, ok := a.memberByID(uint64(id))
138 if !ok {
139 return Member{}, "", errTeamUnavailable
140 }
141 return m, s.Subject, nil
142 case s.Method == "magiclink" || s.Method == "keymail":
143 // A local session was admitted at sign-in (Admits). Its member is
144 // created on first sight exactly as actor() always has.
145 m, err := a.memberFor(s.Subject)
146 if err != nil {
147 return Member{}, "", errTeamUnavailable
148 }
149 subject, err := teamstate.HomeSubject(ctx, a.team.db, a.team.issuer(), int64(m.ID))
150 if errors.Is(err, teamstate.ErrRefused) {
151 return m, "", nil
152 }
153 if err != nil {
154 return Member{}, "", errTeamUnavailable
155 }
156 return m, subject, nil
157 }
158 return Member{}, "", errNoIdentity
159}
160
161// resolveTeamAccess asks everything afresh: session, member, roster.
162// The roster call may wait for a refresh, so never call it holding the
163// SQLite writer.
164func (a *App) resolveTeamAccess(r *http.Request) (*teamAccess, error) {
165 s, ok := a.homeSessions.From(r)
166 if !ok {
167 return nil, errNoIdentity
168 }
169 m, subject, err := a.teamIdentify(r.Context(), s)
170 if err != nil {
171 return nil, err
172 }
173 return &teamAccess{member: m, Access: a.team.Access(r.Context(), subject)}, nil
174}
175
176// actor is the member a handler acts as: the stored row with the
177// request's standing attached.
178func (t *teamAccess) actor() Member {
179 m := t.member
180 m.team = t
181 m.TeamRole = string(t.role())
182 return m
183}
184
185// withStanding asks the roster about somebody other than the viewer —
186// the share screen's people and its revoke notice — so what it says
187// about them is what roleFor would decide for them.
188func (a *App) withStanding(ctx context.Context, m Member) Member {
189 if a.team == nil {
190 return m
191 }
192 t := &teamAccess{member: m}
193 subject, err := teamstate.HomeSubject(ctx, a.team.db, a.team.issuer(), int64(m.ID))
194 switch {
195 case err == nil:
196 t.Access = a.team.Access(ctx, subject)
197 case errors.Is(err, teamstate.ErrRefused):
198 t.Access = a.team.Access(ctx, "") // no Home identity: NotMember, unasked
199 }
200 // Anything else leaves Access nil, which is Unknown.
201 return t.actor()
202}
203
204// asGuest is m as the share rung alone sees them: no team standing, so
205// roleFor reaches only what somebody else shared with them. It never asks
206// the roster. writeGuard uses it to tell a write a share allows from one
207// that rests on standing.
208func (a *App) asGuest(ctx context.Context, m Member) Member {
209 return (&teamAccess{member: m, Access: a.team.Access(ctx, "")}).actor()
210}
211
212// teamJobOwner is the jobs registry's owner key in team mode. A
213// Home-only member has no address to key on, so every team-mode session
214// keys on its member instead; both job routes read it from the same
215// middleware.
216func teamJobOwner(id uint64) string { return "sheets-member:" + strconv.FormatUint(id, 10) }
217
218// teamRequire is homeRequire for team mode: the session must resolve to
219// a member, and the request carries that member's roster standing. It
220// does not sign anybody out for lacking standing: a removed member keeps
221// a session that reaches only what was shared with them.
222func (a *App) teamRequire(next http.Handler) http.Handler {
223 return a.homeSessions.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
224 session, ok := a.homeSessions.From(r)
225 var t *teamAccess
226 err := errNoIdentity
227 if ok {
228 t, err = a.resolveTeamAccess(r)
229 }
230 if errors.Is(err, errTeamUnavailable) {
231 w.Header().Set("Cache-Control", "no-store")
232 http.Error(w, "Sign-in unavailable.", http.StatusServiceUnavailable)
233 return
234 }
235 if err != nil {
236 if ok {
237 a.homeSessions.SignOut(w, r)
238 }
239 w.Header().Set("Cache-Control", "no-store")
240 if r.Method == http.MethodGet || r.Method == http.MethodHead {
241 http.Redirect(w, r, "/signin?return_to="+url.QueryEscape(r.URL.RequestURI()), http.StatusSeeOther)
242 } else {
243 http.Error(w, "Sign in first.", http.StatusForbidden)
244 }
245 return
246 }
247 session.Subject = teamJobOwner(t.member.ID)
248 // Twice: Sheets' own handlers read the member with it, and
249 // teammode.Guard reads the same Access inside a write's
250 // transaction (team_guard.go), so both see one latched standing.
251 ctx := teammode.WithAccess(context.WithValue(r.Context(), teamAccessKey{}, t), t.Access)
252 r = r.WithContext(ctx)
253 next.ServeHTTP(w, sessions.WithSession(r, session))
254 }))
255}
256