# Team accounts and permissions ## What this builds A multi-tenant foundation for a web app. People sign in, create a workspace, invite others, and give each member a role. Every request is checked against that role, and no request can read or change another workspace's data. Read `spekkr.inputs.json` first. Where this spec says *workspace*, use the person's `workspace_noun` in everything a person sees. Keep `workspace` in code and in the database. ## Out of scope - Billing and plans. - Single sign-on with an identity provider. - Any product feature beyond the sample resource described in R7. ## Roles | Role | Can do | | --- | --- | | Owner | Everything an admin can, plus delete the workspace and transfer ownership. | | Admin | Invite and remove members, change roles below owner, change workspace settings. | | Member | Create, read, update and delete the workspace's resources. | | Viewer | Read the workspace's resources. | A workspace always has exactly one owner. ## Requirements Each requirement has an id. Tests name the id they cover. ### R1. Sign in - A person signs in with the method in the `sign_in` input. - `email_link`: they enter an email address and receive a link that signs them in once and expires after 15 minutes. - `password`: they register with an email address and a password of at least 12 characters. Passwords are stored with a slow, salted hash. - A session lasts 30 days and ends at sign-out. - Sign-in attempts are limited to 5 per email address per 15 minutes. Accept when: a valid sign-in creates a session, a used or expired link is refused, and the sixth attempt inside 15 minutes is refused with a clear message. ### R2. Workspaces - A signed-in person can create a workspace and becomes its owner. - A person can belong to many workspaces and switch between them. - A workspace has a name and a URL-safe slug that is unique across the app. Accept when: two people can create workspaces with the same name but not the same slug, and each sees only the workspaces they belong to. ### R3. Invitations - An admin or owner invites a person by email address and picks a role below owner. - The invitation email holds a link that expires after 7 days and works once. - Accepting an invitation while signed in with a different email address is refused. - An admin can revoke a pending invitation. - If `invite_links` is on, an admin can also create a link with a role, an expiry and a maximum number of uses. Accept when: an invited person joins with the chosen role, and a revoked, expired or used invitation is refused. ### R4. Members and roles - Admins and owners see the member list with each member's role. - An admin can change a member's role to admin, member or viewer, and remove members. - An admin cannot change or remove the owner. - The owner can transfer ownership to another member. The previous owner becomes an admin. - A member can leave a workspace. The owner must transfer ownership first. Accept when: every action above succeeds for the roles allowed and is refused with status 403 for every other role. ### R5. Tenant isolation - Every table that holds workspace data has a `workspace_id` column. - Every query for workspace data filters by the workspace of the current request. - A request that names a resource in another workspace gets status 404, never 403, so that ids cannot be probed. Accept when: for every resource route, a member of workspace A gets 404 for a resource id that belongs to workspace B. ### R6. Audit log Only if `audit_log` is on. - Record member invited, invitation revoked, member joined, role changed, member removed, ownership transferred and workspace settings changed. - Each entry holds the time, the actor, the action, the target and the workspace. - Admins and owners can read the log, newest first, 50 entries per page. - Entries cannot be edited or deleted through the app. Accept when: each action above produces exactly one entry and a member or viewer gets 403 on the log. ### R7. Sample resource Build one simple resource, `notes` with a title and a body, so the permission rules have something to protect. The person's agent replaces it with real features later. Accept when: a viewer can read notes but gets 403 on create, update and delete. ### R8. Interface - Pages: sign in, workspace switcher, notes, members, invitations, settings, and the audit log if it is on. - A control a person is not allowed to use is not shown. The server still checks. - Every form shows its errors next to the field that caused them. Accept when: each page works with a keyboard alone and at a 360 pixel wide screen. ## Definition of done - Every acceptance check above has an automated test, and all tests pass. - The permission matrix in `design.md` is tested cell by cell. - A README explains how to configure the secrets, run the app and run the tests.