| 1 | package calendar |
| 2 | |
| 3 | // Team mode's per-request standing. With a.team nil nothing here runs: |
| 4 | // requirePerson, the route gates and every store read are what they have |
| 5 | // always been. |
| 6 | // |
| 7 | // Standing itself is home/teammode's Access. Identity is resolved from the |
| 8 | // actual session (a local address the allowlist admits, an account link, |
| 9 | // or a Home-only person); the roster is asked about the Home subject, if |
| 10 | // there is one; and the answer is one of three: |
| 11 | // |
| 12 | // - Member: the team rung, which in Calendar is every grant a person |
| 13 | // holds. Calendar has no shares: every grant is made by |
| 14 | // EnsurePersonSetup for the person themselves (editor on the team |
| 15 | // calendar, owner of their own), so every grant is the team rung and |
| 16 | // counts only while the roster says Member. The roster's role says |
| 17 | // owner or member, and only the roster's. |
| 18 | // - NotMember: nothing. With no shares a guest reaches nothing, so they |
| 19 | // are signed out and told so (offboard), as Memoria does. The one |
| 20 | // exception is a local account with no Oficina link, which has no |
| 21 | // standing until it links and is sent to /account/home to do that. |
| 22 | // - Unknown: the roster could not be read within its lease. Lists are |
| 23 | // partial with a notice (Paul, 2026-10-28) — and, with no shares, |
| 24 | // empty — and everything else is a retryable 503 rather than a 404 |
| 25 | // that would claim the event is gone. |
| 26 | // |
| 27 | // A Member decision carries a lease. Access.State checks it every time it |
| 28 | // is asked; the store's reads ask it once before they query (team_reach.go) |
| 29 | // and a write checks it again inside its own transaction, before and |
| 30 | // after (team_guard.go); a list asks it again after it is built. |
| 31 | |
| 32 | import ( |
| 33 | "context" |
| 34 | "errors" |
| 35 | "net/http" |
| 36 | |
| 37 | "amadan.net/oficina/home/connect" |
| 38 | "amadan.net/oficina/home/connect/linkstore" |
| 39 | "amadan.net/oficina/home/roster" |
| 40 | "amadan.net/oficina/home/teammode" |
| 41 | rauth "amadan.net/rastrillo/rastrillo/auth" |
| 42 | "amadan.net/rastrillo/rastrillo/sessions" |
| 43 | "gorm.io/gorm" |
| 44 | |
| 45 | "amadan.net/oficina/calendar/internal/calendar/teamstate" |
| 46 | ) |
| 47 | |
| 48 | // teamPerson is who one request's session speaks for in team mode, and |
| 49 | // their standing. Never stored beyond the request. |
| 50 | type teamPerson struct { |
| 51 | person Person |
| 52 | // subject is the Home subject the roster knows them by, "" for a |
| 53 | // local account with no Oficina link (no standing). |
| 54 | subject string |
| 55 | // method is the session's sign-in method. |
| 56 | method string |
| 57 | access *teammode.Access |
| 58 | } |
| 59 | |
| 60 | // unlinkedLocal reports whether p is a local account with no Oficina |
| 61 | // link: identity, but nothing to ask the roster about. |
| 62 | func (p *teamPerson) unlinkedLocal() bool { |
| 63 | return p.subject == "" && (p.method == "magiclink" || p.method == "keymail") |
| 64 | } |
| 65 | |
| 66 | var errNoIdentity = errors.New("calendar: no team identity") |
| 67 | |
| 68 | type teamPersonKey struct{} |
| 69 | |
| 70 | // withTeamPerson carries p, and its Access for the store's reads and |
| 71 | // guard, on ctx. |
| 72 | func withTeamPerson(ctx context.Context, p *teamPerson) context.Context { |
| 73 | return context.WithValue(teammode.WithAccess(ctx, p.access), teamPersonKey{}, p) |
| 74 | } |
| 75 | |
| 76 | func teamPersonFrom(ctx context.Context) (*teamPerson, bool) { |
| 77 | p, ok := ctx.Value(teamPersonKey{}).(*teamPerson) |
| 78 | return p, ok && p != nil |
| 79 | } |
| 80 | |
| 81 | // teamIdentify resolves a session to the person it speaks for. A Home |
| 82 | // subject resolves through its link or its Home-only row (connect's |
| 83 | // Resolve, with the HomeOnly hook), and only reads; a local session |
| 84 | // through the allowlist, which in team mode admits the local door and |
| 85 | // nothing more, and its link, with its row and calendars made on first |
| 86 | // sight exactly as requirePerson always has (identity, never standing). |
| 87 | // errTeamUnavailable |
| 88 | // is storage that cannot be read now; errNoIdentity a session that speaks |
| 89 | // for nobody here. |
| 90 | func (a *App) teamIdentify(ctx context.Context, s sessions.Session) (Person, string, error) { |
| 91 | switch s.Method { |
| 92 | case connect.HomeMethod: |
| 93 | if a.home == nil { |
| 94 | return Person{}, "", errNoIdentity |
| 95 | } |
| 96 | ref, err := a.home.bridge.Resolve(ctx, s.Subject) |
| 97 | if errors.Is(err, linkstore.ErrStorage) { |
| 98 | return Person{}, "", errTeamUnavailable |
| 99 | } |
| 100 | if err != nil { |
| 101 | return Person{}, "", errNoIdentity |
| 102 | } |
| 103 | var p Person |
| 104 | if err := a.db.G.WithContext(ctx).First(&p, ref.ID).Error; err != nil { |
| 105 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 106 | return Person{}, "", errNoIdentity |
| 107 | } |
| 108 | return Person{}, "", errTeamUnavailable |
| 109 | } |
| 110 | switch { |
| 111 | case ref.Label == "": |
| 112 | // Home-only: connect's HomeOnly hook found them (connect holds |
| 113 | // it to an empty label, and the table's triggers to a |
| 114 | // placeholder address). |
| 115 | p.homeOnly = s.Subject |
| 116 | case p.Address != ref.Label: |
| 117 | return Person{}, "", errNoIdentity |
| 118 | } |
| 119 | return p, s.Subject, nil |
| 120 | case "magiclink", "keymail": |
| 121 | address := normalizeAddress(s.Subject) |
| 122 | if a.home == nil || address == "" || !a.home.admitted.allows(address) { |
| 123 | return Person{}, "", errNoIdentity |
| 124 | } |
| 125 | p, err := a.store.EnsurePersonSetup(ctx, address) |
| 126 | if err != nil { |
| 127 | return Person{}, "", errTeamUnavailable |
| 128 | } |
| 129 | subject, err := a.homeSubjectOf(ctx, p.ID) |
| 130 | if err != nil { |
| 131 | return Person{}, "", err |
| 132 | } |
| 133 | return p, subject, nil |
| 134 | } |
| 135 | return Person{}, "", errNoIdentity |
| 136 | } |
| 137 | |
| 138 | // homeSubjectOf is the Home subject the roster knows a person by: a |
| 139 | // Home-only row or an account link, "" for neither. |
| 140 | func (a *App) homeSubjectOf(ctx context.Context, id int64) (string, error) { |
| 141 | subject, err := teamstate.HomeSubject(ctx, a.team.db, a.team.issuer(), id) |
| 142 | switch { |
| 143 | case err == nil: |
| 144 | return subject, nil |
| 145 | case errors.Is(err, teamstate.ErrRefused): |
| 146 | return "", nil |
| 147 | } |
| 148 | return "", errTeamUnavailable |
| 149 | } |
| 150 | |
| 151 | // teamRequirePerson is requirePerson in team mode: who the session is, |
| 152 | // and the roster's answer, asked once and carried on the request. |
| 153 | // |
| 154 | // A definite NotMember is signed out and told so (offboard), existing |
| 155 | // sessions included: that is removal within the lease. An unlinked local |
| 156 | // account is let through to the routes that need only a person — and the |
| 157 | // content routes send it to /account/home (standing). A session that |
| 158 | // speaks for nobody is the 404 it has always been. Unknown ends nothing. |
| 159 | func (a *App) teamRequirePerson(w http.ResponseWriter, r *http.Request, next http.Handler) { |
| 160 | s, ok := sessions.Current(r) |
| 161 | if !ok { |
| 162 | a.h.errorPage(w, r, http.StatusNotFound, "") |
| 163 | return |
| 164 | } |
| 165 | p, subject, err := a.teamIdentify(r.Context(), s) |
| 166 | switch { |
| 167 | case errors.Is(err, errTeamUnavailable): |
| 168 | a.h.teamUnavailable(w, r) |
| 169 | return |
| 170 | case err != nil: |
| 171 | a.h.errorPage(w, r, http.StatusNotFound, "") |
| 172 | return |
| 173 | } |
| 174 | if p.homeOnly != "" { |
| 175 | // A Home-only person's calendars are made on their first page, |
| 176 | // as a local person's are (identity, never standing). |
| 177 | if err := a.store.setupPerson(r.Context(), p); err != nil { |
| 178 | a.h.teamUnavailable(w, r) |
| 179 | return |
| 180 | } |
| 181 | } |
| 182 | tp := &teamPerson{person: p, subject: subject, method: s.Method, access: a.team.Access(r.Context(), subject)} |
| 183 | switch tp.access.State() { |
| 184 | case roster.NotMember: |
| 185 | if !tp.unlinkedLocal() { |
| 186 | a.offboard(w, r) |
| 187 | return |
| 188 | } |
| 189 | case roster.Member: |
| 190 | // Back on the team: reminders that went dormant when they left |
| 191 | // are queued again (team_jobs.go). |
| 192 | if err := a.store.wakeDormantReminders(r.Context(), p.ID); err != nil { |
| 193 | a.logger.Error("calendar: wake dormant reminders", "stage", "team.wake", "err", scrubErr(err)) |
| 194 | } |
| 195 | } |
| 196 | ctx := context.WithValue(withTeamPerson(r.Context(), tp), personCtxKey{}, p) |
| 197 | next.ServeHTTP(w, r.WithContext(ctx)) |
| 198 | } |
| 199 | |
| 200 | // offboardPath is where offboard sends a session the roster says is off |
| 201 | // the team: the door, which says so (signin.go's signinRemoved). The |
| 202 | // door's admission probe reads this redirect as "removed". |
| 203 | const offboardPath = "/signin?err=removed" |
| 204 | |
| 205 | // offboard ends a session the roster says is off the team and sends it to |
| 206 | // the sign-in door, which says so. A 303, so a form POST lands on a page |
| 207 | // too. |
| 208 | // |
| 209 | // Under the sign-in door's admission probe (signin_admission.go) it only answers: the |
| 210 | // probe decides what to end, and must not revoke a session it was merely |
| 211 | // asked about, such as the one MintHomeSession has just stored, which then |
| 212 | // lands on its first page and is offboarded there, told why. |
| 213 | func (a *App) offboard(w http.ResponseWriter, r *http.Request) { |
| 214 | if !probing(r.Context()) { |
| 215 | a.home.sessions.SignOut(w, r) |
| 216 | } |
| 217 | w.Header().Set("Cache-Control", "no-store") |
| 218 | http.Redirect(w, r, offboardPath, http.StatusSeeOther) |
| 219 | } |
| 220 | |
| 221 | // teamViewer is the account menu's identity on a page requirePerson has |
| 222 | // not run for (a public page, an error raised before it). A Home session |
| 223 | // is resolved read-only, a Home-only person shown by their label; a local |
| 224 | // session is viewer's, as before. |
| 225 | func (a *App) teamViewer(r *http.Request) (rauth.Identity, bool) { |
| 226 | s, ok := sessions.Current(r) |
| 227 | if !ok || s.Method != connect.HomeMethod { |
| 228 | return viewer(r) |
| 229 | } |
| 230 | p, _, err := a.teamIdentify(r.Context(), s) |
| 231 | if err != nil { |
| 232 | return rauth.Identity{}, false |
| 233 | } |
| 234 | return rauth.Identity{Address: p.shownAddress()}, true |
| 235 | } |
| 236 | |