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 "errors"
5 "net/http"
6 "strings"
7
8 "amadan.net/oficina/home/connect"
9 "gorm.io/gorm"
10 "gorm.io/gorm/clause"
11)
12
13// errNoDB is what every service method answers when it is called on an
14// App built without storage. Tests that render pages do that
15// deliberately; production never can, because main refuses to boot
16// without a database.
17var errNoDB = errors.New("sheets: no database")
18
19// memberFor returns the Member row for an address, creating it the
20// first time that address is seen.
21//
22// First sight is the right moment because admission has already
23// happened: auth.Config.Authorize refused every address not on the
24// allowlist before a session could exist, so anything reaching here
25// belongs here. Minting rows at boot from SHEETS_OWNERS instead would
26// create members for people who never signed in, and would go stale the
27// moment the allowlist changed.
28//
29// The write is an upsert-then-read rather than a read-then-create: two
30// tabs signing in at the same moment would otherwise race, and the loser
31// would get a unique-constraint error instead of a member.
32func (a *App) memberFor(address string) (Member, error) {
33 addr := strings.ToLower(strings.TrimSpace(address))
34 if addr == "" {
35 // An empty address would mint one shared member that every
36 // unauthenticated path then wrote edits as — the uid-0 trap
37 // wearing a different hat.
38 return Member{}, errors.New("sheets: refusing to mint a member for an empty address")
39 }
40 if a.db == nil {
41 return Member{}, errNoDB
42 }
43 // SHEETS_OWNERS is the team; anybody else who reaches here arrived
44 // through a share and is a guest. Guest is the safe default: the
45 // wrong direction is unrecoverable in the way that matters, since a
46 // guest wrongly made a member sees every sheet on the instance.
47 //
48 // In team mode the roster is the team and this column is never read
49 // (team_access.go), so SHEETS_OWNERS — if it is set at all — only
50 // admits a local sign-in and promotes nobody.
51 role := TeamGuest
52 if a.team == nil && a.admit != nil && a.admit(addr) {
53 role = TeamOwner
54 }
55
56 m := Member{Address: addr, TeamRole: string(role)}
57 if err := a.db.Clauses(clause.OnConflict{DoNothing: true}).Create(&m).Error; err != nil {
58 return Member{}, err
59 }
60 if m.ID != 0 {
61 return m, nil
62 }
63 // The insert conflicted, so the row is already there: read it.
64 var existing Member
65 if err := a.db.Where("address = ?", addr).First(&existing).Error; err != nil {
66 if errors.Is(err, gorm.ErrRecordNotFound) {
67 return Member{}, errors.New("sheets: member neither created nor found")
68 }
69 return Member{}, err
70 }
71 // Promote a row that predates the allowlist, or predates team roles
72 // existing at all. Never demote: a role somebody was given by hand
73 // is not this function's to take away.
74 if role == TeamOwner && TeamRole(existing.TeamRole) != TeamOwner {
75 if err := a.db.Model(&existing).Update("team_role", string(TeamOwner)).Error; err != nil {
76 return Member{}, err
77 }
78 existing.TeamRole = string(TeamOwner)
79 }
80 return existing, nil
81}
82
83// actor resolves the signed-in viewer to a Member.
84//
85// No session is not an error — it is "no actor" — so callers gate on ok
86// rather than on err. Every route that calls this has already been
87// through requireSession, so a false here in production means the
88// session went away between the middleware and the handler.
89func (a *App) actor(r *http.Request) (Member, bool) {
90 if a.team != nil {
91 // Team mode: the standing teamRequire resolved for this request,
92 // or a fresh one off the group. Never memberFor's stored role.
93 t, ok := teamAccessFrom(r)
94 if !ok {
95 var err error
96 if t, err = a.resolveTeamAccess(r); err != nil {
97 return Member{}, false
98 }
99 }
100 return t.actor(), true
101 }
102 if a.home != nil {
103 s, ok := a.homeSessions.From(r)
104 if ok && connect.IsHomeDerived(s.Method) {
105 p, err := a.home.ResolveSession(r)
106 if err != nil {
107 return Member{}, false
108 }
109 return a.memberByID(uint64(p.ID))
110 }
111 }
112 address, ok := a.identity(r)
113 if !ok {
114 return Member{}, false
115 }
116 m, err := a.memberFor(address)
117 if err != nil {
118 return Member{}, false
119 }
120 return m, true
121}
122
123// memberByID reads one member row.
124//
125// It exists for the share screen's revoke path, which has an id from a
126// form and needs the person's TEAM standing — the field that decides
127// whether removing their grant actually removed anything. Reading the
128// row is the only way to know: team standing is not in the grants
129// table, which is exactly why revoke could not see it.
130func (a *App) memberByID(id uint64) (Member, bool) {
131 if a.db == nil || id == 0 {
132 return Member{}, false
133 }
134 var m Member
135 if err := a.db.Where("id = ?", id).First(&m).Error; err != nil {
136 return Member{}, false
137 }
138 return m, true
139}
140