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 (
4 "context"
5 "errors"
6 "net/http"
7 "strconv"
8 "time"
9)
10
11// shareView is the share screen's payload.
12type shareView struct {
13 Sheet SheetSummary
14 People []sharedWith
15 Roles []SheetRole
16 LinkRoles []SheetRole
17 Links []linkRow
18 Notice string
19 // NoticeTone is the callout's rastrillo tone (shared chrome spec §4).
20 // Not sent to the native apps, which read the notice code instead.
21 NoticeTone string `json:"-"`
22 // NewLink is a full URL, shown once. It is not stored anywhere, so
23 // a reload of this screen cannot show it again — which is the
24 // direct consequence of storing only the hash, and the screen says
25 // so rather than letting somebody discover it.
26 NewLink string
27}
28
29// linkRow is one live link, described without its token.
30type linkRow struct {
31 ID uint64
32 Role SheetRole
33 Created time.Time
34}
35
36// shareableRoles is what the screen offers, LEAST able first.
37//
38// The order is the security control. A <select> with no selected option
39// defaults to its first, and this list is rendered straight into one, so
40// whichever role leads is what a person shares by default — including
41// the person who fills in an address, presses Share, and never opens the
42// dropdown at all. Most-able-first made that default "owner": the
43// quickest possible path through this screen handed the sheet away, and
44// the only repair is another owner taking it back.
45//
46// Least-able-first makes the same path grant "viewer", and anything
47// more has to be chosen. That is the same instinct the spec applies to
48// link sharing, which is viewer-or-commenter and never editor.
49//
50// Owner is still offered: handing a sheet on is a real thing to want,
51// and leaving it out means the only way to transfer one is a database
52// edit. It is last because it is the one that cannot be undone by the
53// person who did it.
54var shareableRoles = []SheetRole{SheetViewer, SheetCommenter, SheetEditor, SheetOwner}
55
56// handleShare renders who can reach a sheet.
57func (a *App) handleShare(w http.ResponseWriter, r *http.Request) {
58 s, viewer, ok := a.sheetForSharing(w, r)
59 if !ok {
60 return
61 }
62 a.renderShare(w, r, s, viewer, r.URL.Query().Get("notice"), "")
63}
64
65// renderShare draws the screen. newLink is a token in readable form,
66// and it arrives as an argument rather than out of the request for the
67// reason handleLinkCreate gives: the only caller that has one is the
68// handler that just minted it, in the same process, on the same
69// goroutine. Nothing that crossed a URL can reach this parameter.
70func (a *App) renderShare(w http.ResponseWriter, r *http.Request, s SheetSummary, _ Member, notice, newLink string) {
71 people, err := a.grantsOn(r.Context(), s.ID)
72 if err != nil {
73 a.fail(w, r, err)
74 return
75 }
76 links, err := a.linksOn(s.ID)
77 if err != nil {
78 a.fail(w, r, err)
79 return
80 }
81 rows := make([]linkRow, 0, len(links))
82 for _, l := range links {
83 rows = append(rows, linkRow{ID: l.ID, Role: SheetRole(l.Role), Created: l.CreatedAt})
84 }
85
86 view := shareView{
87 Sheet: s, People: people, Roles: shareableRoles,
88 LinkRoles: linkRoles, Links: rows, Notice: noticeText(notice),
89 }
90 if notice == noticeRevokedTeam {
91 view.Notice = a.revokedTeamNotice(r, s.ID)
92 }
93 view.NoticeTone = noticeTone(notice)
94 if newLink != "" {
95 view.NewLink = a.linkURL(newLink)
96 // The new link's own callout says "copy it now" with the link
97 // in it; a second callout above it saying the same thing is the
98 // duplicate the audit found (§4: keep one, a warning).
99 if notice == "link" {
100 view.Notice = ""
101 }
102 }
103 f := onSheet(s, "Share")
104 f.ret = "/sheets/" + strconv.FormatUint(s.ID, 10) + "/share"
105 a.render(w, r, "share", f, http.StatusOK, view)
106}
107
108// noticeText maps a code to a fixed sentence.
109//
110// A code, never the text itself: the value comes from a query string,
111// and a page that echoed it would take a redirect parameter straight
112// into the document. This is the same shape signinMessage uses, for the
113// same reason.
114func noticeText(code string) string {
115 switch code {
116 case "shared":
117 return "Shared. They can sign in with that address to open it."
118 case "revoked":
119 return "Access removed."
120 case noticeRevokedTeam:
121 // renderShare replaces this with the named version when it can
122 // resolve who the revoke was about. This is the honest sentence
123 // when it cannot.
124 return revokedTeamText
125 case "bad-address":
126 return "That does not look like an email address."
127 case "bad-role":
128 return "Unknown role."
129 case "last-owner":
130 return "A sheet needs an owner. Make somebody else the owner first."
131 case "link":
132 return "Link created. Copy it now — it is not stored, so this is the only time it can be shown."
133 case "link-revoked":
134 return "Link turned off. Anyone holding it now gets nothing."
135 case "bad-link-role":
136 return "A link can only view or comment, never edit."
137 default:
138 return ""
139 }
140}
141
142// handleShareGrant adds or changes somebody's access.
143func (a *App) handleShareGrant(w http.ResponseWriter, r *http.Request) {
144 s, viewer, ok := a.sheetForSharing(w, r)
145 if !ok {
146 return
147 }
148 role := SheetRole(r.FormValue("role"))
149 if !role.Valid() {
150 a.shareRedirect(w, r, s, "bad-role")
151 return
152 }
153 if _, err := a.grant(a.writeGuard(r.Context(), s.ID, viewer, SheetOwner), s.ID, r.FormValue("address"), role, viewer); err != nil {
154 if a.guardRefused(w, r, err) {
155 return
156 }
157 // Demoting the last owner is refused, and says so. Told
158 // "that does not look like an email address" instead, a person
159 // would retype their own address and try again — the one
160 // response that cannot help.
161 if errors.Is(err, errLastOwner) {
162 a.shareRedirect(w, r, s, "last-owner")
163 return
164 }
165 // The address is not echoed back. It is somebody's email, and
166 // an error string is a log line waiting to happen.
167 a.shareRedirect(w, r, s, "bad-address")
168 return
169 }
170 a.shareRedirect(w, r, s, "shared")
171}
172
173// handleShareRevoke removes somebody's access.
174func (a *App) handleShareRevoke(w http.ResponseWriter, r *http.Request) {
175 s, viewer, ok := a.sheetForSharing(w, r)
176 if !ok {
177 return
178 }
179 memberID, err := strconv.ParseUint(r.FormValue("member"), 10, 64)
180 if err != nil || memberID == 0 {
181 a.notFound(w, r)
182 return
183 }
184 // A sheet with no owner is a sheet nobody can ever share again, and
185 // there is no admin path to repair it. Refusing the last one is the
186 // difference between a mistake and an unrecoverable mistake.
187 //
188 // The check is inside revoke's own transaction rather than here.
189 // Here it was a read separated from its delete by the rest of this
190 // handler, so two owners revoking each other at the same moment both
191 // saw two owners and both deleted.
192 if err := a.revoke(a.writeGuard(r.Context(), s.ID, viewer, SheetOwner), s.ID, memberID); err != nil {
193 if a.guardRefused(w, r, err) {
194 return
195 }
196 if errors.Is(err, errLastOwner) {
197 a.shareRedirect(w, r, s, "last-owner")
198 return
199 }
200 a.fail(w, r, err)
201 return
202 }
203 if a.stillReaches(r.Context(), s.ID, memberID) {
204 a.shareRedirectAbout(w, r, s, noticeRevokedTeam, memberID)
205 return
206 }
207 a.shareRedirect(w, r, s, "revoked")
208}
209
210// wouldOrphan reports whether removing this grant would leave the sheet
211// with no owner at all.
212func (a *App) wouldOrphan(ctx context.Context, sheetID, memberID uint64) bool {
213 people, err := a.grantsOn(ctx, sheetID)
214 if err != nil {
215 return true // cannot tell means do not risk it
216 }
217 var owners, losing int
218 for _, p := range people {
219 if p.Role != SheetOwner {
220 continue
221 }
222 owners++
223 if p.Member.ID == memberID {
224 losing++
225 }
226 }
227 return owners-losing < 1
228}
229
230// sheetForSharing loads the sheet and proves the viewer may change who
231// can reach it.
232//
233// Sheet owners only. Changing who else can read a sheet is the one
234// power the people it affects cannot undo, so it does not come with
235// team standing — see roleFor, which hands colleagues editor and not
236// owner for exactly this reason.
237func (a *App) sheetForSharing(w http.ResponseWriter, r *http.Request) (SheetSummary, Member, bool) {
238 s, ok := a.sheetFromURL(w, r)
239 if !ok {
240 return SheetSummary{}, Member{}, false
241 }
242 viewer, ok := a.actor(r)
243 if !ok {
244 a.notFound(w, r)
245 return SheetSummary{}, Member{}, false
246 }
247 if !a.mayShare(s.ID, viewer) {
248 // 404, not 403: somebody who can read a sheet but not share it
249 // learns nothing new, and somebody who can do neither learns
250 // nothing at all.
251 a.notFound(w, r)
252 return SheetSummary{}, Member{}, false
253 }
254 return s, viewer, true
255}
256
257// shareRedirect answers a POST with a 303 back to the screen.
258//
259// The notice is a fixed code, and the sheet id is re-formatted from a
260// uint64 rather than passed through — both end up in a Location header,
261// and a number that came back out as a number cannot carry anything
262// else.
263// shareRedirectAbout is shareRedirect for a notice that has to name
264// somebody. It carries the member id and never the address: an address
265// in a request line is an address in the platform's access logs.
266func (a *App) shareRedirectAbout(w http.ResponseWriter, r *http.Request, s SheetSummary,
267 notice string, memberID uint64) {
268 if nativeNotice(w, r, notice) {
269 return
270 }
271 http.Redirect(w, r, "/sheets/"+strconv.FormatUint(s.ID, 10)+"/share?notice="+notice+
272 "&who="+strconv.FormatUint(memberID, 10), http.StatusSeeOther)
273}
274
275func (a *App) shareRedirect(w http.ResponseWriter, r *http.Request, s SheetSummary, notice string) {
276 if nativeNotice(w, r, notice) {
277 return
278 }
279 http.Redirect(w, r, "/sheets/"+strconv.FormatUint(s.ID, 10)+"/share?notice="+notice,
280 http.StatusSeeOther)
281}
282