feat(store): add roles, role assignments, api tokens and invitations

Built-in roles are seeded with fixed ids and are read-only for every org.
Existing non-owner members receive the Viewer role.

Refs #6, #7, #8, #9

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-22 00:49:35 +01:00
1 parent 15932db98a
commit ee09c497de
15 files changed
+1063 -13

No files matched your search

+66
View File
@@ -0,0 +1,66 @@
-- +goose Up
-- Built-in roles have org_id NULL and fixed ids; they are visible to every
-- org but writable by none. Custom roles belong to one org.
CREATE TABLE roles (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
org_id uuid REFERENCES orgs (id) ON DELETE CASCADE,
key text UNIQUE,
name text NOT NULL CHECK (length(name) BETWEEN 1 AND 100),
permissions text[] NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
CHECK ((org_id IS NULL) = (key IS NOT NULL)),
UNIQUE NULLS NOT DISTINCT (org_id, name)
);
INSERT INTO roles (id, key, name, permissions) VALUES
('00000000-0000-0000-0000-00000000a001', 'admin', 'Admin',
'{org.manage,members.manage,roles.manage,monitors.write,statuspages.write,incidents.write,notifications.write}'),
('00000000-0000-0000-0000-00000000a002', 'editor', 'Editor',
'{members.read,monitors.write,statuspages.write,incidents.write,notifications.write}'),
('00000000-0000-0000-0000-00000000a003', 'viewer', 'Viewer',
'{members.read,monitors.read,statuspages.read,incidents.read,notifications.read}');
-- A deleted role must not silently strip people of access, so assignments
-- block deletion. Removing a membership removes its assignments.
CREATE TABLE role_assignments (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
org_id uuid NOT NULL,
user_id uuid NOT NULL,
role_id uuid NOT NULL REFERENCES roles (id) ON DELETE RESTRICT,
resource_type text CHECK (resource_type IN ('monitor_group', 'status_page')),
resource_id uuid,
created_at timestamptz NOT NULL DEFAULT now(),
FOREIGN KEY (org_id, user_id) REFERENCES memberships (org_id, user_id) ON DELETE CASCADE,
CHECK ((resource_type IS NULL) = (resource_id IS NULL)),
UNIQUE NULLS NOT DISTINCT (org_id, user_id, role_id, resource_type, resource_id)
);
CREATE INDEX role_assignments_member_idx ON role_assignments (org_id, user_id);
CREATE INDEX role_assignments_role_idx ON role_assignments (role_id);
INSERT INTO role_assignments (org_id, user_id, role_id)
SELECT org_id, user_id, '00000000-0000-0000-0000-00000000a003' FROM memberships WHERE NOT is_owner;
ALTER TABLE orgs ADD COLUMN require_sso boolean NOT NULL DEFAULT false;
ALTER TABLE sso_providers ADD COLUMN default_role_id uuid REFERENCES roles (id) ON DELETE SET NULL;
ALTER TABLE roles ENABLE ROW LEVEL SECURITY;
ALTER TABLE roles FORCE ROW LEVEL SECURITY;
CREATE POLICY roles_read ON roles FOR SELECT
USING (org_id IS NULL OR app_scope_system() OR org_id = app_org_id());
CREATE POLICY roles_write ON roles FOR ALL
USING (org_id IS NOT NULL AND (app_scope_system() OR org_id = app_org_id()))
WITH CHECK (org_id IS NOT NULL AND (app_scope_system() OR org_id = app_org_id()));
ALTER TABLE role_assignments ENABLE ROW LEVEL SECURITY;
ALTER TABLE role_assignments FORCE ROW LEVEL SECURITY;
CREATE POLICY role_assignments_tenant ON role_assignments FOR ALL
USING (app_scope_system() OR org_id = app_org_id())
WITH CHECK (app_scope_system() OR org_id = app_org_id());
-- +goose Down
ALTER TABLE sso_providers DROP COLUMN default_role_id;
ALTER TABLE orgs DROP COLUMN require_sso;
DROP TABLE role_assignments;
DROP TABLE roles;
@@ -0,0 +1,46 @@
-- +goose Up
CREATE TABLE api_tokens (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
org_id uuid NOT NULL,
user_id uuid NOT NULL,
name text NOT NULL CHECK (length(name) BETWEEN 1 AND 100),
token_hash bytea NOT NULL UNIQUE,
-- First characters of the token, shown so users can tell tokens apart.
prefix text NOT NULL,
permissions text[] NOT NULL,
expires_at timestamptz,
last_used_at timestamptz,
created_at timestamptz NOT NULL DEFAULT now(),
-- Leaving the org revokes the member's tokens.
FOREIGN KEY (org_id, user_id) REFERENCES memberships (org_id, user_id) ON DELETE CASCADE
);
CREATE INDEX api_tokens_member_idx ON api_tokens (org_id, user_id);
ALTER TABLE api_tokens ENABLE ROW LEVEL SECURITY;
ALTER TABLE api_tokens FORCE ROW LEVEL SECURITY;
CREATE POLICY api_tokens_tenant ON api_tokens FOR ALL
USING (app_scope_system() OR org_id = app_org_id())
WITH CHECK (app_scope_system() OR org_id = app_org_id());
-- Requests carry only the token, so its org is unknown until it is found.
-- Like org_id_by_slug, the system scope is raised for this single lookup by
-- hash, which cannot enumerate other tokens.
-- +goose StatementBegin
CREATE FUNCTION api_token_org(hash bytea) RETURNS uuid
LANGUAGE plpgsql VOLATILE AS $$
DECLARE
prev text := coalesce(current_setting('app.scope', true), '');
result uuid;
BEGIN
PERFORM set_config('app.scope', 'system', true);
SELECT org_id INTO result FROM api_tokens WHERE token_hash = hash;
PERFORM set_config('app.scope', prev, true);
RETURN result;
END
$$;
-- +goose StatementEnd
-- +goose Down
DROP FUNCTION api_token_org(bytea);
DROP TABLE api_tokens;
@@ -0,0 +1,40 @@
-- +goose Up
CREATE TABLE invitations (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
org_id uuid NOT NULL REFERENCES orgs (id) ON DELETE CASCADE,
email citext NOT NULL,
role_id uuid NOT NULL REFERENCES roles (id) ON DELETE CASCADE,
token_hash bytea NOT NULL UNIQUE,
invited_by uuid REFERENCES users (id) ON DELETE SET NULL,
expires_at timestamptz NOT NULL,
accepted_at timestamptz,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX invitations_org_idx ON invitations (org_id);
ALTER TABLE invitations ENABLE ROW LEVEL SECURITY;
ALTER TABLE invitations FORCE ROW LEVEL SECURITY;
CREATE POLICY invitations_tenant ON invitations FOR ALL
USING (app_scope_system() OR org_id = app_org_id())
WITH CHECK (app_scope_system() OR org_id = app_org_id());
-- Invitees are not members yet; see api_token_org for the pattern.
-- +goose StatementBegin
CREATE FUNCTION invitation_org(hash bytea) RETURNS uuid
LANGUAGE plpgsql VOLATILE AS $$
DECLARE
prev text := coalesce(current_setting('app.scope', true), '');
result uuid;
BEGIN
PERFORM set_config('app.scope', 'system', true);
SELECT org_id INTO result FROM invitations WHERE token_hash = hash;
PERFORM set_config('app.scope', prev, true);
RETURN result;
END
$$;
-- +goose StatementEnd
-- +goose Down
DROP FUNCTION invitation_org(bytea);
DROP TABLE invitations;