feat(coderd/rbac): make organization-member a per-org system custom role (#21359)

Migrated the built-in organization-member role to DB storage so it can be customized per org.

Closes https://github.com/coder/internal/issues/1073 (part 1)
This commit is contained in:
George K
2026-01-12 18:19:19 -08:00
committed by GitHub
parent 2b448c7178
commit cc2efe9e1f
46 changed files with 1845 additions and 438 deletions
@@ -0,0 +1,3 @@
ALTER TABLE custom_roles DROP COLUMN IF EXISTS member_permissions;
ALTER TABLE custom_roles DROP COLUMN IF EXISTS is_system;
@@ -0,0 +1,10 @@
-- Add is_system column to identify system-managed roles.
ALTER TABLE custom_roles
ADD COLUMN is_system boolean NOT NULL DEFAULT false;
-- Add member_permissions column for member-scoped permissions within an organization.
ALTER TABLE custom_roles
ADD COLUMN member_permissions jsonb NOT NULL DEFAULT '[]'::jsonb;
COMMENT ON COLUMN custom_roles.is_system IS
'System roles are managed by Coder and cannot be modified or deleted by users.';
@@ -0,0 +1 @@
ALTER TABLE organizations DROP COLUMN IF EXISTS workspace_sharing_disabled;
@@ -0,0 +1,2 @@
ALTER TABLE organizations
ADD COLUMN workspace_sharing_disabled boolean NOT NULL DEFAULT false;
@@ -0,0 +1,6 @@
-- Drop the trigger and function created by the up migration.
DROP TRIGGER IF EXISTS trigger_insert_org_member_system_role ON organizations;
DROP FUNCTION IF EXISTS insert_org_member_system_role;
-- Remove organization-member system roles created by the up migration.
DELETE FROM custom_roles WHERE name = 'organization-member' AND is_system = true;
@@ -0,0 +1,85 @@
-- Create placeholder organization-member system roles for existing
-- organizations. Also add a trigger that creates the placeholder role
-- when an organization is created. Permissions will be empty until
-- populated by the reconciliation routine.
--
-- Note: why do all this in the database (as opposed to coderd)? Less
-- room for race conditions. If the role doesn't exist when coderd
-- expects it, the only correct option is to panic. On the other hand,
-- a placeholder role with empty permissions is harmless and the
-- reconciliation process is idempotent.
-- 'organization-member' is reserved and blocked from being created in
-- coderd, but let's do a delete just in case.
DELETE FROM custom_roles WHERE name = 'organization-member';
-- Create roles for the existing organizations.
INSERT INTO custom_roles (
name,
display_name,
organization_id,
site_permissions,
org_permissions,
user_permissions,
member_permissions,
is_system,
created_at,
updated_at
)
SELECT
'organization-member', -- reserved role name, so it doesn't exist in DB yet
'',
id,
'[]'::jsonb,
'[]'::jsonb,
'[]'::jsonb,
'[]'::jsonb,
true,
NOW(),
NOW()
FROM
organizations
WHERE
NOT EXISTS (
SELECT 1
FROM custom_roles
WHERE
custom_roles.name = 'organization-member'
AND custom_roles.organization_id = organizations.id
);
-- When we insert a new organization, we also want to create a
-- placeholder org-member system role for it.
CREATE OR REPLACE FUNCTION insert_org_member_system_role() RETURNS trigger AS $$
BEGIN
INSERT INTO custom_roles (
name,
display_name,
organization_id,
site_permissions,
org_permissions,
user_permissions,
member_permissions,
is_system,
created_at,
updated_at
) VALUES (
'organization-member',
'',
NEW.id,
'[]'::jsonb,
'[]'::jsonb,
'[]'::jsonb,
'[]'::jsonb,
true,
NOW(),
NOW()
);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER trigger_insert_org_member_system_role
AFTER INSERT ON organizations
FOR EACH ROW
EXECUTE FUNCTION insert_org_member_system_role();