Files
coder/coderd/database/queries/roles.sql
T
Jeremy Ruppel 2d65ff14c3 fix(coderd): sort built-in and custom roles for stable list order (#32463)
The role selector on the new user page showed roles in a different order
on every load. `SiteBuiltInRoles()` and `OrganizationRoles()` built
their lists by ranging over the `builtInRoles` map, so `GET
/api/v2/users/roles` and `GET /api/v2/organizations/{org}/members/roles`
returned built-in roles in random order.

Both functions now sort roles by name before returning. The
`CustomRoles` query also had no `ORDER BY`, so custom roles, which are
appended after the built-in ones, now come back ordered by `name,
organization_id`.

`TestListRoles` and `TestReadCustomRoles` now assert the exact order.
Each fails without its fix and passes with it.

Closes CLIENT-1104

---

*Generated by Coder Agents on behalf of @jeremyruppel.*
2026-12-01 09:31:21 -05:00

92 lines
2.3 KiB
SQL

-- name: CustomRoles :many
SELECT
*
FROM
custom_roles
WHERE
true
-- @lookup_roles will filter for exact (role_name, org_id) pairs
-- To do this manually in SQL, you can construct an array and cast it:
-- cast(ARRAY[('customrole','ece79dac-926e-44ca-9790-2ff7c5eb6e0c')] AS name_organization_pair[])
AND CASE WHEN array_length(@lookup_roles :: name_organization_pair[], 1) > 0 THEN
-- Using 'coalesce' to avoid troubles with null literals being an empty string.
(name, coalesce(organization_id, '00000000-0000-0000-0000-000000000000' ::uuid)) = ANY (@lookup_roles::name_organization_pair[])
ELSE true
END
-- This allows fetching all roles, or just site wide roles
AND CASE WHEN @exclude_org_roles :: boolean THEN
organization_id IS null
ELSE true
END
-- Allows fetching all roles to a particular organization
AND CASE WHEN @organization_id :: uuid != '00000000-0000-0000-0000-000000000000'::uuid THEN
organization_id = @organization_id
ELSE true
END
-- Filter system roles. By default, system roles are excluded.
-- System roles are managed by Coder and should be hidden from user-facing APIs.
-- The authorization system uses @include_system_roles = true to load them.
AND CASE WHEN @include_system_roles :: boolean THEN
true
ELSE
is_system = false
END
ORDER BY
name, organization_id
;
-- name: DeleteCustomRole :exec
DELETE FROM
custom_roles
WHERE
name = lower(@name)
AND organization_id = @organization_id
-- Prevents accidental deletion of system roles even if the API
-- layer check is bypassed due to a bug.
AND is_system = false
;
-- name: InsertCustomRole :one
INSERT INTO
custom_roles (
name,
display_name,
organization_id,
site_permissions,
org_permissions,
user_permissions,
member_permissions,
is_system,
created_at,
updated_at
)
VALUES (
-- Always force lowercase names
lower(@name),
@display_name,
@organization_id,
@site_permissions,
@org_permissions,
@user_permissions,
@member_permissions,
@is_system,
now(),
now()
)
RETURNING *;
-- name: UpdateCustomRole :one
UPDATE
custom_roles
SET
display_name = @display_name,
site_permissions = @site_permissions,
org_permissions = @org_permissions,
user_permissions = @user_permissions,
member_permissions = @member_permissions,
updated_at = now()
WHERE
name = lower(@name)
AND organization_id = @organization_id
RETURNING *;