mirror of
https://github.com/chaitin/MonkeyCode.git
synced 2026-12-05 21:00:15 +08:00
1940 lines
48 KiB
Go
1940 lines
48 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.30.0
|
|
// source: query.sql
|
|
|
|
package sqlc
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
)
|
|
|
|
const assignCreatedUserGroups = `-- name: AssignCreatedUserGroups :exec
|
|
INSERT INTO group_users (group_id, user_id, assigned_by_user_id)
|
|
SELECT unnest($1::uuid[]), $2, $3
|
|
`
|
|
|
|
type AssignCreatedUserGroupsParams struct {
|
|
GroupIds []string
|
|
UserID string
|
|
ActorID string
|
|
}
|
|
|
|
func (q *Queries) AssignCreatedUserGroups(ctx context.Context, arg AssignCreatedUserGroupsParams) error {
|
|
_, err := q.db.Exec(ctx, assignCreatedUserGroups, arg.GroupIds, arg.UserID, arg.ActorID)
|
|
return err
|
|
}
|
|
|
|
const bindGrant = `-- name: BindGrant :execrows
|
|
UPDATE oauth_grants SET machine_id = $2, key_id = $3
|
|
WHERE id = $1 AND revoked_at IS NULL AND expires_at > now()
|
|
AND (machine_id IS NULL OR (machine_id = $2 AND key_id = $3))
|
|
`
|
|
|
|
type BindGrantParams struct {
|
|
ID string
|
|
MachineID *string
|
|
KeyID *string
|
|
}
|
|
|
|
func (q *Queries) BindGrant(ctx context.Context, arg BindGrantParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, bindGrant, arg.ID, arg.MachineID, arg.KeyID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const cleanEmailCodes = `-- name: CleanEmailCodes :exec
|
|
DELETE FROM email_codes WHERE expires_at < now()
|
|
`
|
|
|
|
func (q *Queries) CleanEmailCodes(ctx context.Context) error {
|
|
_, err := q.db.Exec(ctx, cleanEmailCodes)
|
|
return err
|
|
}
|
|
|
|
const cleanEmailDeliveries = `-- name: CleanEmailDeliveries :exec
|
|
DELETE FROM email_code_deliveries WHERE created_at < now() - interval '1 hour'
|
|
`
|
|
|
|
func (q *Queries) CleanEmailDeliveries(ctx context.Context) error {
|
|
_, err := q.db.Exec(ctx, cleanEmailDeliveries)
|
|
return err
|
|
}
|
|
|
|
const clientMayIssueToken = `-- name: ClientMayIssueToken :one
|
|
WITH policy AS (SELECT value->'client_access_policy' AS rules FROM settings WHERE key='authentication' FOR SHARE)
|
|
SELECT COALESCE((SELECT jsonb_typeof(rules)='object' AND COALESCE(rules->($1::text)='true'::jsonb,true) FROM policy),true)
|
|
OR ($1::text='monkeyai-runtime' AND EXISTS (
|
|
SELECT 1 FROM oauth_grants g JOIN environment_runtimes r ON r.machine_id=g.requested_machine_id
|
|
JOIN environments e ON e.id=r.environment_id AND e.user_id=g.user_id
|
|
WHERE g.id=$2::uuid AND g.client_id='monkeyai-runtime' AND g.purpose='runtime'
|
|
AND g.requested_key_id=translate(encode(sha256(r.public_key),'base64'),'+/=','-_')
|
|
)) AS allowed
|
|
`
|
|
|
|
type ClientMayIssueTokenParams struct {
|
|
ClientID string
|
|
GrantID string
|
|
}
|
|
|
|
// Applies to issuance/rotation only, never to an already issued access token.
|
|
func (q *Queries) ClientMayIssueToken(ctx context.Context, arg ClientMayIssueTokenParams) (*bool, error) {
|
|
row := q.db.QueryRow(ctx, clientMayIssueToken, arg.ClientID, arg.GrantID)
|
|
var allowed *bool
|
|
err := row.Scan(&allowed)
|
|
return allowed, err
|
|
}
|
|
|
|
const completeAuthorizationRequest = `-- name: CompleteAuthorizationRequest :execresult
|
|
UPDATE
|
|
oauth_authorization_requests
|
|
SET
|
|
completed_at = now()
|
|
WHERE
|
|
id = $1
|
|
AND completed_at IS NULL
|
|
AND expires_at > now()
|
|
`
|
|
|
|
func (q *Queries) CompleteAuthorizationRequest(ctx context.Context, id string) (pgconn.CommandTag, error) {
|
|
return q.db.Exec(ctx, completeAuthorizationRequest, id)
|
|
}
|
|
|
|
const consumeLoginState = `-- name: ConsumeLoginState :one
|
|
UPDATE
|
|
oauth_login_states
|
|
SET
|
|
consumed_at = now()
|
|
WHERE
|
|
state_hash = $1
|
|
AND consumed_at IS NULL
|
|
AND expires_at > now()
|
|
RETURNING
|
|
connection_id,
|
|
coalesce(authorization_request_id::text, '')::text AS authorization_request_id,
|
|
purpose,
|
|
coalesce(device_request_id::text,'')::text AS device_request_id
|
|
`
|
|
|
|
type ConsumeLoginStateRow struct {
|
|
ConnectionID string
|
|
AuthorizationRequestID string
|
|
Purpose string
|
|
DeviceRequestID string
|
|
}
|
|
|
|
func (q *Queries) ConsumeLoginState(ctx context.Context, stateHash string) (ConsumeLoginStateRow, error) {
|
|
row := q.db.QueryRow(ctx, consumeLoginState, stateHash)
|
|
var i ConsumeLoginStateRow
|
|
err := row.Scan(
|
|
&i.ConnectionID,
|
|
&i.AuthorizationRequestID,
|
|
&i.Purpose,
|
|
&i.DeviceRequestID,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const countUsers = `-- name: CountUsers :one
|
|
SELECT
|
|
count(*)
|
|
FROM
|
|
users
|
|
WHERE
|
|
deleted_at IS NULL
|
|
`
|
|
|
|
func (q *Queries) CountUsers(ctx context.Context) (int64, error) {
|
|
row := q.db.QueryRow(ctx, countUsers)
|
|
var count int64
|
|
err := row.Scan(&count)
|
|
return count, err
|
|
}
|
|
|
|
const createAuthorizationCode = `-- name: CreateAuthorizationCode :execresult
|
|
INSERT INTO oauth_authorization_codes (code_hash, authorization_request_id, user_id, client_id, redirect_uri,
|
|
code_challenge, expires_at, scopes, audience)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
|
`
|
|
|
|
type CreateAuthorizationCodeParams struct {
|
|
CodeHash string
|
|
AuthorizationRequestID string
|
|
UserID string
|
|
ClientID string
|
|
RedirectUri string
|
|
CodeChallenge string
|
|
ExpiresAt time.Time
|
|
Scopes []string
|
|
Audience string
|
|
}
|
|
|
|
func (q *Queries) CreateAuthorizationCode(ctx context.Context, arg CreateAuthorizationCodeParams) (pgconn.CommandTag, error) {
|
|
return q.db.Exec(ctx, createAuthorizationCode,
|
|
arg.CodeHash,
|
|
arg.AuthorizationRequestID,
|
|
arg.UserID,
|
|
arg.ClientID,
|
|
arg.RedirectUri,
|
|
arg.CodeChallenge,
|
|
arg.ExpiresAt,
|
|
arg.Scopes,
|
|
arg.Audience,
|
|
)
|
|
}
|
|
|
|
const createAuthorizationRequest = `-- name: CreateAuthorizationRequest :one
|
|
INSERT INTO oauth_authorization_requests (client_id, redirect_uri, state, code_challenge, code_challenge_method, expires_at, scopes, audience)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
|
RETURNING
|
|
id
|
|
`
|
|
|
|
type CreateAuthorizationRequestParams struct {
|
|
ClientID string
|
|
RedirectUri string
|
|
State string
|
|
CodeChallenge string
|
|
CodeChallengeMethod string
|
|
ExpiresAt time.Time
|
|
Scopes []string
|
|
Audience string
|
|
}
|
|
|
|
func (q *Queries) CreateAuthorizationRequest(ctx context.Context, arg CreateAuthorizationRequestParams) (string, error) {
|
|
row := q.db.QueryRow(ctx, createAuthorizationRequest,
|
|
arg.ClientID,
|
|
arg.RedirectUri,
|
|
arg.State,
|
|
arg.CodeChallenge,
|
|
arg.CodeChallengeMethod,
|
|
arg.ExpiresAt,
|
|
arg.Scopes,
|
|
arg.Audience,
|
|
)
|
|
var id string
|
|
err := row.Scan(&id)
|
|
return id, err
|
|
}
|
|
|
|
const createBrowserSession = `-- name: CreateBrowserSession :execresult
|
|
INSERT INTO browser_sessions (token_hash, user_id, authentication_method, expires_at)
|
|
VALUES ($1, $2, $3, $4)
|
|
`
|
|
|
|
type CreateBrowserSessionParams struct {
|
|
TokenHash string
|
|
UserID string
|
|
AuthenticationMethod string
|
|
ExpiresAt time.Time
|
|
}
|
|
|
|
func (q *Queries) CreateBrowserSession(ctx context.Context, arg CreateBrowserSessionParams) (pgconn.CommandTag, error) {
|
|
return q.db.Exec(ctx, createBrowserSession,
|
|
arg.TokenHash,
|
|
arg.UserID,
|
|
arg.AuthenticationMethod,
|
|
arg.ExpiresAt,
|
|
)
|
|
}
|
|
|
|
const createEmailUser = `-- name: CreateEmailUser :exec
|
|
INSERT INTO users (name, email, ROLE)
|
|
VALUES ($1, $2, 'user')
|
|
ON CONFLICT (lower(email)) WHERE deleted_at IS NULL DO NOTHING
|
|
`
|
|
|
|
type CreateEmailUserParams struct {
|
|
Name string
|
|
Email string
|
|
}
|
|
|
|
func (q *Queries) CreateEmailUser(ctx context.Context, arg CreateEmailUserParams) error {
|
|
_, err := q.db.Exec(ctx, createEmailUser, arg.Name, arg.Email)
|
|
return err
|
|
}
|
|
|
|
const createGrant = `-- name: CreateGrant :one
|
|
INSERT INTO oauth_grants(user_id, client_id, scopes, audience, expires_at)
|
|
VALUES ($1, $2, $3, $4, $5) RETURNING id, user_id, client_id, scopes, audience, purpose, machine_id, key_id, revocation_version, expires_at, created_at, revoked_at, revoke_reason, requested_machine_id, requested_key_id, restore_approved
|
|
`
|
|
|
|
type CreateGrantParams struct {
|
|
UserID string
|
|
ClientID string
|
|
Scopes []string
|
|
Audience string
|
|
ExpiresAt time.Time
|
|
}
|
|
|
|
func (q *Queries) CreateGrant(ctx context.Context, arg CreateGrantParams) (OauthGrant, error) {
|
|
row := q.db.QueryRow(ctx, createGrant,
|
|
arg.UserID,
|
|
arg.ClientID,
|
|
arg.Scopes,
|
|
arg.Audience,
|
|
arg.ExpiresAt,
|
|
)
|
|
var i OauthGrant
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.UserID,
|
|
&i.ClientID,
|
|
&i.Scopes,
|
|
&i.Audience,
|
|
&i.Purpose,
|
|
&i.MachineID,
|
|
&i.KeyID,
|
|
&i.RevocationVersion,
|
|
&i.ExpiresAt,
|
|
&i.CreatedAt,
|
|
&i.RevokedAt,
|
|
&i.RevokeReason,
|
|
&i.RequestedMachineID,
|
|
&i.RequestedKeyID,
|
|
&i.RestoreApproved,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const createIdentityUser = `-- name: CreateIdentityUser :one
|
|
INSERT INTO users (name, email, avatar_url, ROLE, last_login_at)
|
|
VALUES ($1, $2, NULLIF ($3::text, ''),
|
|
'user', now())
|
|
ON CONFLICT (lower(email))
|
|
WHERE
|
|
deleted_at IS NULL
|
|
DO UPDATE SET
|
|
name = EXCLUDED.name,
|
|
avatar_url = EXCLUDED.avatar_url,
|
|
last_login_at = now(),
|
|
updated_at = now()
|
|
WHERE
|
|
users.status = 'active'
|
|
RETURNING
|
|
id,
|
|
name,
|
|
email,
|
|
coalesce(nullif(btrim(custom_avatar_url), ''), nullif(btrim(avatar_url), ''), '')::text AS avatar_url,
|
|
ROLE,
|
|
status,
|
|
joined_at,
|
|
last_login_at
|
|
`
|
|
|
|
type CreateIdentityUserParams struct {
|
|
Name string
|
|
Email string
|
|
AvatarUrl string
|
|
}
|
|
|
|
type CreateIdentityUserRow struct {
|
|
ID string
|
|
Name string
|
|
Email string
|
|
AvatarUrl string
|
|
Role string
|
|
Status string
|
|
JoinedAt time.Time
|
|
LastLoginAt *time.Time
|
|
}
|
|
|
|
func (q *Queries) CreateIdentityUser(ctx context.Context, arg CreateIdentityUserParams) (CreateIdentityUserRow, error) {
|
|
row := q.db.QueryRow(ctx, createIdentityUser, arg.Name, arg.Email, arg.AvatarUrl)
|
|
var i CreateIdentityUserRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Email,
|
|
&i.AvatarUrl,
|
|
&i.Role,
|
|
&i.Status,
|
|
&i.JoinedAt,
|
|
&i.LastLoginAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const createInitialAdmin = `-- name: CreateInitialAdmin :execresult
|
|
INSERT INTO users (name, email, password_hash, ROLE, status)
|
|
VALUES ($1, $2, $3, 'admin', 'active')
|
|
`
|
|
|
|
type CreateInitialAdminParams struct {
|
|
Name string
|
|
Email string
|
|
PasswordHash *string
|
|
}
|
|
|
|
func (q *Queries) CreateInitialAdmin(ctx context.Context, arg CreateInitialAdminParams) (pgconn.CommandTag, error) {
|
|
return q.db.Exec(ctx, createInitialAdmin, arg.Name, arg.Email, arg.PasswordHash)
|
|
}
|
|
|
|
const createLoginState = `-- name: CreateLoginState :execresult
|
|
INSERT INTO oauth_login_states (state_hash, connection_id, authorization_request_id, purpose, expires_at,device_request_id)
|
|
VALUES ($1, $2, NULLIF ($3::text, '')::uuid,
|
|
$4, $5,NULLIF($6::text,'')::uuid)
|
|
`
|
|
|
|
type CreateLoginStateParams struct {
|
|
StateHash string
|
|
ConnectionID string
|
|
AuthorizationRequestID string
|
|
Purpose string
|
|
ExpiresAt time.Time
|
|
DeviceRequestID string
|
|
}
|
|
|
|
func (q *Queries) CreateLoginState(ctx context.Context, arg CreateLoginStateParams) (pgconn.CommandTag, error) {
|
|
return q.db.Exec(ctx, createLoginState,
|
|
arg.StateHash,
|
|
arg.ConnectionID,
|
|
arg.AuthorizationRequestID,
|
|
arg.Purpose,
|
|
arg.ExpiresAt,
|
|
arg.DeviceRequestID,
|
|
)
|
|
}
|
|
|
|
const createToken = `-- name: CreateToken :execresult
|
|
INSERT INTO oauth_tokens (user_id, client_id, access_token_hash, refresh_token_hash, access_expires_at, refresh_expires_at, grant_id, parent_id)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
|
`
|
|
|
|
type CreateTokenParams struct {
|
|
UserID string
|
|
ClientID string
|
|
AccessTokenHash string
|
|
RefreshTokenHash string
|
|
AccessExpiresAt time.Time
|
|
RefreshExpiresAt time.Time
|
|
GrantID string
|
|
ParentID *string
|
|
}
|
|
|
|
func (q *Queries) CreateToken(ctx context.Context, arg CreateTokenParams) (pgconn.CommandTag, error) {
|
|
return q.db.Exec(ctx, createToken,
|
|
arg.UserID,
|
|
arg.ClientID,
|
|
arg.AccessTokenHash,
|
|
arg.RefreshTokenHash,
|
|
arg.AccessExpiresAt,
|
|
arg.RefreshExpiresAt,
|
|
arg.GrantID,
|
|
arg.ParentID,
|
|
)
|
|
}
|
|
|
|
const createUser = `-- name: CreateUser :one
|
|
INSERT INTO users (name, email, ROLE, password_hash)
|
|
VALUES ($1, $2, $3, $4)
|
|
RETURNING
|
|
id, name, email, coalesce(nullif(btrim(custom_avatar_url), ''), nullif(btrim(avatar_url), ''), '')::text AS avatar_url, ROLE, status, joined_at, last_login_at
|
|
`
|
|
|
|
type CreateUserParams struct {
|
|
Name string
|
|
Email string
|
|
Role string
|
|
PasswordHash *string
|
|
}
|
|
|
|
type CreateUserRow struct {
|
|
ID string
|
|
Name string
|
|
Email string
|
|
AvatarUrl string
|
|
Role string
|
|
Status string
|
|
JoinedAt time.Time
|
|
LastLoginAt *time.Time
|
|
}
|
|
|
|
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (CreateUserRow, error) {
|
|
row := q.db.QueryRow(ctx, createUser,
|
|
arg.Name,
|
|
arg.Email,
|
|
arg.Role,
|
|
arg.PasswordHash,
|
|
)
|
|
var i CreateUserRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Email,
|
|
&i.AvatarUrl,
|
|
&i.Role,
|
|
&i.Status,
|
|
&i.JoinedAt,
|
|
&i.LastLoginAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const deleteEmailCode = `-- name: DeleteEmailCode :exec
|
|
DELETE FROM email_codes WHERE email = $1 AND purpose = $2
|
|
`
|
|
|
|
type DeleteEmailCodeParams struct {
|
|
Email string
|
|
Purpose string
|
|
}
|
|
|
|
func (q *Queries) DeleteEmailCode(ctx context.Context, arg DeleteEmailCodeParams) error {
|
|
_, err := q.db.Exec(ctx, deleteEmailCode, arg.Email, arg.Purpose)
|
|
return err
|
|
}
|
|
|
|
const emailDeliveryLimited = `-- name: EmailDeliveryLimited :one
|
|
SELECT (count(*) FILTER (WHERE email = $1) >= 10
|
|
OR count(*) FILTER (WHERE ip_hash = $2) >= 30
|
|
OR count(*) FILTER (WHERE email = $1 AND created_at > now() - interval '30 seconds') > 0)::boolean AS limited
|
|
FROM email_code_deliveries
|
|
`
|
|
|
|
type EmailDeliveryLimitedParams struct {
|
|
Email string
|
|
IpHash string
|
|
}
|
|
|
|
func (q *Queries) EmailDeliveryLimited(ctx context.Context, arg EmailDeliveryLimitedParams) (bool, error) {
|
|
row := q.db.QueryRow(ctx, emailDeliveryLimited, arg.Email, arg.IpHash)
|
|
var limited bool
|
|
err := row.Scan(&limited)
|
|
return limited, err
|
|
}
|
|
|
|
const extendGrant = `-- name: ExtendGrant :exec
|
|
UPDATE oauth_grants SET expires_at = $2 WHERE id = $1 AND revoked_at IS NULL
|
|
`
|
|
|
|
type ExtendGrantParams struct {
|
|
ID string
|
|
ExpiresAt time.Time
|
|
}
|
|
|
|
func (q *Queries) ExtendGrant(ctx context.Context, arg ExtendGrantParams) error {
|
|
_, err := q.db.Exec(ctx, extendGrant, arg.ID, arg.ExpiresAt)
|
|
return err
|
|
}
|
|
|
|
const failEmailCode = `-- name: FailEmailCode :exec
|
|
UPDATE email_codes SET attempts = attempts + 1 WHERE email = $1 AND purpose = $2
|
|
`
|
|
|
|
type FailEmailCodeParams struct {
|
|
Email string
|
|
Purpose string
|
|
}
|
|
|
|
func (q *Queries) FailEmailCode(ctx context.Context, arg FailEmailCodeParams) error {
|
|
_, err := q.db.Exec(ctx, failEmailCode, arg.Email, arg.Purpose)
|
|
return err
|
|
}
|
|
|
|
const findTokenGrant = `-- name: FindTokenGrant :one
|
|
SELECT grant_id FROM oauth_tokens
|
|
WHERE client_id = $2 AND (access_token_hash = $1 OR refresh_token_hash = $1)
|
|
`
|
|
|
|
type FindTokenGrantParams struct {
|
|
AccessTokenHash string
|
|
ClientID string
|
|
}
|
|
|
|
func (q *Queries) FindTokenGrant(ctx context.Context, arg FindTokenGrantParams) (string, error) {
|
|
row := q.db.QueryRow(ctx, findTokenGrant, arg.AccessTokenHash, arg.ClientID)
|
|
var grant_id string
|
|
err := row.Scan(&grant_id)
|
|
return grant_id, err
|
|
}
|
|
|
|
const getAccessCredential = `-- name: GetAccessCredential :one
|
|
SELECT t.access_expires_at, t.user_id, g.id AS grant_id, t.client_id,
|
|
g.scopes, g.audience, g.purpose, g.machine_id, g.key_id, g.revocation_version
|
|
FROM oauth_tokens t
|
|
JOIN users u ON u.id = t.user_id
|
|
JOIN oauth_grants g ON g.id = t.grant_id
|
|
WHERE t.access_token_hash = $1
|
|
AND t.revoked_at IS NULL AND t.access_expires_at > now()
|
|
AND g.revoked_at IS NULL AND g.expires_at > now()
|
|
AND u.status = 'active' AND u.deleted_at IS NULL
|
|
`
|
|
|
|
type GetAccessCredentialRow struct {
|
|
AccessExpiresAt time.Time
|
|
UserID string
|
|
GrantID string
|
|
ClientID string
|
|
Scopes []string
|
|
Audience string
|
|
Purpose string
|
|
MachineID *string
|
|
KeyID *string
|
|
RevocationVersion int64
|
|
}
|
|
|
|
func (q *Queries) GetAccessCredential(ctx context.Context, accessTokenHash string) (GetAccessCredentialRow, error) {
|
|
row := q.db.QueryRow(ctx, getAccessCredential, accessTokenHash)
|
|
var i GetAccessCredentialRow
|
|
err := row.Scan(
|
|
&i.AccessExpiresAt,
|
|
&i.UserID,
|
|
&i.GrantID,
|
|
&i.ClientID,
|
|
&i.Scopes,
|
|
&i.Audience,
|
|
&i.Purpose,
|
|
&i.MachineID,
|
|
&i.KeyID,
|
|
&i.RevocationVersion,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getAuthorizationCode = `-- name: GetAuthorizationCode :one
|
|
SELECT
|
|
id,
|
|
user_id,
|
|
client_id,
|
|
redirect_uri,
|
|
code_challenge,
|
|
expires_at,
|
|
redeemed_at,
|
|
scopes,
|
|
audience
|
|
FROM
|
|
oauth_authorization_codes
|
|
WHERE
|
|
code_hash = $1
|
|
`
|
|
|
|
type GetAuthorizationCodeRow struct {
|
|
ID string
|
|
UserID string
|
|
ClientID string
|
|
RedirectUri string
|
|
CodeChallenge string
|
|
ExpiresAt time.Time
|
|
RedeemedAt *time.Time
|
|
Scopes []string
|
|
Audience string
|
|
}
|
|
|
|
func (q *Queries) GetAuthorizationCode(ctx context.Context, codeHash string) (GetAuthorizationCodeRow, error) {
|
|
row := q.db.QueryRow(ctx, getAuthorizationCode, codeHash)
|
|
var i GetAuthorizationCodeRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.UserID,
|
|
&i.ClientID,
|
|
&i.RedirectUri,
|
|
&i.CodeChallenge,
|
|
&i.ExpiresAt,
|
|
&i.RedeemedAt,
|
|
&i.Scopes,
|
|
&i.Audience,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getAuthorizationRequest = `-- name: GetAuthorizationRequest :one
|
|
SELECT
|
|
id,
|
|
client_id,
|
|
redirect_uri,
|
|
state,
|
|
code_challenge,
|
|
code_challenge_method,
|
|
expires_at,
|
|
completed_at,
|
|
scopes,
|
|
audience
|
|
FROM
|
|
oauth_authorization_requests
|
|
WHERE
|
|
id = $1
|
|
`
|
|
|
|
type GetAuthorizationRequestRow struct {
|
|
ID string
|
|
ClientID string
|
|
RedirectUri string
|
|
State string
|
|
CodeChallenge string
|
|
CodeChallengeMethod string
|
|
ExpiresAt time.Time
|
|
CompletedAt *time.Time
|
|
Scopes []string
|
|
Audience string
|
|
}
|
|
|
|
func (q *Queries) GetAuthorizationRequest(ctx context.Context, id string) (GetAuthorizationRequestRow, error) {
|
|
row := q.db.QueryRow(ctx, getAuthorizationRequest, id)
|
|
var i GetAuthorizationRequestRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.ClientID,
|
|
&i.RedirectUri,
|
|
&i.State,
|
|
&i.CodeChallenge,
|
|
&i.CodeChallengeMethod,
|
|
&i.ExpiresAt,
|
|
&i.CompletedAt,
|
|
&i.Scopes,
|
|
&i.Audience,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getBrowserUser = `-- name: GetBrowserUser :one
|
|
SELECT
|
|
u.id,
|
|
u.name,
|
|
u.email,
|
|
coalesce(nullif(btrim(u.custom_avatar_url), ''), nullif(btrim(u.avatar_url), ''), '')::text AS avatar_url,
|
|
u.role,
|
|
u.status,
|
|
u.joined_at,
|
|
u.last_login_at,
|
|
s.authentication_method
|
|
FROM
|
|
browser_sessions s
|
|
JOIN users u ON u.id = s.user_id
|
|
WHERE
|
|
s.token_hash = $1
|
|
AND s.revoked_at IS NULL
|
|
AND s.expires_at > now()
|
|
AND u.status = 'active'
|
|
AND u.deleted_at IS NULL
|
|
`
|
|
|
|
type GetBrowserUserRow struct {
|
|
ID string
|
|
Name string
|
|
Email string
|
|
AvatarUrl string
|
|
Role string
|
|
Status string
|
|
JoinedAt time.Time
|
|
LastLoginAt *time.Time
|
|
AuthenticationMethod string
|
|
}
|
|
|
|
func (q *Queries) GetBrowserUser(ctx context.Context, tokenHash string) (GetBrowserUserRow, error) {
|
|
row := q.db.QueryRow(ctx, getBrowserUser, tokenHash)
|
|
var i GetBrowserUserRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Email,
|
|
&i.AvatarUrl,
|
|
&i.Role,
|
|
&i.Status,
|
|
&i.JoinedAt,
|
|
&i.LastLoginAt,
|
|
&i.AuthenticationMethod,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getEmailCode = `-- name: GetEmailCode :one
|
|
SELECT code_hash, attempts FROM email_codes WHERE email = $1 AND purpose = $2 AND ready AND expires_at > now() FOR UPDATE
|
|
`
|
|
|
|
type GetEmailCodeParams struct {
|
|
Email string
|
|
Purpose string
|
|
}
|
|
|
|
type GetEmailCodeRow struct {
|
|
CodeHash string
|
|
Attempts int32
|
|
}
|
|
|
|
func (q *Queries) GetEmailCode(ctx context.Context, arg GetEmailCodeParams) (GetEmailCodeRow, error) {
|
|
row := q.db.QueryRow(ctx, getEmailCode, arg.Email, arg.Purpose)
|
|
var i GetEmailCodeRow
|
|
err := row.Scan(&i.CodeHash, &i.Attempts)
|
|
return i, err
|
|
}
|
|
|
|
const getGrant = `-- name: GetGrant :one
|
|
SELECT g.id, g.user_id, g.client_id, g.scopes, g.audience, g.purpose, g.machine_id, g.key_id, g.revocation_version, g.expires_at, g.created_at, g.revoked_at, g.revoke_reason, g.requested_machine_id, g.requested_key_id, g.restore_approved FROM oauth_grants g JOIN users u ON u.id = g.user_id
|
|
WHERE g.id = $1 AND g.revoked_at IS NULL AND g.expires_at > now()
|
|
AND u.status = 'active' AND u.deleted_at IS NULL
|
|
`
|
|
|
|
func (q *Queries) GetGrant(ctx context.Context, id string) (OauthGrant, error) {
|
|
row := q.db.QueryRow(ctx, getGrant, id)
|
|
var i OauthGrant
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.UserID,
|
|
&i.ClientID,
|
|
&i.Scopes,
|
|
&i.Audience,
|
|
&i.Purpose,
|
|
&i.MachineID,
|
|
&i.KeyID,
|
|
&i.RevocationVersion,
|
|
&i.ExpiresAt,
|
|
&i.CreatedAt,
|
|
&i.RevokedAt,
|
|
&i.RevokeReason,
|
|
&i.RequestedMachineID,
|
|
&i.RequestedKeyID,
|
|
&i.RestoreApproved,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getIdentityUser = `-- name: GetIdentityUser :one
|
|
SELECT
|
|
u.id,
|
|
u.name,
|
|
u.email,
|
|
coalesce(nullif(btrim(u.custom_avatar_url), ''), nullif(btrim(u.avatar_url), ''), '')::text AS avatar_url,
|
|
u.role,
|
|
u.status,
|
|
u.joined_at,
|
|
u.last_login_at
|
|
FROM
|
|
user_identities i
|
|
JOIN users u ON u.id = i.user_id
|
|
WHERE
|
|
i.provider = $1
|
|
AND i.issuer = $2
|
|
AND i.provider_subject = $3
|
|
AND i.deleted_at IS NULL
|
|
AND u.deleted_at IS NULL
|
|
`
|
|
|
|
type GetIdentityUserParams struct {
|
|
Provider string
|
|
Issuer string
|
|
ProviderSubject string
|
|
}
|
|
|
|
type GetIdentityUserRow struct {
|
|
ID string
|
|
Name string
|
|
Email string
|
|
AvatarUrl string
|
|
Role string
|
|
Status string
|
|
JoinedAt time.Time
|
|
LastLoginAt *time.Time
|
|
}
|
|
|
|
func (q *Queries) GetIdentityUser(ctx context.Context, arg GetIdentityUserParams) (GetIdentityUserRow, error) {
|
|
row := q.db.QueryRow(ctx, getIdentityUser, arg.Provider, arg.Issuer, arg.ProviderSubject)
|
|
var i GetIdentityUserRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Email,
|
|
&i.AvatarUrl,
|
|
&i.Role,
|
|
&i.Status,
|
|
&i.JoinedAt,
|
|
&i.LastLoginAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getPasswordUser = `-- name: GetPasswordUser :one
|
|
SELECT
|
|
id,
|
|
name,
|
|
email,
|
|
coalesce(nullif(btrim(custom_avatar_url), ''), nullif(btrim(avatar_url), ''), '')::text AS avatar_url,
|
|
ROLE,
|
|
status,
|
|
joined_at,
|
|
last_login_at,
|
|
password_hash
|
|
FROM
|
|
users
|
|
WHERE
|
|
lower(email) = $1
|
|
AND (NOT $2::boolean OR ROLE = 'admin')
|
|
AND status = 'active'
|
|
AND deleted_at IS NULL
|
|
AND password_hash IS NOT NULL
|
|
`
|
|
|
|
type GetPasswordUserParams struct {
|
|
Email string
|
|
AdminOnly bool
|
|
}
|
|
|
|
type GetPasswordUserRow struct {
|
|
ID string
|
|
Name string
|
|
Email string
|
|
AvatarUrl string
|
|
Role string
|
|
Status string
|
|
JoinedAt time.Time
|
|
LastLoginAt *time.Time
|
|
PasswordHash *string
|
|
}
|
|
|
|
func (q *Queries) GetPasswordUser(ctx context.Context, arg GetPasswordUserParams) (GetPasswordUserRow, error) {
|
|
row := q.db.QueryRow(ctx, getPasswordUser, arg.Email, arg.AdminOnly)
|
|
var i GetPasswordUserRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Email,
|
|
&i.AvatarUrl,
|
|
&i.Role,
|
|
&i.Status,
|
|
&i.JoinedAt,
|
|
&i.LastLoginAt,
|
|
&i.PasswordHash,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getRefreshToken = `-- name: GetRefreshToken :one
|
|
SELECT id, user_id, client_id, access_token_hash, refresh_token_hash, access_expires_at, refresh_expires_at, created_at, revoked_at, grant_id, parent_id, rotated_at FROM oauth_tokens WHERE refresh_token_hash = $1 AND client_id = $2
|
|
`
|
|
|
|
type GetRefreshTokenParams struct {
|
|
RefreshTokenHash string
|
|
ClientID string
|
|
}
|
|
|
|
func (q *Queries) GetRefreshToken(ctx context.Context, arg GetRefreshTokenParams) (OauthToken, error) {
|
|
row := q.db.QueryRow(ctx, getRefreshToken, arg.RefreshTokenHash, arg.ClientID)
|
|
var i OauthToken
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.UserID,
|
|
&i.ClientID,
|
|
&i.AccessTokenHash,
|
|
&i.RefreshTokenHash,
|
|
&i.AccessExpiresAt,
|
|
&i.RefreshExpiresAt,
|
|
&i.CreatedAt,
|
|
&i.RevokedAt,
|
|
&i.GrantID,
|
|
&i.ParentID,
|
|
&i.RotatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getTokenUser = `-- name: GetTokenUser :one
|
|
SELECT
|
|
u.id,
|
|
u.name,
|
|
u.email,
|
|
coalesce(nullif(btrim(u.custom_avatar_url), ''), nullif(btrim(u.avatar_url), ''), '')::text AS avatar_url,
|
|
u.role,
|
|
u.status,
|
|
u.joined_at,
|
|
u.last_login_at,
|
|
t.access_expires_at,
|
|
g.id AS grant_id,
|
|
t.client_id,
|
|
g.scopes,
|
|
g.audience,
|
|
g.purpose,
|
|
g.machine_id,
|
|
g.requested_machine_id,
|
|
g.key_id,
|
|
g.revocation_version
|
|
FROM
|
|
oauth_tokens t
|
|
JOIN users u ON u.id = t.user_id
|
|
JOIN oauth_grants g ON g.id = t.grant_id
|
|
WHERE
|
|
t.access_token_hash = $1
|
|
AND t.revoked_at IS NULL
|
|
AND t.access_expires_at > now()
|
|
AND g.revoked_at IS NULL AND g.expires_at > now()
|
|
AND u.status = 'active'
|
|
AND u.deleted_at IS NULL
|
|
`
|
|
|
|
type GetTokenUserRow struct {
|
|
ID string
|
|
Name string
|
|
Email string
|
|
AvatarUrl string
|
|
Role string
|
|
Status string
|
|
JoinedAt time.Time
|
|
LastLoginAt *time.Time
|
|
AccessExpiresAt time.Time
|
|
GrantID string
|
|
ClientID string
|
|
Scopes []string
|
|
Audience string
|
|
Purpose string
|
|
MachineID *string
|
|
RequestedMachineID *string
|
|
KeyID *string
|
|
RevocationVersion int64
|
|
}
|
|
|
|
func (q *Queries) GetTokenUser(ctx context.Context, accessTokenHash string) (GetTokenUserRow, error) {
|
|
row := q.db.QueryRow(ctx, getTokenUser, accessTokenHash)
|
|
var i GetTokenUserRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Email,
|
|
&i.AvatarUrl,
|
|
&i.Role,
|
|
&i.Status,
|
|
&i.JoinedAt,
|
|
&i.LastLoginAt,
|
|
&i.AccessExpiresAt,
|
|
&i.GrantID,
|
|
&i.ClientID,
|
|
&i.Scopes,
|
|
&i.Audience,
|
|
&i.Purpose,
|
|
&i.MachineID,
|
|
&i.RequestedMachineID,
|
|
&i.KeyID,
|
|
&i.RevocationVersion,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getUser = `-- name: GetUser :one
|
|
SELECT
|
|
id,
|
|
name,
|
|
email,
|
|
coalesce(nullif(btrim(custom_avatar_url), ''), nullif(btrim(avatar_url), ''), '')::text AS avatar_url,
|
|
ROLE,
|
|
status,
|
|
joined_at,
|
|
last_login_at
|
|
FROM
|
|
users
|
|
WHERE
|
|
id = $1
|
|
AND deleted_at IS NULL
|
|
`
|
|
|
|
type GetUserRow struct {
|
|
ID string
|
|
Name string
|
|
Email string
|
|
AvatarUrl string
|
|
Role string
|
|
Status string
|
|
JoinedAt time.Time
|
|
LastLoginAt *time.Time
|
|
}
|
|
|
|
func (q *Queries) GetUser(ctx context.Context, id string) (GetUserRow, error) {
|
|
row := q.db.QueryRow(ctx, getUser, id)
|
|
var i GetUserRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Email,
|
|
&i.AvatarUrl,
|
|
&i.Role,
|
|
&i.Status,
|
|
&i.JoinedAt,
|
|
&i.LastLoginAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getUserByEmail = `-- name: GetUserByEmail :one
|
|
SELECT
|
|
id,
|
|
name,
|
|
email,
|
|
coalesce(nullif(btrim(custom_avatar_url), ''), nullif(btrim(avatar_url), ''), '')::text AS avatar_url,
|
|
ROLE,
|
|
status,
|
|
joined_at,
|
|
last_login_at
|
|
FROM
|
|
users
|
|
WHERE
|
|
lower(email) = lower($1)
|
|
AND deleted_at IS NULL
|
|
`
|
|
|
|
type GetUserByEmailRow struct {
|
|
ID string
|
|
Name string
|
|
Email string
|
|
AvatarUrl string
|
|
Role string
|
|
Status string
|
|
JoinedAt time.Time
|
|
LastLoginAt *time.Time
|
|
}
|
|
|
|
func (q *Queries) GetUserByEmail(ctx context.Context, lower string) (GetUserByEmailRow, error) {
|
|
row := q.db.QueryRow(ctx, getUserByEmail, lower)
|
|
var i GetUserByEmailRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Email,
|
|
&i.AvatarUrl,
|
|
&i.Role,
|
|
&i.Status,
|
|
&i.JoinedAt,
|
|
&i.LastLoginAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getUserCreationGroups = `-- name: GetUserCreationGroups :many
|
|
SELECT id
|
|
FROM groups
|
|
WHERE id = ANY ($1::uuid[]) AND deleted_at IS NULL
|
|
ORDER BY id
|
|
FOR SHARE
|
|
`
|
|
|
|
func (q *Queries) GetUserCreationGroups(ctx context.Context, dollar_1 []string) ([]string, error) {
|
|
rows, err := q.db.Query(ctx, getUserCreationGroups, dollar_1)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []string{}
|
|
for rows.Next() {
|
|
var id string
|
|
if err := rows.Scan(&id); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, id)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const getUserSupervisedGroups = `-- name: GetUserSupervisedGroups :many
|
|
SELECT g.id, g.name
|
|
FROM group_users gu
|
|
JOIN groups g ON g.id = gu.group_id AND g.deleted_at IS NULL
|
|
WHERE gu.user_id = $1 AND gu.is_supervisor AND gu.removed_at IS NULL
|
|
ORDER BY g.name, g.id
|
|
`
|
|
|
|
type GetUserSupervisedGroupsRow struct {
|
|
ID string
|
|
Name string
|
|
}
|
|
|
|
func (q *Queries) GetUserSupervisedGroups(ctx context.Context, userID string) ([]GetUserSupervisedGroupsRow, error) {
|
|
rows, err := q.db.Query(ctx, getUserSupervisedGroups, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []GetUserSupervisedGroupsRow{}
|
|
for rows.Next() {
|
|
var i GetUserSupervisedGroupsRow
|
|
if err := rows.Scan(&i.ID, &i.Name); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listUsers = `-- name: ListUsers :many
|
|
SELECT u.id, u.name, u.email,
|
|
coalesce(nullif(btrim(u.custom_avatar_url), ''), nullif(btrim(u.avatar_url), ''), '')::text AS avatar_url,
|
|
u.role, u.status, u.joined_at, u.last_login_at,
|
|
coalesce(s.groups, '[]'::jsonb)::jsonb AS supervised_groups
|
|
FROM users u
|
|
LEFT JOIN (
|
|
SELECT gu.user_id, jsonb_agg(jsonb_build_object('id', g.id, 'name', g.name)
|
|
ORDER BY g.name, g.id) AS groups
|
|
FROM group_users gu
|
|
JOIN groups g ON g.id = gu.group_id AND g.deleted_at IS NULL
|
|
WHERE gu.is_supervisor AND gu.removed_at IS NULL
|
|
GROUP BY gu.user_id
|
|
) s ON s.user_id = u.id
|
|
WHERE u.deleted_at IS NULL
|
|
ORDER BY u.joined_at DESC
|
|
`
|
|
|
|
type ListUsersRow struct {
|
|
ID string
|
|
Name string
|
|
Email string
|
|
AvatarUrl string
|
|
Role string
|
|
Status string
|
|
JoinedAt time.Time
|
|
LastLoginAt *time.Time
|
|
SupervisedGroups []byte
|
|
}
|
|
|
|
func (q *Queries) ListUsers(ctx context.Context) ([]ListUsersRow, error) {
|
|
rows, err := q.db.Query(ctx, listUsers)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ListUsersRow{}
|
|
for rows.Next() {
|
|
var i ListUsersRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Email,
|
|
&i.AvatarUrl,
|
|
&i.Role,
|
|
&i.Status,
|
|
&i.JoinedAt,
|
|
&i.LastLoginAt,
|
|
&i.SupervisedGroups,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const lockActiveAdmin = `-- name: LockActiveAdmin :one
|
|
SELECT id FROM users
|
|
WHERE id = $1 AND role = 'admin' AND status = 'active' AND deleted_at IS NULL
|
|
FOR SHARE
|
|
`
|
|
|
|
func (q *Queries) LockActiveAdmin(ctx context.Context, id string) (string, error) {
|
|
row := q.db.QueryRow(ctx, lockActiveAdmin, id)
|
|
err := row.Scan(&id)
|
|
return id, err
|
|
}
|
|
|
|
const lockEmailDelivery = `-- name: LockEmailDelivery :exec
|
|
SELECT pg_advisory_xact_lock(741210)
|
|
`
|
|
|
|
func (q *Queries) LockEmailDelivery(ctx context.Context) error {
|
|
_, err := q.db.Exec(ctx, lockEmailDelivery)
|
|
return err
|
|
}
|
|
|
|
const lockEmailRegistrationSettings = `-- name: LockEmailRegistrationSettings :exec
|
|
SELECT key FROM settings WHERE key='authentication' FOR SHARE
|
|
`
|
|
|
|
func (q *Queries) LockEmailRegistrationSettings(ctx context.Context) error {
|
|
_, err := q.db.Exec(ctx, lockEmailRegistrationSettings)
|
|
return err
|
|
}
|
|
|
|
const lockGrant = `-- name: LockGrant :one
|
|
SELECT g.id, g.user_id, g.client_id, g.scopes, g.audience, g.purpose, g.machine_id, g.key_id, g.revocation_version, g.expires_at, g.created_at, g.revoked_at, g.revoke_reason, g.requested_machine_id, g.requested_key_id, g.restore_approved, u.status AS user_status, u.deleted_at AS user_deleted_at
|
|
FROM oauth_grants g JOIN users u ON u.id = g.user_id
|
|
WHERE g.id = $1 FOR UPDATE OF g
|
|
`
|
|
|
|
type LockGrantRow struct {
|
|
ID string
|
|
UserID string
|
|
ClientID string
|
|
Scopes []string
|
|
Audience string
|
|
Purpose string
|
|
MachineID *string
|
|
KeyID *string
|
|
RevocationVersion int64
|
|
ExpiresAt time.Time
|
|
CreatedAt time.Time
|
|
RevokedAt *time.Time
|
|
RevokeReason *string
|
|
RequestedMachineID *string
|
|
RequestedKeyID *string
|
|
RestoreApproved bool
|
|
UserStatus string
|
|
UserDeletedAt *time.Time
|
|
}
|
|
|
|
func (q *Queries) LockGrant(ctx context.Context, id string) (LockGrantRow, error) {
|
|
row := q.db.QueryRow(ctx, lockGrant, id)
|
|
var i LockGrantRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.UserID,
|
|
&i.ClientID,
|
|
&i.Scopes,
|
|
&i.Audience,
|
|
&i.Purpose,
|
|
&i.MachineID,
|
|
&i.KeyID,
|
|
&i.RevocationVersion,
|
|
&i.ExpiresAt,
|
|
&i.CreatedAt,
|
|
&i.RevokedAt,
|
|
&i.RevokeReason,
|
|
&i.RequestedMachineID,
|
|
&i.RequestedKeyID,
|
|
&i.RestoreApproved,
|
|
&i.UserStatus,
|
|
&i.UserDeletedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const lockInitialAdmin = `-- name: LockInitialAdmin :execresult
|
|
SELECT
|
|
pg_advisory_xact_lock(hashtext('monkeyai:initial-admin'))
|
|
`
|
|
|
|
func (q *Queries) LockInitialAdmin(ctx context.Context) (pgconn.CommandTag, error) {
|
|
return q.db.Exec(ctx, lockInitialAdmin)
|
|
}
|
|
|
|
const lockUserCreationGroups = `-- name: LockUserCreationGroups :exec
|
|
SELECT pg_advisory_xact_lock(741209)
|
|
`
|
|
|
|
// Same advisory lock as group writes, including membership replacement and deletion.
|
|
func (q *Queries) LockUserCreationGroups(ctx context.Context) error {
|
|
_, err := q.db.Exec(ctx, lockUserCreationGroups)
|
|
return err
|
|
}
|
|
|
|
const managedMemberIDs = `-- name: ManagedMemberIDs :many
|
|
WITH RECURSIVE managed(id) AS (
|
|
SELECT g.id FROM groups g JOIN group_users gu ON gu.group_id = g.id
|
|
WHERE gu.user_id = $1::uuid AND gu.is_supervisor
|
|
AND gu.removed_at IS NULL AND g.deleted_at IS NULL
|
|
UNION
|
|
SELECT g.id FROM groups g JOIN managed parent ON g.parent_id = parent.id
|
|
WHERE g.deleted_at IS NULL
|
|
)
|
|
SELECT DISTINCT u.id FROM managed g
|
|
JOIN group_users gu ON gu.group_id = g.id AND gu.removed_at IS NULL
|
|
JOIN users u ON u.id = gu.user_id AND u.deleted_at IS NULL
|
|
`
|
|
|
|
func (q *Queries) ManagedMemberIDs(ctx context.Context, supervisorID string) ([]string, error) {
|
|
rows, err := q.db.Query(ctx, managedMemberIDs, supervisorID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []string{}
|
|
for rows.Next() {
|
|
var id string
|
|
if err := rows.Scan(&id); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, id)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const markUserDeleted = `-- name: MarkUserDeleted :one
|
|
UPDATE users SET deleted_at = now(), updated_at = now(), password_hash = NULL
|
|
WHERE id = $1 AND deleted_at IS NULL
|
|
RETURNING lower(email)::text
|
|
`
|
|
|
|
func (q *Queries) MarkUserDeleted(ctx context.Context, id string) (string, error) {
|
|
row := q.db.QueryRow(ctx, markUserDeleted, id)
|
|
var column_1 string
|
|
err := row.Scan(&column_1)
|
|
return column_1, err
|
|
}
|
|
|
|
const markUserIdentitiesDeleted = `-- name: MarkUserIdentitiesDeleted :exec
|
|
UPDATE user_identities SET deleted_at = now(), updated_at = now()
|
|
WHERE user_id = $1 AND deleted_at IS NULL
|
|
`
|
|
|
|
func (q *Queries) MarkUserIdentitiesDeleted(ctx context.Context, userID string) error {
|
|
_, err := q.db.Exec(ctx, markUserIdentitiesDeleted, userID)
|
|
return err
|
|
}
|
|
|
|
const readyEmailCode = `-- name: ReadyEmailCode :exec
|
|
UPDATE email_codes SET ready = true WHERE email = $1 AND purpose = $2 AND code_hash = $3
|
|
`
|
|
|
|
type ReadyEmailCodeParams struct {
|
|
Email string
|
|
Purpose string
|
|
CodeHash string
|
|
}
|
|
|
|
func (q *Queries) ReadyEmailCode(ctx context.Context, arg ReadyEmailCodeParams) error {
|
|
_, err := q.db.Exec(ctx, readyEmailCode, arg.Email, arg.Purpose, arg.CodeHash)
|
|
return err
|
|
}
|
|
|
|
const recordEmailDelivery = `-- name: RecordEmailDelivery :exec
|
|
INSERT INTO email_code_deliveries (email, ip_hash) VALUES ($1, $2)
|
|
`
|
|
|
|
type RecordEmailDeliveryParams struct {
|
|
Email string
|
|
IpHash string
|
|
}
|
|
|
|
func (q *Queries) RecordEmailDelivery(ctx context.Context, arg RecordEmailDeliveryParams) error {
|
|
_, err := q.db.Exec(ctx, recordEmailDelivery, arg.Email, arg.IpHash)
|
|
return err
|
|
}
|
|
|
|
const redeemAuthorizationCode = `-- name: RedeemAuthorizationCode :execresult
|
|
UPDATE
|
|
oauth_authorization_codes
|
|
SET
|
|
redeemed_at = now()
|
|
WHERE
|
|
id = $1
|
|
AND redeemed_at IS NULL
|
|
AND expires_at > now()
|
|
`
|
|
|
|
func (q *Queries) RedeemAuthorizationCode(ctx context.Context, id string) (pgconn.CommandTag, error) {
|
|
return q.db.Exec(ctx, redeemAuthorizationCode, id)
|
|
}
|
|
|
|
const removeDeletedUserGroups = `-- name: RemoveDeletedUserGroups :exec
|
|
WITH removed AS (
|
|
UPDATE group_users SET removed_at = now()
|
|
WHERE user_id = $1 AND removed_at IS NULL
|
|
RETURNING group_id
|
|
)
|
|
UPDATE groups SET updated_at = now()
|
|
WHERE id IN (SELECT group_id FROM removed) AND deleted_at IS NULL
|
|
`
|
|
|
|
func (q *Queries) RemoveDeletedUserGroups(ctx context.Context, userID string) error {
|
|
_, err := q.db.Exec(ctx, removeDeletedUserGroups, userID)
|
|
return err
|
|
}
|
|
|
|
const resetAdminPassword = `-- name: ResetAdminPassword :one
|
|
UPDATE users SET password_hash = $1, updated_at = now()
|
|
WHERE lower(email) = $2 AND role = 'admin' AND deleted_at IS NULL
|
|
RETURNING id, email
|
|
`
|
|
|
|
type ResetAdminPasswordParams struct {
|
|
PasswordHash *string
|
|
Email string
|
|
}
|
|
|
|
type ResetAdminPasswordRow struct {
|
|
ID string
|
|
Email string
|
|
}
|
|
|
|
func (q *Queries) ResetAdminPassword(ctx context.Context, arg ResetAdminPasswordParams) (ResetAdminPasswordRow, error) {
|
|
row := q.db.QueryRow(ctx, resetAdminPassword, arg.PasswordHash, arg.Email)
|
|
var i ResetAdminPasswordRow
|
|
err := row.Scan(&i.ID, &i.Email)
|
|
return i, err
|
|
}
|
|
|
|
const resetPassword = `-- name: ResetPassword :one
|
|
UPDATE users SET password_hash = $2, updated_at = now()
|
|
WHERE lower(email) = $1 AND deleted_at IS NULL AND status = 'active' RETURNING id
|
|
`
|
|
|
|
type ResetPasswordParams struct {
|
|
Email string
|
|
PasswordHash *string
|
|
}
|
|
|
|
func (q *Queries) ResetPassword(ctx context.Context, arg ResetPasswordParams) (string, error) {
|
|
row := q.db.QueryRow(ctx, resetPassword, arg.Email, arg.PasswordHash)
|
|
var id string
|
|
err := row.Scan(&id)
|
|
return id, err
|
|
}
|
|
|
|
const resetUserPassword = `-- name: ResetUserPassword :one
|
|
UPDATE users SET password_hash = $2, updated_at = now()
|
|
WHERE id = $1 AND deleted_at IS NULL RETURNING id
|
|
`
|
|
|
|
type ResetUserPasswordParams struct {
|
|
ID string
|
|
PasswordHash *string
|
|
}
|
|
|
|
func (q *Queries) ResetUserPassword(ctx context.Context, arg ResetUserPasswordParams) (string, error) {
|
|
row := q.db.QueryRow(ctx, resetUserPassword, arg.ID, arg.PasswordHash)
|
|
var id string
|
|
err := row.Scan(&id)
|
|
return id, err
|
|
}
|
|
|
|
const revokeBrowserSession = `-- name: RevokeBrowserSession :execresult
|
|
UPDATE
|
|
browser_sessions
|
|
SET
|
|
revoked_at = now()
|
|
WHERE
|
|
token_hash = $1
|
|
AND revoked_at IS NULL
|
|
`
|
|
|
|
func (q *Queries) RevokeBrowserSession(ctx context.Context, tokenHash string) (pgconn.CommandTag, error) {
|
|
return q.db.Exec(ctx, revokeBrowserSession, tokenHash)
|
|
}
|
|
|
|
const revokeGrant = `-- name: RevokeGrant :exec
|
|
UPDATE oauth_grants SET revoked_at = now(), revoke_reason = $2,
|
|
revocation_version = revocation_version + 1
|
|
WHERE id = $1 AND revoked_at IS NULL
|
|
`
|
|
|
|
type RevokeGrantParams struct {
|
|
ID string
|
|
RevokeReason *string
|
|
}
|
|
|
|
func (q *Queries) RevokeGrant(ctx context.Context, arg RevokeGrantParams) error {
|
|
_, err := q.db.Exec(ctx, revokeGrant, arg.ID, arg.RevokeReason)
|
|
return err
|
|
}
|
|
|
|
const revokeGrantTokens = `-- name: RevokeGrantTokens :exec
|
|
UPDATE oauth_tokens SET revoked_at = coalesce(revoked_at, now()) WHERE grant_id = $1
|
|
`
|
|
|
|
func (q *Queries) RevokeGrantTokens(ctx context.Context, grantID string) error {
|
|
_, err := q.db.Exec(ctx, revokeGrantTokens, grantID)
|
|
return err
|
|
}
|
|
|
|
const revokeToken = `-- name: RevokeToken :execresult
|
|
UPDATE
|
|
oauth_tokens
|
|
SET
|
|
revoked_at = now()
|
|
WHERE
|
|
client_id = $2
|
|
AND revoked_at IS NULL
|
|
AND (access_token_hash = $1
|
|
OR refresh_token_hash = $1)
|
|
`
|
|
|
|
type RevokeTokenParams struct {
|
|
AccessTokenHash string
|
|
ClientID string
|
|
}
|
|
|
|
func (q *Queries) RevokeToken(ctx context.Context, arg RevokeTokenParams) (pgconn.CommandTag, error) {
|
|
return q.db.Exec(ctx, revokeToken, arg.AccessTokenHash, arg.ClientID)
|
|
}
|
|
|
|
const revokeUserCodes = `-- name: RevokeUserCodes :exec
|
|
UPDATE oauth_authorization_codes SET redeemed_at = now() WHERE user_id = $1 AND redeemed_at IS NULL
|
|
`
|
|
|
|
func (q *Queries) RevokeUserCodes(ctx context.Context, userID string) error {
|
|
_, err := q.db.Exec(ctx, revokeUserCodes, userID)
|
|
return err
|
|
}
|
|
|
|
const revokeUserSessions = `-- name: RevokeUserSessions :exec
|
|
UPDATE browser_sessions SET revoked_at = now() WHERE user_id = $1 AND revoked_at IS NULL
|
|
`
|
|
|
|
func (q *Queries) RevokeUserSessions(ctx context.Context, userID string) error {
|
|
_, err := q.db.Exec(ctx, revokeUserSessions, userID)
|
|
return err
|
|
}
|
|
|
|
const revokeUserTokens = `-- name: RevokeUserTokens :exec
|
|
WITH grants AS (
|
|
UPDATE oauth_grants AS g SET revoked_at = now(), revoke_reason = 'user_revocation',
|
|
revocation_version = g.revocation_version + 1
|
|
WHERE g.user_id = $1 AND g.revoked_at IS NULL
|
|
)
|
|
UPDATE oauth_tokens AS t SET revoked_at = now() WHERE t.user_id = $1 AND t.revoked_at IS NULL
|
|
`
|
|
|
|
func (q *Queries) RevokeUserTokens(ctx context.Context, userID string) error {
|
|
_, err := q.db.Exec(ctx, revokeUserTokens, userID)
|
|
return err
|
|
}
|
|
|
|
const rotateToken = `-- name: RotateToken :execrows
|
|
UPDATE oauth_tokens SET rotated_at = now(), revoked_at = now()
|
|
WHERE id = $1 AND rotated_at IS NULL AND revoked_at IS NULL AND refresh_expires_at > now()
|
|
`
|
|
|
|
func (q *Queries) RotateToken(ctx context.Context, id string) (int64, error) {
|
|
result, err := q.db.Exec(ctx, rotateToken, id)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const saveEmailCode = `-- name: SaveEmailCode :exec
|
|
INSERT INTO email_codes (email, purpose, code_hash, expires_at) VALUES ($1, $2, $3, $4)
|
|
ON CONFLICT (email, purpose) DO UPDATE SET code_hash = EXCLUDED.code_hash, expires_at = EXCLUDED.expires_at, attempts = 0, ready = false
|
|
`
|
|
|
|
type SaveEmailCodeParams struct {
|
|
Email string
|
|
Purpose string
|
|
CodeHash string
|
|
ExpiresAt time.Time
|
|
}
|
|
|
|
func (q *Queries) SaveEmailCode(ctx context.Context, arg SaveEmailCodeParams) error {
|
|
_, err := q.db.Exec(ctx, saveEmailCode,
|
|
arg.Email,
|
|
arg.Purpose,
|
|
arg.CodeHash,
|
|
arg.ExpiresAt,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const searchAdminUserCandidates = `-- name: SearchAdminUserCandidates :many
|
|
SELECT jsonb_build_object('id',u.id,'name',u.name,'email',u.email,
|
|
'avatar_url',coalesce(nullif(btrim(u.custom_avatar_url),''), nullif(btrim(u.avatar_url),'')),'deleted',u.deleted_at IS NOT NULL)
|
|
FROM users u
|
|
WHERE ($1::uuid[] IS NULL OR u.id = ANY($1::uuid[]))
|
|
AND ($2::boolean OR u.deleted_at IS NULL)
|
|
AND ($3::text = '' OR u.id = nullif($3::text,'')::uuid)
|
|
AND ($4::text = '' OR strpos(lower(u.name),lower($4::text))>0
|
|
OR strpos(lower(u.email),lower($4::text))>0
|
|
OR strpos(u.id::text,lower($4::text))>0)
|
|
ORDER BY lower(u.name),lower(u.email),u.id
|
|
LIMIT $5
|
|
`
|
|
|
|
type SearchAdminUserCandidatesParams struct {
|
|
VisibleUsers []string
|
|
IncludeDeleted bool
|
|
UserID string
|
|
Query string
|
|
ResultLimit int32
|
|
}
|
|
|
|
func (q *Queries) SearchAdminUserCandidates(ctx context.Context, arg SearchAdminUserCandidatesParams) ([][]byte, error) {
|
|
rows, err := q.db.Query(ctx, searchAdminUserCandidates,
|
|
arg.VisibleUsers,
|
|
arg.IncludeDeleted,
|
|
arg.UserID,
|
|
arg.Query,
|
|
arg.ResultLimit,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := [][]byte{}
|
|
for rows.Next() {
|
|
var jsonb_build_object []byte
|
|
if err := rows.Scan(&jsonb_build_object); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, jsonb_build_object)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const searchUsers = `-- name: SearchUsers :many
|
|
SELECT
|
|
id,
|
|
name,
|
|
email
|
|
FROM
|
|
users
|
|
WHERE
|
|
status = 'active'
|
|
AND deleted_at IS NULL
|
|
AND id <> $1
|
|
AND (strpos(lower(name), lower($2)) > 0
|
|
OR strpos(lower(email), lower($2)) > 0)
|
|
ORDER BY
|
|
lower(name),
|
|
id
|
|
LIMIT $3
|
|
`
|
|
|
|
type SearchUsersParams struct {
|
|
ID string
|
|
Lower string
|
|
Limit int32
|
|
}
|
|
|
|
type SearchUsersRow struct {
|
|
ID string
|
|
Name string
|
|
Email string
|
|
}
|
|
|
|
func (q *Queries) SearchUsers(ctx context.Context, arg SearchUsersParams) ([]SearchUsersRow, error) {
|
|
rows, err := q.db.Query(ctx, searchUsers, arg.ID, arg.Lower, arg.Limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []SearchUsersRow{}
|
|
for rows.Next() {
|
|
var i SearchUsersRow
|
|
if err := rows.Scan(&i.ID, &i.Name, &i.Email); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const touchLogin = `-- name: TouchLogin :execresult
|
|
UPDATE
|
|
users
|
|
SET
|
|
last_login_at = now(),
|
|
updated_at = now()
|
|
WHERE
|
|
id = $1
|
|
`
|
|
|
|
func (q *Queries) TouchLogin(ctx context.Context, id string) (pgconn.CommandTag, error) {
|
|
return q.db.Exec(ctx, touchLogin, id)
|
|
}
|
|
|
|
const touchUserCreationGroups = `-- name: TouchUserCreationGroups :exec
|
|
UPDATE groups SET updated_at = now() WHERE id = ANY ($1::uuid[])
|
|
`
|
|
|
|
func (q *Queries) TouchUserCreationGroups(ctx context.Context, dollar_1 []string) error {
|
|
_, err := q.db.Exec(ctx, touchUserCreationGroups, dollar_1)
|
|
return err
|
|
}
|
|
|
|
const updateBaizhiyunEmail = `-- name: UpdateBaizhiyunEmail :exec
|
|
WITH identity AS (
|
|
UPDATE user_identities
|
|
SET email = $1::text
|
|
WHERE user_id = $2
|
|
AND provider = 'baizhiyun'
|
|
AND issuer = $3
|
|
AND provider_subject = $4
|
|
AND deleted_at IS NULL
|
|
RETURNING user_id, provider_subject
|
|
)
|
|
UPDATE users u
|
|
SET email = $1::text, updated_at = now()
|
|
FROM identity i
|
|
WHERE u.id = i.user_id
|
|
AND u.email = i.provider_subject || '@baizhiyun.oauth.local'
|
|
AND NOT EXISTS (
|
|
SELECT 1 FROM users existing
|
|
WHERE lower(existing.email) = lower($1::text)
|
|
AND existing.deleted_at IS NULL
|
|
)
|
|
`
|
|
|
|
type UpdateBaizhiyunEmailParams struct {
|
|
Email string
|
|
UserID string
|
|
Issuer string
|
|
ProviderSubject string
|
|
}
|
|
|
|
func (q *Queries) UpdateBaizhiyunEmail(ctx context.Context, arg UpdateBaizhiyunEmailParams) error {
|
|
_, err := q.db.Exec(ctx, updateBaizhiyunEmail,
|
|
arg.Email,
|
|
arg.UserID,
|
|
arg.Issuer,
|
|
arg.ProviderSubject,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const updateIdentityUser = `-- name: UpdateIdentityUser :one
|
|
UPDATE
|
|
users
|
|
SET
|
|
name = $1,
|
|
avatar_url = NULLIF ($2::text, ''),
|
|
last_login_at = now(),
|
|
updated_at = now()
|
|
WHERE
|
|
id = $3
|
|
RETURNING
|
|
id,
|
|
name,
|
|
email,
|
|
coalesce(nullif(btrim(custom_avatar_url), ''), nullif(btrim(avatar_url), ''), '')::text AS avatar_url,
|
|
ROLE,
|
|
status,
|
|
joined_at,
|
|
last_login_at
|
|
`
|
|
|
|
type UpdateIdentityUserParams struct {
|
|
Name string
|
|
AvatarUrl string
|
|
ID string
|
|
}
|
|
|
|
type UpdateIdentityUserRow struct {
|
|
ID string
|
|
Name string
|
|
Email string
|
|
AvatarUrl string
|
|
Role string
|
|
Status string
|
|
JoinedAt time.Time
|
|
LastLoginAt *time.Time
|
|
}
|
|
|
|
func (q *Queries) UpdateIdentityUser(ctx context.Context, arg UpdateIdentityUserParams) (UpdateIdentityUserRow, error) {
|
|
row := q.db.QueryRow(ctx, updateIdentityUser, arg.Name, arg.AvatarUrl, arg.ID)
|
|
var i UpdateIdentityUserRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Email,
|
|
&i.AvatarUrl,
|
|
&i.Role,
|
|
&i.Status,
|
|
&i.JoinedAt,
|
|
&i.LastLoginAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const updateUser = `-- name: UpdateUser :one
|
|
UPDATE
|
|
users
|
|
SET
|
|
name = $1,
|
|
ROLE = $2,
|
|
status = $3,
|
|
disabled_at = $4,
|
|
password_hash = CASE WHEN $5::text = '' THEN
|
|
password_hash
|
|
ELSE
|
|
$5::text
|
|
END,
|
|
updated_at = now()
|
|
WHERE
|
|
id = $6
|
|
AND deleted_at IS NULL
|
|
RETURNING
|
|
id,
|
|
name,
|
|
email,
|
|
coalesce(nullif(btrim(custom_avatar_url), ''), nullif(btrim(avatar_url), ''), '')::text AS avatar_url,
|
|
ROLE,
|
|
status,
|
|
joined_at,
|
|
last_login_at
|
|
`
|
|
|
|
type UpdateUserParams struct {
|
|
Name string
|
|
Role string
|
|
Status string
|
|
DisabledAt *time.Time
|
|
PasswordHash string
|
|
ID string
|
|
}
|
|
|
|
type UpdateUserRow struct {
|
|
ID string
|
|
Name string
|
|
Email string
|
|
AvatarUrl string
|
|
Role string
|
|
Status string
|
|
JoinedAt time.Time
|
|
LastLoginAt *time.Time
|
|
}
|
|
|
|
func (q *Queries) UpdateUser(ctx context.Context, arg UpdateUserParams) (UpdateUserRow, error) {
|
|
row := q.db.QueryRow(ctx, updateUser,
|
|
arg.Name,
|
|
arg.Role,
|
|
arg.Status,
|
|
arg.DisabledAt,
|
|
arg.PasswordHash,
|
|
arg.ID,
|
|
)
|
|
var i UpdateUserRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Email,
|
|
&i.AvatarUrl,
|
|
&i.Role,
|
|
&i.Status,
|
|
&i.JoinedAt,
|
|
&i.LastLoginAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const upsertIdentity = `-- name: UpsertIdentity :execresult
|
|
INSERT INTO user_identities (user_id, provider, issuer, provider_subject, username, email, avatar_url)
|
|
VALUES ($1, $2, $3, $4,
|
|
NULLIF ($5::text, ''), NULLIF($6::text,''), NULLIF ($7::text, ''))
|
|
`
|
|
|
|
type UpsertIdentityParams struct {
|
|
UserID string
|
|
Provider string
|
|
Issuer string
|
|
ProviderSubject string
|
|
ProviderUsername string
|
|
ProviderEmail string
|
|
ProviderAvatarUrl string
|
|
}
|
|
|
|
func (q *Queries) UpsertIdentity(ctx context.Context, arg UpsertIdentityParams) (pgconn.CommandTag, error) {
|
|
return q.db.Exec(ctx, upsertIdentity,
|
|
arg.UserID,
|
|
arg.Provider,
|
|
arg.Issuer,
|
|
arg.ProviderSubject,
|
|
arg.ProviderUsername,
|
|
arg.ProviderEmail,
|
|
arg.ProviderAvatarUrl,
|
|
)
|
|
}
|