-- name: CreatePlatformIDP :one INSERT INTO platform_identity_providers (id, kind, name, issuer, client_id, client_secret, allow_signup, enabled) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *; -- name: UpdatePlatformIDP :one UPDATE platform_identity_providers SET name = $2, issuer = $3, client_id = $4, client_secret = coalesce(sqlc.narg(client_secret), client_secret), allow_signup = $5, enabled = $6, updated_at = now() WHERE id = $1 RETURNING *; -- name: GetPlatformIDP :one SELECT * FROM platform_identity_providers WHERE id = $1; -- name: ListPlatformIDPs :many SELECT * FROM platform_identity_providers ORDER BY name; -- name: DeletePlatformIDP :execrows DELETE FROM platform_identity_providers WHERE id = $1; -- name: GetPlatformIdentity :one SELECT * FROM platform_identities WHERE provider_id = $1 AND subject = $2; -- name: CreatePlatformIdentity :exec INSERT INTO platform_identities (provider_id, subject, user_id, email) VALUES ($1, $2, $3, $4); -- name: TouchPlatformIdentity :exec UPDATE platform_identities SET last_login_at = now(), email = coalesce(sqlc.narg(email), email) WHERE provider_id = $1 AND subject = $2; -- name: ListUserPlatformIdentities :many SELECT i.provider_id, i.email, i.created_at, p.name AS provider_name, p.kind FROM platform_identities i JOIN platform_identity_providers p ON p.id = i.provider_id WHERE i.user_id = $1 ORDER BY p.name; -- name: DeletePlatformIdentity :execrows DELETE FROM platform_identities WHERE user_id = $1 AND provider_id = $2; -- name: CountUserSignIns :one -- Ways the account can sign in besides platform identities. SELECT (password_hash IS NOT NULL)::integer + (SELECT count(*) FROM user_identities ui WHERE ui.user_id = users.id)::integer + (SELECT count(*) FROM platform_identities pi WHERE pi.user_id = users.id)::integer AS ways FROM users WHERE id = $1; -- name: SoleSignInUsers :one -- Accounts that can sign in only through this provider. SELECT count(*) FROM platform_identities pi JOIN users u ON u.id = pi.user_id WHERE pi.provider_id = $1 AND u.password_hash IS NULL AND NOT EXISTS (SELECT 1 FROM user_identities ui WHERE ui.user_id = u.id) AND NOT EXISTS (SELECT 1 FROM platform_identities o WHERE o.user_id = u.id AND o.provider_id <> pi.provider_id);