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
This commit is contained in:
1 parent
15932db98a
commit
ee09c497de
15 files changed
+1063
-13
No files matched your search
@@ -0,0 +1,27 @@
|
||||
-- name: APITokenOrg :one
|
||||
SELECT coalesce(api_token_org(sqlc.arg(hash)::bytea), '00000000-0000-0000-0000-000000000000')::uuid AS org_id;
|
||||
|
||||
-- name: GetAPITokenByHash :one
|
||||
SELECT * FROM api_tokens WHERE token_hash = $1;
|
||||
|
||||
-- name: TouchAPIToken :exec
|
||||
UPDATE api_tokens SET last_used_at = now()
|
||||
WHERE id = $1 AND (last_used_at IS NULL OR last_used_at < now() - interval '1 minute');
|
||||
|
||||
-- name: CreateAPIToken :one
|
||||
INSERT INTO api_tokens (org_id, user_id, name, token_hash, prefix, permissions, expires_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING *;
|
||||
|
||||
-- name: ListAPITokens :many
|
||||
SELECT t.*, u.email AS user_email
|
||||
FROM api_tokens t
|
||||
JOIN users u ON u.id = t.user_id
|
||||
WHERE t.org_id = $1 AND (sqlc.narg(user_id)::uuid IS NULL OR t.user_id = sqlc.narg(user_id))
|
||||
ORDER BY t.created_at DESC;
|
||||
|
||||
-- name: GetAPIToken :one
|
||||
SELECT * FROM api_tokens WHERE id = $1;
|
||||
|
||||
-- name: DeleteAPIToken :execrows
|
||||
DELETE FROM api_tokens WHERE id = $1;
|
||||
@@ -0,0 +1,31 @@
|
||||
-- name: InvitationOrg :one
|
||||
SELECT coalesce(invitation_org(sqlc.arg(hash)::bytea), '00000000-0000-0000-0000-000000000000')::uuid AS org_id;
|
||||
|
||||
-- name: CreateInvitation :one
|
||||
INSERT INTO invitations (org_id, email, role_id, token_hash, invited_by, expires_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
RETURNING *;
|
||||
|
||||
-- name: GetInvitationByHash :one
|
||||
SELECT i.*, r.name AS role_name, o.name AS org_name, o.slug AS org_slug
|
||||
FROM invitations i
|
||||
JOIN roles r ON r.id = i.role_id
|
||||
JOIN orgs o ON o.id = i.org_id
|
||||
WHERE i.token_hash = $1;
|
||||
|
||||
-- name: ListPendingInvitations :many
|
||||
SELECT i.id, i.email, i.expires_at, i.created_at, r.name AS role_name
|
||||
FROM invitations i
|
||||
JOIN roles r ON r.id = i.role_id
|
||||
WHERE i.accepted_at IS NULL AND i.expires_at > now()
|
||||
ORDER BY i.created_at DESC;
|
||||
|
||||
-- name: GetInvitation :one
|
||||
SELECT * FROM invitations WHERE id = $1;
|
||||
|
||||
-- name: DeleteInvitation :execrows
|
||||
DELETE FROM invitations WHERE id = $1;
|
||||
|
||||
-- name: MarkInvitationAccepted :execrows
|
||||
UPDATE invitations SET accepted_at = now()
|
||||
WHERE id = $1 AND accepted_at IS NULL AND expires_at > now();
|
||||
@@ -13,3 +13,8 @@ SELECT * FROM orgs ORDER BY name;
|
||||
|
||||
-- name: DeleteOrg :execrows
|
||||
DELETE FROM orgs WHERE id = $1;
|
||||
|
||||
-- name: UpdateOrgPolicy :one
|
||||
UPDATE orgs SET require_totp = $2, require_sso = $3, updated_at = now()
|
||||
WHERE id = $1
|
||||
RETURNING *;
|
||||
@@ -0,0 +1,45 @@
|
||||
-- name: ListRoles :many
|
||||
SELECT * FROM roles ORDER BY org_id NULLS FIRST, name;
|
||||
|
||||
-- name: GetRole :one
|
||||
SELECT * FROM roles WHERE id = $1;
|
||||
|
||||
-- name: CreateRole :one
|
||||
INSERT INTO roles (org_id, name, permissions) VALUES ($1, $2, $3)
|
||||
RETURNING *;
|
||||
|
||||
-- name: UpdateRole :one
|
||||
UPDATE roles SET name = $2, permissions = $3, updated_at = now()
|
||||
WHERE id = $1 AND org_id IS NOT NULL
|
||||
RETURNING *;
|
||||
|
||||
-- name: DeleteRole :execrows
|
||||
DELETE FROM roles WHERE id = $1 AND org_id IS NOT NULL;
|
||||
|
||||
-- name: CountRoleAssignments :one
|
||||
SELECT count(*) FROM role_assignments WHERE role_id = $1;
|
||||
|
||||
-- name: MemberGrants :many
|
||||
-- Every role a member holds, with the scope it applies to.
|
||||
SELECT r.permissions, a.resource_type, a.resource_id
|
||||
FROM role_assignments a
|
||||
JOIN roles r ON r.id = a.role_id
|
||||
WHERE a.org_id = $1 AND a.user_id = $2;
|
||||
|
||||
-- name: ListAssignments :many
|
||||
SELECT a.id, a.user_id, a.role_id, r.name AS role_name, r.key AS role_key, a.resource_type, a.resource_id
|
||||
FROM role_assignments a
|
||||
JOIN roles r ON r.id = a.role_id
|
||||
WHERE a.org_id = $1
|
||||
ORDER BY r.name;
|
||||
|
||||
-- name: GetAssignment :one
|
||||
SELECT * FROM role_assignments WHERE id = $1;
|
||||
|
||||
-- name: CreateAssignment :one
|
||||
INSERT INTO role_assignments (org_id, user_id, role_id, resource_type, resource_id)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
RETURNING *;
|
||||
|
||||
-- name: DeleteAssignment :execrows
|
||||
DELETE FROM role_assignments WHERE id = $1;
|
||||
@@ -2,8 +2,8 @@
|
||||
SELECT coalesce(org_id_by_slug(sqlc.arg(slug)::citext), '00000000-0000-0000-0000-000000000000')::uuid AS id;
|
||||
|
||||
-- name: CreateSSOProvider :one
|
||||
INSERT INTO sso_providers (id, org_id, name, issuer, client_id, client_secret, scopes, allowed_domains, auto_provision, enabled)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||
INSERT INTO sso_providers (id, org_id, name, issuer, client_id, client_secret, scopes, allowed_domains, auto_provision, enabled, default_role_id)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
|
||||
RETURNING *;
|
||||
|
||||
-- name: UpdateSSOProvider :one
|
||||
@@ -16,6 +16,7 @@ UPDATE sso_providers SET
|
||||
allowed_domains = $6,
|
||||
auto_provision = $7,
|
||||
enabled = $8,
|
||||
default_role_id = $9,
|
||||
updated_at = now()
|
||||
WHERE id = $1
|
||||
RETURNING *;
|
||||
|
||||
Reference in new issue
Block a user