| 1 | package slides |
| 2 | |
| 3 | // Home sign-in for a roster member with no account here: connect's two |
| 4 | // team-mode hooks over Slides' users table. connect completes an |
| 5 | // ordinary Home sign-in only for a subject with an account link, and |
| 6 | // refuses the rest as "unknown" — right for an allowlist instance, where |
| 7 | // nobody may enter on a Home identity alone. In team mode a roster member |
| 8 | // who has never had a local account must be able to, so initHome gives |
| 9 | // connect these, and connect's own completion does the rest (its cookie |
| 10 | // checks, the same-person rule, the link flow). |
| 11 | // |
| 12 | // The user Unlinked creates is identity, never standing: every request |
| 13 | // after this one asks the roster again (team_access.go). |
| 14 | |
| 15 | import ( |
| 16 | "context" |
| 17 | "database/sql" |
| 18 | "errors" |
| 19 | |
| 20 | "amadan.net/oficina/home/connect" |
| 21 | "amadan.net/oficina/home/teammode" |
| 22 | |
| 23 | "amadan.net/oficina/slides/internal/slides/teamstate" |
| 24 | ) |
| 25 | |
| 26 | // teamAdmit is connect's Admit, the roster decision taken first (Home spec |
| 27 | // 2026-11-14-oficina-id-native.md §8) for a subject with neither a link |
| 28 | // nor a Home-only user: Runtime.AdmitUnlinked, which allocates nothing and |
| 29 | // never asks whether Slides has local accounts. A Member is asked to |
| 30 | // choose (connect's choose page: connect an existing email account, or |
| 31 | // "I'm new here", which then reaches teamUnlinked); a stranger is |
| 32 | // connect's "unknown"; an undecidable roster is a 503. |
| 33 | func (a *app) teamAdmit(ctx context.Context, issuer, subject string) error { |
| 34 | if a.team == nil { |
| 35 | return errTeamUnavailable |
| 36 | } |
| 37 | switch err := a.team.AdmitUnlinked(ctx, issuer, subject); { |
| 38 | case err == nil: |
| 39 | return nil |
| 40 | case errors.Is(err, teammode.ErrNotMember): |
| 41 | return connect.ErrNoPerson |
| 42 | } |
| 43 | return errTeamUnavailable |
| 44 | } |
| 45 | |
| 46 | // teamHomeOnly is connect's HomeOnly: the addressless user Unlinked gave |
| 47 | // a subject, read and never created. |
| 48 | func (a *app) teamHomeOnly(d *sql.DB) func(context.Context, string, string) (connect.Person, error) { |
| 49 | return func(ctx context.Context, issuer, subject string) (connect.Person, error) { |
| 50 | id, err := teamstate.HomeUser(ctx, d, issuer, subject) |
| 51 | switch { |
| 52 | case err == nil: |
| 53 | return connect.Person{ID: id}, nil |
| 54 | case errors.Is(err, teamstate.ErrRefused): |
| 55 | return connect.Person{}, connect.ErrNoPerson |
| 56 | } |
| 57 | return connect.Person{}, errTeamUnavailable |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // teamUnlinked is connect's Unlinked, asked only after a Member chose "I'm |
| 62 | // new here" with nobody signed in, for a subject with neither a link nor a |
| 63 | // Home-only user (connect rechecks teamAdmit first): an addressless |
| 64 | // user for a subject the roster says is a Member now, with the lease on |
| 65 | // that answer still valid (Runtime.AdmitUnlinked). A stranger is |
| 66 | // connect's "unknown"; an undecidable roster is a 503. |
| 67 | // |
| 68 | // A user made here gets the sample deck, as a first local sign-in does |
| 69 | // (currentUser): it is their first sign-in, and an empty list would be a |
| 70 | // worse welcome than the one everybody else gets. |
| 71 | func (a *app) teamUnlinked(d *sql.DB) func(context.Context, string, string) (connect.Person, error) { |
| 72 | return func(ctx context.Context, issuer, subject string) (connect.Person, error) { |
| 73 | if a.team == nil { |
| 74 | return connect.Person{}, errTeamUnavailable |
| 75 | } |
| 76 | if err := a.team.AdmitUnlinked(ctx, issuer, subject); err != nil { |
| 77 | if errors.Is(err, teammode.ErrNotMember) { |
| 78 | return connect.Person{}, connect.ErrNoPerson |
| 79 | } |
| 80 | return connect.Person{}, errTeamUnavailable |
| 81 | } |
| 82 | id, created, err := teamstate.EnsureHomeUser(ctx, d, issuer, subject, colourFor(subject)) |
| 83 | switch { |
| 84 | case err == nil: |
| 85 | case errors.Is(err, teamstate.ErrRefused): |
| 86 | // Linked to a local account in the meantime: that person signs |
| 87 | // in as that account, not as a new one. |
| 88 | return connect.Person{}, connect.ErrNoPerson |
| 89 | default: |
| 90 | return connect.Person{}, errTeamUnavailable |
| 91 | } |
| 92 | if created { |
| 93 | var u User |
| 94 | if err := a.db.G.WithContext(ctx).Take(&u, id).Error; err != nil { |
| 95 | a.logger.Error("slides: read a new Home-only user to seed", "user", id, "err", err) |
| 96 | } else { |
| 97 | u.team = &teamAccess{Access: a.team.Access(ctx, subject)} |
| 98 | a.seedSampleDeckFor(ctx, u) |
| 99 | } |
| 100 | } |
| 101 | return connect.Person{ID: id}, nil |
| 102 | } |
| 103 | } |
| 104 | |