# Workspaces and access

> How workspaces, roles, permissions and row-level security are designed and built.

Source: https://werker.cloud/docs/engineering/workspaces-and-access (updated 2026-12-02)

Adding people and changing roles in the app is in
[users and access](../users-and-access.md).

## Workspaces

A workspace owns everything in it: connections, agents, workflows,
runs, the audit log, usage and credits.

- A user belongs to one workspace, through a membership.
- The instance admin (whoever installed Werker) manages the instance
  and its workspaces. Setup creates the first workspace.
- Sandboxes are never shared between workspaces.

## Roles and permissions (RBAC)

- Roles, resources and actions are fixed lists in code. A permission
  is `action:resource`, for example `update:connection`.
- Each role's permissions are written out per role.
- A role belongs to a membership, not to a user: someone removed from
  one workspace and added to another starts with the role given there.
- Instance admin is a separate role above all workspaces: Better
  Auth's admin plugin, user role `admin` (everyone else has `user`).
  The first user added becomes one (`add_user_to_workspace`).
- `can_grant_role()` limits who can grant or remove a role: only
  someone at or above it.
- A workspace can adjust a role's permissions without a deploy; the
  top workspace role cannot be overridden.

Starting lists:

| List      | Values                                                             |
| --------- | ------------------------------------------------------------------ |
| Roles     | owner, admin, member, viewer                                       |
| Resources | member, agent, workflow, run, connection, settings, audit, billing |
| Actions   | view, create, update, delete, run, configure                       |

## Row-level security

The app checks permissions; Postgres row-level security (RLS) is the
second line.

- Workspace-owned tables carry `workspace_id` and a policy on
  `app.workspace_id`.
- Each transaction sets `app.workspace_id` (`with_workspace` in
  `packages/db/src/client/client.ts`).
- The app connects as `werker_app`, which does not own the tables.
- Workers claim runs across all workspaces with the database function
  `claim_next_run` ([queue](database-and-events.md)).

## What's built

Sign-in and workspaces use [Better Auth](https://www.better-auth.com)
(`packages/auth`) with email and password and its organization plugin.

- **Workspaces are organizations.** Better Auth's tables (`user`,
  `session`, `account`, `organization`, `member`, `invitation`) live
  in the `auth` schema, used by the `werker_auth` role. Werker's
  workspace-owned tables reference `auth.organization (id)`, and RLS
  on them works as before. `werker_app` cannot read the `auth` schema.
- **Werker's own schemas:** `durable` holds each run's Pi Durable
  state, `observability` the run traces, spend, totals and scores
  (`run_spans`, `run_usage`, `run_totals`, `run_agent_totals`,
  `run_scores`, `score_definitions`), and `analytics` the public
  site's page views (`page_views`, `page_view_salt`). Everything else
  is in `public`. SQL in `packages/db/src` names those tables with
  their schema; the workspace-owned ones have the same row-level
  security as the rest.
- **Sign-in:** email and password through Better Auth, from a
  SvelteKit form action; Better Auth's endpoints are mounted at
  `/api/auth`. Public sign-up is off: an operator adds users with
  `pnpm add-user --email <email> --workspace <slug> --role <role>`
  (password from `WERKER_USER_PASSWORD`). A new workspace is created
  with that user as its owner.
- **Dev sign-in:** under `vite dev` only, a request with no session is
  signed in as `auto-dev@werker.local`
  (`packages/core/src/access/dev-user.ts`), which is created if
  missing.
- **One workspace per user:** a unique index on
  `auth.member ("userId")` (`001_schema.sql`). `current_user` in
  `packages/auth/src/session.ts` gives the one membership, or null.
  `add_workspace_member` and `add_user_to_workspace` refuse someone in
  another workspace. Being an instance admin adds no workspace. Better
  Auth's `session."activeOrganizationId"` is not read, and its
  `set-active` and `accept-invitation` endpoints are off.
- **Name:** `rename_workspace` in
  `packages/core/src/access/access.ts`, needing `configure:settings`,
  changes the organization's name, not the slug operator scripts use.
  Better Auth's `organization/update` is off. A new workspace's name
  is its slug. Two workspaces may have the same name.
- **Instance settings:** Admin → Sandboxes (instance admins) sets
  where every workspace's sandboxes run, including the hosted
  providers' keys. Model and service keys belong to one workspace
  each.
- **Roles:** owner, admin, member and viewer, defined with Better
  Auth's access control in `packages/auth/src/access.ts`. Each role
  lists Better Auth's own organization permissions (members,
  invitations) plus Werker's resources, written out per role.
- **Role permissions per workspace:** Admin → Roles switches a
  permission on or off for admin, member or viewer. Only differences
  from the built-in lists are stored (`role_permission_overrides`,
  `packages/db/src/workspace/role-permissions.ts`); `current_user` in
  `packages/core/src/access/access.ts` loads them with the membership,
  so `has_permission` applies them on every check. Changing one needs
  `configure:settings`. The owner role cannot be changed, nobody
  changes their own role's permissions, and nobody switches a
  permission they do not hold (`can_edit_permission`).
- **API keys:** a key belongs to a membership (`api_keys`) and is
  deleted with it. Only its SHA-256 hash is kept
  (`packages/core/src/access/api-keys.ts`). `find_api_key` finds none
  that has expired, whose owner is banned, or whose member is in
  another workspace, and the owner's role is read again on each
  request. What accepts a key is in
  [API, CLI and MCP](api-and-cli.md).
- **Invitations:** `packages/core/src/access/invitations.ts` queues
  each email with its invitation. Acceptance rechecks the inviter’s
  permissions and consumes the link in the transaction that adds the
  member. The invitation’s `email_id` identifies the queued message,
  not an inbox delivery receipt.
- **Members:** `add_member`, `change_member_role` and `remove_member`
  in `packages/core/src/access/access.ts`, needing `create:member`,
  `update:member` and `delete:member`. `can_grant_role`
  (`packages/auth/src/access.ts`) limits both the role given and the
  member changed to roles at or below the actor's own. Nobody changes
  or removes themselves, so a workspace keeps an owner. Better Auth's
  own HTTP endpoints for inviting, removing, changing and leaving are
  switched off (`disabledPaths` in `packages/auth/src/auth.ts`).
- **Web:** run pages and `/runs/<id>/events` require `view:run`; a
  denied request is a 404.
- **Settings:** `WERKER_AUTH_SECRET` signs cookies and is required in
  production; `WERKER_AUTH_DATABASE_URL` and `ORIGIN` (Better Auth's
  base URL) are optional locally.
