Files
uptimesebi/internal/migrations/sql/00009_roles.sql
T
Sebastian UnterschützandClaude Opus 5.5 ee09c497de 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
2026-11-22 00:49:35 +01:00

67 lines
3.2 KiB
SQL

-- +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;