Skip to content

Commit 0b20020

Browse files
committed
chore: implement OIDCClaimFieldValues for idp sync mappings help
1 parent c3c23ed commit 0b20020

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

coderd/database/queries/user_links.sql

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ SET
5858
WHERE
5959
user_id = $7 AND login_type = $8 RETURNING *;
6060

61-
6261
-- name: OIDCClaimFields :many
6362
-- OIDCClaimFields returns a list of distinct keys in the the merged_claims fields.
6463
-- This query is used to generate the list of available sync fields for idp sync settings.
@@ -78,3 +77,35 @@ WHERE
7877
ELSE true
7978
END
8079
;
80+
81+
-- name: OIDCClaimFieldValues :many
82+
SELECT
83+
-- DISTINCT to remove duplicates
84+
DISTINCT jsonb_array_elements_text(CASE
85+
-- When the type is an array, filter out any non-string elements.
86+
-- This is to keep the return type consistent.
87+
WHEN jsonb_typeof(claims->'merged_claims'->'groups') = 'array' THEN
88+
(
89+
SELECT
90+
jsonb_agg(element)
91+
FROM
92+
jsonb_array_elements(claims->'merged_claims'->@claim_field) AS element
93+
WHERE
94+
-- Filtering out non-string elements
95+
jsonb_typeof(element) = 'string'
96+
)
97+
-- Some IDPs return a single string instead of an array of strings.
98+
WHEN jsonb_typeof(claims->'merged_claims'->'groups') = 'string' THEN
99+
jsonb_build_array(claims->'merged_claims'->@claim_field)
100+
END)::text
101+
FROM
102+
user_links
103+
WHERE
104+
-- IDP sync only supports string and array (of string) types
105+
jsonb_typeof(claims->'merged_claims'->@claim_field) = ANY(ARRAY['string', 'array'])
106+
AND login_type = 'oidc'
107+
AND CASE WHEN @organization_id :: uuid != '00000000-0000-0000-0000-000000000000'::uuid THEN
108+
user_links.user_id = ANY(SELECT organization_members.user_id FROM organization_members WHERE organization_id = @organization_id)
109+
ELSE true
110+
END
111+
;

0 commit comments

Comments
 (0)