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,194 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: invitations.sql
|
||||
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const createInvitation = `-- 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 id, org_id, email, role_id, token_hash, invited_by, expires_at, accepted_at, created_at
|
||||
`
|
||||
|
||||
type CreateInvitationParams struct {
|
||||
OrgID uuid.UUID
|
||||
Email string
|
||||
RoleID uuid.UUID
|
||||
TokenHash []byte
|
||||
InvitedBy *uuid.UUID
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) CreateInvitation(ctx context.Context, arg CreateInvitationParams) (Invitation, error) {
|
||||
row := q.db.QueryRow(ctx, createInvitation,
|
||||
arg.OrgID,
|
||||
arg.Email,
|
||||
arg.RoleID,
|
||||
arg.TokenHash,
|
||||
arg.InvitedBy,
|
||||
arg.ExpiresAt,
|
||||
)
|
||||
var i Invitation
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.OrgID,
|
||||
&i.Email,
|
||||
&i.RoleID,
|
||||
&i.TokenHash,
|
||||
&i.InvitedBy,
|
||||
&i.ExpiresAt,
|
||||
&i.AcceptedAt,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const deleteInvitation = `-- name: DeleteInvitation :execrows
|
||||
DELETE FROM invitations WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteInvitation(ctx context.Context, id uuid.UUID) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, deleteInvitation, id)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const getInvitation = `-- name: GetInvitation :one
|
||||
SELECT id, org_id, email, role_id, token_hash, invited_by, expires_at, accepted_at, created_at FROM invitations WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetInvitation(ctx context.Context, id uuid.UUID) (Invitation, error) {
|
||||
row := q.db.QueryRow(ctx, getInvitation, id)
|
||||
var i Invitation
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.OrgID,
|
||||
&i.Email,
|
||||
&i.RoleID,
|
||||
&i.TokenHash,
|
||||
&i.InvitedBy,
|
||||
&i.ExpiresAt,
|
||||
&i.AcceptedAt,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getInvitationByHash = `-- name: GetInvitationByHash :one
|
||||
SELECT i.id, i.org_id, i.email, i.role_id, i.token_hash, i.invited_by, i.expires_at, i.accepted_at, i.created_at, 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
|
||||
`
|
||||
|
||||
type GetInvitationByHashRow struct {
|
||||
ID uuid.UUID
|
||||
OrgID uuid.UUID
|
||||
Email string
|
||||
RoleID uuid.UUID
|
||||
TokenHash []byte
|
||||
InvitedBy *uuid.UUID
|
||||
ExpiresAt time.Time
|
||||
AcceptedAt *time.Time
|
||||
CreatedAt time.Time
|
||||
RoleName string
|
||||
OrgName string
|
||||
OrgSlug string
|
||||
}
|
||||
|
||||
func (q *Queries) GetInvitationByHash(ctx context.Context, tokenHash []byte) (GetInvitationByHashRow, error) {
|
||||
row := q.db.QueryRow(ctx, getInvitationByHash, tokenHash)
|
||||
var i GetInvitationByHashRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.OrgID,
|
||||
&i.Email,
|
||||
&i.RoleID,
|
||||
&i.TokenHash,
|
||||
&i.InvitedBy,
|
||||
&i.ExpiresAt,
|
||||
&i.AcceptedAt,
|
||||
&i.CreatedAt,
|
||||
&i.RoleName,
|
||||
&i.OrgName,
|
||||
&i.OrgSlug,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const invitationOrg = `-- name: InvitationOrg :one
|
||||
SELECT coalesce(invitation_org($1::bytea), '00000000-0000-0000-0000-000000000000')::uuid AS org_id
|
||||
`
|
||||
|
||||
func (q *Queries) InvitationOrg(ctx context.Context, hash []byte) (uuid.UUID, error) {
|
||||
row := q.db.QueryRow(ctx, invitationOrg, hash)
|
||||
var org_id uuid.UUID
|
||||
err := row.Scan(&org_id)
|
||||
return org_id, err
|
||||
}
|
||||
|
||||
const listPendingInvitations = `-- 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
|
||||
`
|
||||
|
||||
type ListPendingInvitationsRow struct {
|
||||
ID uuid.UUID
|
||||
Email string
|
||||
ExpiresAt time.Time
|
||||
CreatedAt time.Time
|
||||
RoleName string
|
||||
}
|
||||
|
||||
func (q *Queries) ListPendingInvitations(ctx context.Context) ([]ListPendingInvitationsRow, error) {
|
||||
rows, err := q.db.Query(ctx, listPendingInvitations)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListPendingInvitationsRow{}
|
||||
for rows.Next() {
|
||||
var i ListPendingInvitationsRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Email,
|
||||
&i.ExpiresAt,
|
||||
&i.CreatedAt,
|
||||
&i.RoleName,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const markInvitationAccepted = `-- name: MarkInvitationAccepted :execrows
|
||||
UPDATE invitations SET accepted_at = now()
|
||||
WHERE id = $1 AND accepted_at IS NULL AND expires_at > now()
|
||||
`
|
||||
|
||||
func (q *Queries) MarkInvitationAccepted(ctx context.Context, id uuid.UUID) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, markInvitationAccepted, id)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
Reference in new issue
Block a user