1622 lines
36 KiB
Go
1622 lines
36 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: query.sql
|
|
|
|
package database
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
|
|
"mediarr/ident"
|
|
)
|
|
|
|
const createAccount = `-- name: CreateAccount :one
|
|
|
|
INSERT INTO account (
|
|
principal_id,
|
|
login,
|
|
login_key,
|
|
password_hash,
|
|
password_changed_at,
|
|
must_change_password,
|
|
state,
|
|
failed_attempts,
|
|
locked_until,
|
|
last_login_at,
|
|
created_at
|
|
) VALUES (
|
|
?1,
|
|
?2,
|
|
?3,
|
|
?4,
|
|
?5,
|
|
?6,
|
|
?7,
|
|
?8,
|
|
?9,
|
|
?10,
|
|
?11
|
|
)
|
|
RETURNING principal_id, login, login_key, password_hash, password_changed_at, must_change_password, state, failed_attempts, locked_until, last_login_at, created_at
|
|
`
|
|
|
|
type CreateAccountParams struct {
|
|
PrincipalID ident.UUID
|
|
Login string
|
|
LoginKey string
|
|
PasswordHash sql.NullString
|
|
PasswordChangedAt sql.NullInt64
|
|
MustChangePassword int64
|
|
State string
|
|
FailedAttempts int64
|
|
LockedUntil sql.NullInt64
|
|
LastLoginAt sql.NullInt64
|
|
CreatedAt int64
|
|
}
|
|
|
|
// ============================================================================
|
|
// account
|
|
// ============================================================================
|
|
func (q *Queries) CreateAccount(ctx context.Context, arg CreateAccountParams) (Account, error) {
|
|
row := q.db.QueryRowContext(ctx, createAccount,
|
|
arg.PrincipalID,
|
|
arg.Login,
|
|
arg.LoginKey,
|
|
arg.PasswordHash,
|
|
arg.PasswordChangedAt,
|
|
arg.MustChangePassword,
|
|
arg.State,
|
|
arg.FailedAttempts,
|
|
arg.LockedUntil,
|
|
arg.LastLoginAt,
|
|
arg.CreatedAt,
|
|
)
|
|
var i Account
|
|
err := row.Scan(
|
|
&i.PrincipalID,
|
|
&i.Login,
|
|
&i.LoginKey,
|
|
&i.PasswordHash,
|
|
&i.PasswordChangedAt,
|
|
&i.MustChangePassword,
|
|
&i.State,
|
|
&i.FailedAttempts,
|
|
&i.LockedUntil,
|
|
&i.LastLoginAt,
|
|
&i.CreatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const createMembership = `-- name: CreateMembership :one
|
|
|
|
INSERT INTO membership (
|
|
group_id,
|
|
member_id,
|
|
role,
|
|
created_at
|
|
) VALUES (
|
|
?1,
|
|
?2,
|
|
?3,
|
|
?4
|
|
)
|
|
RETURNING group_id, member_id, role, created_at
|
|
`
|
|
|
|
type CreateMembershipParams struct {
|
|
GroupID ident.UUID
|
|
MemberID ident.UUID
|
|
Role string
|
|
CreatedAt int64
|
|
}
|
|
|
|
// ============================================================================
|
|
// membership
|
|
// ============================================================================
|
|
func (q *Queries) CreateMembership(ctx context.Context, arg CreateMembershipParams) (Membership, error) {
|
|
row := q.db.QueryRowContext(ctx, createMembership,
|
|
arg.GroupID,
|
|
arg.MemberID,
|
|
arg.Role,
|
|
arg.CreatedAt,
|
|
)
|
|
var i Membership
|
|
err := row.Scan(
|
|
&i.GroupID,
|
|
&i.MemberID,
|
|
&i.Role,
|
|
&i.CreatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const createPrincipal = `-- name: CreatePrincipal :one
|
|
|
|
INSERT INTO principal (
|
|
id,
|
|
kind,
|
|
name,
|
|
public_key,
|
|
status,
|
|
successor_id,
|
|
created_at,
|
|
disabled_at
|
|
) VALUES (
|
|
?1,
|
|
?2,
|
|
?3,
|
|
?4,
|
|
?5,
|
|
?6,
|
|
?7,
|
|
?8
|
|
)
|
|
RETURNING id, kind, name, public_key, status, successor_id, created_at, disabled_at
|
|
`
|
|
|
|
type CreatePrincipalParams struct {
|
|
ID ident.UUID
|
|
Kind string
|
|
Name string
|
|
PublicKey []byte
|
|
Status string
|
|
SuccessorID *ident.UUID
|
|
CreatedAt int64
|
|
DisabledAt sql.NullInt64
|
|
}
|
|
|
|
// ============================================================================
|
|
// principal
|
|
// ============================================================================
|
|
func (q *Queries) CreatePrincipal(ctx context.Context, arg CreatePrincipalParams) (Principal, error) {
|
|
row := q.db.QueryRowContext(ctx, createPrincipal,
|
|
arg.ID,
|
|
arg.Kind,
|
|
arg.Name,
|
|
arg.PublicKey,
|
|
arg.Status,
|
|
arg.SuccessorID,
|
|
arg.CreatedAt,
|
|
arg.DisabledAt,
|
|
)
|
|
var i Principal
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Kind,
|
|
&i.Name,
|
|
&i.PublicKey,
|
|
&i.Status,
|
|
&i.SuccessorID,
|
|
&i.CreatedAt,
|
|
&i.DisabledAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const createSession = `-- name: CreateSession :one
|
|
|
|
INSERT INTO session (
|
|
id,
|
|
token_hash,
|
|
principal_id,
|
|
kind,
|
|
label,
|
|
user_agent,
|
|
ip_prefix,
|
|
created_at,
|
|
last_seen_at,
|
|
expires_at,
|
|
idle_expires_at
|
|
) VALUES (
|
|
?1,
|
|
?2,
|
|
?3,
|
|
?4,
|
|
?5,
|
|
?6,
|
|
?7,
|
|
?8,
|
|
?9,
|
|
?10,
|
|
?11
|
|
)
|
|
RETURNING id, token_hash, principal_id, kind, label, user_agent, ip_prefix, created_at, last_seen_at, expires_at, idle_expires_at, revoked_at
|
|
`
|
|
|
|
type CreateSessionParams struct {
|
|
ID ident.UUID
|
|
TokenHash []byte
|
|
PrincipalID ident.UUID
|
|
Kind string
|
|
Label sql.NullString
|
|
UserAgent sql.NullString
|
|
IpPrefix sql.NullString
|
|
CreatedAt int64
|
|
LastSeenAt int64
|
|
ExpiresAt int64
|
|
IdleExpiresAt int64
|
|
}
|
|
|
|
// ============================================================================
|
|
// session
|
|
// ============================================================================
|
|
func (q *Queries) CreateSession(ctx context.Context, arg CreateSessionParams) (Session, error) {
|
|
row := q.db.QueryRowContext(ctx, createSession,
|
|
arg.ID,
|
|
arg.TokenHash,
|
|
arg.PrincipalID,
|
|
arg.Kind,
|
|
arg.Label,
|
|
arg.UserAgent,
|
|
arg.IpPrefix,
|
|
arg.CreatedAt,
|
|
arg.LastSeenAt,
|
|
arg.ExpiresAt,
|
|
arg.IdleExpiresAt,
|
|
)
|
|
var i Session
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.TokenHash,
|
|
&i.PrincipalID,
|
|
&i.Kind,
|
|
&i.Label,
|
|
&i.UserAgent,
|
|
&i.IpPrefix,
|
|
&i.CreatedAt,
|
|
&i.LastSeenAt,
|
|
&i.ExpiresAt,
|
|
&i.IdleExpiresAt,
|
|
&i.RevokedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const deleteAccount = `-- name: DeleteAccount :exec
|
|
DELETE FROM account
|
|
WHERE principal_id = ?1
|
|
`
|
|
|
|
func (q *Queries) DeleteAccount(ctx context.Context, principalID ident.UUID) error {
|
|
_, err := q.db.ExecContext(ctx, deleteAccount, principalID)
|
|
return err
|
|
}
|
|
|
|
const deleteExpiredSessions = `-- name: DeleteExpiredSessions :execrows
|
|
DELETE FROM session
|
|
WHERE expires_at <= ?1
|
|
OR idle_expires_at <= ?1
|
|
`
|
|
|
|
func (q *Queries) DeleteExpiredSessions(ctx context.Context, now int64) (int64, error) {
|
|
result, err := q.db.ExecContext(ctx, deleteExpiredSessions, now)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected()
|
|
}
|
|
|
|
const deleteMembership = `-- name: DeleteMembership :exec
|
|
DELETE FROM membership
|
|
WHERE group_id = ?1
|
|
AND member_id = ?2
|
|
`
|
|
|
|
type DeleteMembershipParams struct {
|
|
GroupID ident.UUID
|
|
MemberID ident.UUID
|
|
}
|
|
|
|
func (q *Queries) DeleteMembership(ctx context.Context, arg DeleteMembershipParams) error {
|
|
_, err := q.db.ExecContext(ctx, deleteMembership, arg.GroupID, arg.MemberID)
|
|
return err
|
|
}
|
|
|
|
const deletePrincipal = `-- name: DeletePrincipal :exec
|
|
DELETE FROM principal
|
|
WHERE id = ?1
|
|
`
|
|
|
|
func (q *Queries) DeletePrincipal(ctx context.Context, id ident.UUID) error {
|
|
_, err := q.db.ExecContext(ctx, deletePrincipal, id)
|
|
return err
|
|
}
|
|
|
|
const getAccount = `-- name: GetAccount :one
|
|
SELECT principal_id, login, login_key, password_hash, password_changed_at, must_change_password, state, failed_attempts, locked_until, last_login_at, created_at
|
|
FROM account
|
|
WHERE principal_id = ?1
|
|
`
|
|
|
|
func (q *Queries) GetAccount(ctx context.Context, principalID ident.UUID) (Account, error) {
|
|
row := q.db.QueryRowContext(ctx, getAccount, principalID)
|
|
var i Account
|
|
err := row.Scan(
|
|
&i.PrincipalID,
|
|
&i.Login,
|
|
&i.LoginKey,
|
|
&i.PasswordHash,
|
|
&i.PasswordChangedAt,
|
|
&i.MustChangePassword,
|
|
&i.State,
|
|
&i.FailedAttempts,
|
|
&i.LockedUntil,
|
|
&i.LastLoginAt,
|
|
&i.CreatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getAccountByLoginKey = `-- name: GetAccountByLoginKey :one
|
|
SELECT principal_id, login, login_key, password_hash, password_changed_at, must_change_password, state, failed_attempts, locked_until, last_login_at, created_at
|
|
FROM account
|
|
WHERE login_key = ?1
|
|
`
|
|
|
|
func (q *Queries) GetAccountByLoginKey(ctx context.Context, loginKey string) (Account, error) {
|
|
row := q.db.QueryRowContext(ctx, getAccountByLoginKey, loginKey)
|
|
var i Account
|
|
err := row.Scan(
|
|
&i.PrincipalID,
|
|
&i.Login,
|
|
&i.LoginKey,
|
|
&i.PasswordHash,
|
|
&i.PasswordChangedAt,
|
|
&i.MustChangePassword,
|
|
&i.State,
|
|
&i.FailedAttempts,
|
|
&i.LockedUntil,
|
|
&i.LastLoginAt,
|
|
&i.CreatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getClusterPrincipal = `-- name: GetClusterPrincipal :one
|
|
SELECT id, kind, name, public_key, status, successor_id, created_at, disabled_at
|
|
FROM principal
|
|
WHERE kind = 'cluster'
|
|
LIMIT 1
|
|
`
|
|
|
|
func (q *Queries) GetClusterPrincipal(ctx context.Context) (Principal, error) {
|
|
row := q.db.QueryRowContext(ctx, getClusterPrincipal)
|
|
var i Principal
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Kind,
|
|
&i.Name,
|
|
&i.PublicKey,
|
|
&i.Status,
|
|
&i.SuccessorID,
|
|
&i.CreatedAt,
|
|
&i.DisabledAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getDocument = `-- name: GetDocument :one
|
|
|
|
SELECT id, kind, owner_id, created_by, parent_id, name, name_key, path, depth, position, head_version_id, metadata, policy_id, visibility, acl_inherit, rev, created_at, updated_at, deleted_at, deleted_by, trash_batch
|
|
FROM document
|
|
WHERE id = ?1
|
|
`
|
|
|
|
// ============================================================================
|
|
// document
|
|
// ============================================================================
|
|
func (q *Queries) GetDocument(ctx context.Context, id ident.UUID) (Document, error) {
|
|
row := q.db.QueryRowContext(ctx, getDocument, id)
|
|
var i Document
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Kind,
|
|
&i.OwnerID,
|
|
&i.CreatedBy,
|
|
&i.ParentID,
|
|
&i.Name,
|
|
&i.NameKey,
|
|
&i.Path,
|
|
&i.Depth,
|
|
&i.Position,
|
|
&i.HeadVersionID,
|
|
&i.Metadata,
|
|
&i.PolicyID,
|
|
&i.Visibility,
|
|
&i.AclInherit,
|
|
&i.Rev,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.DeletedAt,
|
|
&i.DeletedBy,
|
|
&i.TrashBatch,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getMembership = `-- name: GetMembership :one
|
|
SELECT group_id, member_id, role, created_at
|
|
FROM membership
|
|
WHERE group_id = ?1
|
|
AND member_id = ?2
|
|
`
|
|
|
|
type GetMembershipParams struct {
|
|
GroupID ident.UUID
|
|
MemberID ident.UUID
|
|
}
|
|
|
|
func (q *Queries) GetMembership(ctx context.Context, arg GetMembershipParams) (Membership, error) {
|
|
row := q.db.QueryRowContext(ctx, getMembership, arg.GroupID, arg.MemberID)
|
|
var i Membership
|
|
err := row.Scan(
|
|
&i.GroupID,
|
|
&i.MemberID,
|
|
&i.Role,
|
|
&i.CreatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getPrincipal = `-- name: GetPrincipal :one
|
|
SELECT id, kind, name, public_key, status, successor_id, created_at, disabled_at
|
|
FROM principal
|
|
WHERE id = ?1
|
|
`
|
|
|
|
func (q *Queries) GetPrincipal(ctx context.Context, id ident.UUID) (Principal, error) {
|
|
row := q.db.QueryRowContext(ctx, getPrincipal, id)
|
|
var i Principal
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Kind,
|
|
&i.Name,
|
|
&i.PublicKey,
|
|
&i.Status,
|
|
&i.SuccessorID,
|
|
&i.CreatedAt,
|
|
&i.DisabledAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getPrincipalByName = `-- name: GetPrincipalByName :one
|
|
SELECT id, kind, name, public_key, status, successor_id, created_at, disabled_at
|
|
FROM principal
|
|
WHERE name = ?1
|
|
`
|
|
|
|
func (q *Queries) GetPrincipalByName(ctx context.Context, name string) (Principal, error) {
|
|
row := q.db.QueryRowContext(ctx, getPrincipalByName, name)
|
|
var i Principal
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Kind,
|
|
&i.Name,
|
|
&i.PublicKey,
|
|
&i.Status,
|
|
&i.SuccessorID,
|
|
&i.CreatedAt,
|
|
&i.DisabledAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getPrincipalByNameAndKind = `-- name: GetPrincipalByNameAndKind :one
|
|
SELECT id, kind, name, public_key, status, successor_id, created_at, disabled_at
|
|
FROM principal
|
|
WHERE kind = ?1
|
|
AND name = ?2
|
|
`
|
|
|
|
type GetPrincipalByNameAndKindParams struct {
|
|
Kind string
|
|
Name string
|
|
}
|
|
|
|
func (q *Queries) GetPrincipalByNameAndKind(ctx context.Context, arg GetPrincipalByNameAndKindParams) (Principal, error) {
|
|
row := q.db.QueryRowContext(ctx, getPrincipalByNameAndKind, arg.Kind, arg.Name)
|
|
var i Principal
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Kind,
|
|
&i.Name,
|
|
&i.PublicKey,
|
|
&i.Status,
|
|
&i.SuccessorID,
|
|
&i.CreatedAt,
|
|
&i.DisabledAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getSession = `-- name: GetSession :one
|
|
SELECT id, token_hash, principal_id, kind, label, user_agent, ip_prefix, created_at, last_seen_at, expires_at, idle_expires_at, revoked_at
|
|
FROM session
|
|
WHERE id = ?1
|
|
`
|
|
|
|
func (q *Queries) GetSession(ctx context.Context, id ident.UUID) (Session, error) {
|
|
row := q.db.QueryRowContext(ctx, getSession, id)
|
|
var i Session
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.TokenHash,
|
|
&i.PrincipalID,
|
|
&i.Kind,
|
|
&i.Label,
|
|
&i.UserAgent,
|
|
&i.IpPrefix,
|
|
&i.CreatedAt,
|
|
&i.LastSeenAt,
|
|
&i.ExpiresAt,
|
|
&i.IdleExpiresAt,
|
|
&i.RevokedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getSessionByTokenHash = `-- name: GetSessionByTokenHash :one
|
|
SELECT id, token_hash, principal_id, kind, label, user_agent, ip_prefix, created_at, last_seen_at, expires_at, idle_expires_at, revoked_at
|
|
FROM session
|
|
WHERE token_hash = ?1
|
|
`
|
|
|
|
func (q *Queries) GetSessionByTokenHash(ctx context.Context, tokenHash []byte) (Session, error) {
|
|
row := q.db.QueryRowContext(ctx, getSessionByTokenHash, tokenHash)
|
|
var i Session
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.TokenHash,
|
|
&i.PrincipalID,
|
|
&i.Kind,
|
|
&i.Label,
|
|
&i.UserAgent,
|
|
&i.IpPrefix,
|
|
&i.CreatedAt,
|
|
&i.LastSeenAt,
|
|
&i.ExpiresAt,
|
|
&i.IdleExpiresAt,
|
|
&i.RevokedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const incrementFailedAttempts = `-- name: IncrementFailedAttempts :one
|
|
UPDATE account
|
|
SET failed_attempts = failed_attempts + 1
|
|
WHERE principal_id = ?1
|
|
RETURNING principal_id, login, login_key, password_hash, password_changed_at, must_change_password, state, failed_attempts, locked_until, last_login_at, created_at
|
|
`
|
|
|
|
func (q *Queries) IncrementFailedAttempts(ctx context.Context, principalID ident.UUID) (Account, error) {
|
|
row := q.db.QueryRowContext(ctx, incrementFailedAttempts, principalID)
|
|
var i Account
|
|
err := row.Scan(
|
|
&i.PrincipalID,
|
|
&i.Login,
|
|
&i.LoginKey,
|
|
&i.PasswordHash,
|
|
&i.PasswordChangedAt,
|
|
&i.MustChangePassword,
|
|
&i.State,
|
|
&i.FailedAttempts,
|
|
&i.LockedUntil,
|
|
&i.LastLoginAt,
|
|
&i.CreatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const listActiveACLEntriesForDocument = `-- name: ListActiveACLEntriesForDocument :many
|
|
SELECT id, document_id, subject_id, permissions, propagate, expires_at, granted_by, created_at, revoked_at
|
|
FROM acl_entry
|
|
WHERE document_id = ?1
|
|
AND revoked_at IS NULL
|
|
AND (expires_at IS NULL OR expires_at > CAST(?2 AS INTEGER))
|
|
ORDER BY created_at
|
|
`
|
|
|
|
type ListActiveACLEntriesForDocumentParams struct {
|
|
DocumentID ident.UUID
|
|
Now int64
|
|
}
|
|
|
|
func (q *Queries) ListActiveACLEntriesForDocument(ctx context.Context, arg ListActiveACLEntriesForDocumentParams) ([]AclEntry, error) {
|
|
rows, err := q.db.QueryContext(ctx, listActiveACLEntriesForDocument, arg.DocumentID, arg.Now)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []AclEntry
|
|
for rows.Next() {
|
|
var i AclEntry
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.DocumentID,
|
|
&i.SubjectID,
|
|
&i.Permissions,
|
|
&i.Propagate,
|
|
&i.ExpiresAt,
|
|
&i.GrantedBy,
|
|
&i.CreatedAt,
|
|
&i.RevokedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listActivePrincipals = `-- name: ListActivePrincipals :many
|
|
SELECT id, kind, name, public_key, status, successor_id, created_at, disabled_at
|
|
FROM principal
|
|
WHERE status = 'active'
|
|
ORDER BY name
|
|
`
|
|
|
|
func (q *Queries) ListActivePrincipals(ctx context.Context) ([]Principal, error) {
|
|
rows, err := q.db.QueryContext(ctx, listActivePrincipals)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Principal
|
|
for rows.Next() {
|
|
var i Principal
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Kind,
|
|
&i.Name,
|
|
&i.PublicKey,
|
|
&i.Status,
|
|
&i.SuccessorID,
|
|
&i.CreatedAt,
|
|
&i.DisabledAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listGroupMembers = `-- name: ListGroupMembers :many
|
|
SELECT p.id, p.kind, p.name, p.public_key, p.status, p.successor_id, p.created_at, p.disabled_at
|
|
FROM membership m
|
|
JOIN principal p ON p.id = m.member_id
|
|
WHERE m.group_id = ?1
|
|
ORDER BY p.name
|
|
`
|
|
|
|
func (q *Queries) ListGroupMembers(ctx context.Context, groupID ident.UUID) ([]Principal, error) {
|
|
rows, err := q.db.QueryContext(ctx, listGroupMembers, groupID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Principal
|
|
for rows.Next() {
|
|
var i Principal
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Kind,
|
|
&i.Name,
|
|
&i.PublicKey,
|
|
&i.Status,
|
|
&i.SuccessorID,
|
|
&i.CreatedAt,
|
|
&i.DisabledAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listGroupMembersWithRole = `-- name: ListGroupMembersWithRole :many
|
|
SELECT p.id, p.name, p.kind, m.role
|
|
FROM membership m
|
|
JOIN principal p ON p.id = m.member_id
|
|
WHERE m.group_id = ?1
|
|
ORDER BY p.name
|
|
`
|
|
|
|
type ListGroupMembersWithRoleRow struct {
|
|
ID ident.UUID
|
|
Name string
|
|
Kind string
|
|
Role string
|
|
}
|
|
|
|
func (q *Queries) ListGroupMembersWithRole(ctx context.Context, groupID ident.UUID) ([]ListGroupMembersWithRoleRow, error) {
|
|
rows, err := q.db.QueryContext(ctx, listGroupMembersWithRole, groupID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListGroupMembersWithRoleRow
|
|
for rows.Next() {
|
|
var i ListGroupMembersWithRoleRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Kind,
|
|
&i.Role,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listMemberGroups = `-- name: ListMemberGroups :many
|
|
SELECT p.id, p.kind, p.name, p.public_key, p.status, p.successor_id, p.created_at, p.disabled_at
|
|
FROM membership m
|
|
JOIN principal p ON p.id = m.group_id
|
|
WHERE m.member_id = ?1
|
|
ORDER BY p.name
|
|
`
|
|
|
|
func (q *Queries) ListMemberGroups(ctx context.Context, memberID ident.UUID) ([]Principal, error) {
|
|
rows, err := q.db.QueryContext(ctx, listMemberGroups, memberID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Principal
|
|
for rows.Next() {
|
|
var i Principal
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Kind,
|
|
&i.Name,
|
|
&i.PublicKey,
|
|
&i.Status,
|
|
&i.SuccessorID,
|
|
&i.CreatedAt,
|
|
&i.DisabledAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listMemberGroupsWithRole = `-- name: ListMemberGroupsWithRole :many
|
|
SELECT p.id, p.name, p.status, m.role
|
|
FROM membership m
|
|
JOIN principal p ON p.id = m.group_id
|
|
WHERE m.member_id = ?1
|
|
ORDER BY p.name
|
|
`
|
|
|
|
type ListMemberGroupsWithRoleRow struct {
|
|
ID ident.UUID
|
|
Name string
|
|
Status string
|
|
Role string
|
|
}
|
|
|
|
func (q *Queries) ListMemberGroupsWithRole(ctx context.Context, memberID ident.UUID) ([]ListMemberGroupsWithRoleRow, error) {
|
|
rows, err := q.db.QueryContext(ctx, listMemberGroupsWithRole, memberID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListMemberGroupsWithRoleRow
|
|
for rows.Next() {
|
|
var i ListMemberGroupsWithRoleRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Status,
|
|
&i.Role,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listMembershipsByGroup = `-- name: ListMembershipsByGroup :many
|
|
SELECT group_id, member_id, role, created_at
|
|
FROM membership
|
|
WHERE group_id = ?1
|
|
ORDER BY created_at
|
|
`
|
|
|
|
func (q *Queries) ListMembershipsByGroup(ctx context.Context, groupID ident.UUID) ([]Membership, error) {
|
|
rows, err := q.db.QueryContext(ctx, listMembershipsByGroup, groupID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Membership
|
|
for rows.Next() {
|
|
var i Membership
|
|
if err := rows.Scan(
|
|
&i.GroupID,
|
|
&i.MemberID,
|
|
&i.Role,
|
|
&i.CreatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listMembershipsByMember = `-- name: ListMembershipsByMember :many
|
|
SELECT group_id, member_id, role, created_at
|
|
FROM membership
|
|
WHERE member_id = ?1
|
|
ORDER BY created_at
|
|
`
|
|
|
|
func (q *Queries) ListMembershipsByMember(ctx context.Context, memberID ident.UUID) ([]Membership, error) {
|
|
rows, err := q.db.QueryContext(ctx, listMembershipsByMember, memberID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Membership
|
|
for rows.Next() {
|
|
var i Membership
|
|
if err := rows.Scan(
|
|
&i.GroupID,
|
|
&i.MemberID,
|
|
&i.Role,
|
|
&i.CreatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listPrincipals = `-- name: ListPrincipals :many
|
|
SELECT id, kind, name, public_key, status, successor_id, created_at, disabled_at
|
|
FROM principal
|
|
ORDER BY name
|
|
`
|
|
|
|
func (q *Queries) ListPrincipals(ctx context.Context) ([]Principal, error) {
|
|
rows, err := q.db.QueryContext(ctx, listPrincipals)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Principal
|
|
for rows.Next() {
|
|
var i Principal
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Kind,
|
|
&i.Name,
|
|
&i.PublicKey,
|
|
&i.Status,
|
|
&i.SuccessorID,
|
|
&i.CreatedAt,
|
|
&i.DisabledAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listPrincipalsByKind = `-- name: ListPrincipalsByKind :many
|
|
SELECT id, kind, name, public_key, status, successor_id, created_at, disabled_at
|
|
FROM principal
|
|
WHERE kind = ?1
|
|
ORDER BY name
|
|
`
|
|
|
|
func (q *Queries) ListPrincipalsByKind(ctx context.Context, kind string) ([]Principal, error) {
|
|
rows, err := q.db.QueryContext(ctx, listPrincipalsByKind, kind)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Principal
|
|
for rows.Next() {
|
|
var i Principal
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Kind,
|
|
&i.Name,
|
|
&i.PublicKey,
|
|
&i.Status,
|
|
&i.SuccessorID,
|
|
&i.CreatedAt,
|
|
&i.DisabledAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listSessionsByPrincipal = `-- name: ListSessionsByPrincipal :many
|
|
SELECT id, token_hash, principal_id, kind, label, user_agent, ip_prefix, created_at, last_seen_at, expires_at, idle_expires_at, revoked_at
|
|
FROM session
|
|
WHERE principal_id = ?1
|
|
ORDER BY created_at DESC
|
|
`
|
|
|
|
func (q *Queries) ListSessionsByPrincipal(ctx context.Context, principalID ident.UUID) ([]Session, error) {
|
|
rows, err := q.db.QueryContext(ctx, listSessionsByPrincipal, principalID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Session
|
|
for rows.Next() {
|
|
var i Session
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.TokenHash,
|
|
&i.PrincipalID,
|
|
&i.Kind,
|
|
&i.Label,
|
|
&i.UserAgent,
|
|
&i.IpPrefix,
|
|
&i.CreatedAt,
|
|
&i.LastSeenAt,
|
|
&i.ExpiresAt,
|
|
&i.IdleExpiresAt,
|
|
&i.RevokedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const lockAccount = `-- name: LockAccount :one
|
|
UPDATE account
|
|
SET state = 'locked',
|
|
locked_until = ?1
|
|
WHERE principal_id = ?2
|
|
RETURNING principal_id, login, login_key, password_hash, password_changed_at, must_change_password, state, failed_attempts, locked_until, last_login_at, created_at
|
|
`
|
|
|
|
type LockAccountParams struct {
|
|
LockedUntil sql.NullInt64
|
|
PrincipalID ident.UUID
|
|
}
|
|
|
|
func (q *Queries) LockAccount(ctx context.Context, arg LockAccountParams) (Account, error) {
|
|
row := q.db.QueryRowContext(ctx, lockAccount, arg.LockedUntil, arg.PrincipalID)
|
|
var i Account
|
|
err := row.Scan(
|
|
&i.PrincipalID,
|
|
&i.Login,
|
|
&i.LoginKey,
|
|
&i.PasswordHash,
|
|
&i.PasswordChangedAt,
|
|
&i.MustChangePassword,
|
|
&i.State,
|
|
&i.FailedAttempts,
|
|
&i.LockedUntil,
|
|
&i.LastLoginAt,
|
|
&i.CreatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const recordLogin = `-- name: RecordLogin :one
|
|
UPDATE account
|
|
SET last_login_at = ?1,
|
|
failed_attempts = 0,
|
|
locked_until = NULL
|
|
WHERE principal_id = ?2
|
|
RETURNING principal_id, login, login_key, password_hash, password_changed_at, must_change_password, state, failed_attempts, locked_until, last_login_at, created_at
|
|
`
|
|
|
|
type RecordLoginParams struct {
|
|
LastLoginAt sql.NullInt64
|
|
PrincipalID ident.UUID
|
|
}
|
|
|
|
func (q *Queries) RecordLogin(ctx context.Context, arg RecordLoginParams) (Account, error) {
|
|
row := q.db.QueryRowContext(ctx, recordLogin, arg.LastLoginAt, arg.PrincipalID)
|
|
var i Account
|
|
err := row.Scan(
|
|
&i.PrincipalID,
|
|
&i.Login,
|
|
&i.LoginKey,
|
|
&i.PasswordHash,
|
|
&i.PasswordChangedAt,
|
|
&i.MustChangePassword,
|
|
&i.State,
|
|
&i.FailedAttempts,
|
|
&i.LockedUntil,
|
|
&i.LastLoginAt,
|
|
&i.CreatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const registerFailedLogin = `-- name: RegisterFailedLogin :one
|
|
UPDATE account
|
|
SET failed_attempts = failed_attempts + 1,
|
|
state = CASE
|
|
WHEN failed_attempts + 1 >= ?1 THEN 'locked'
|
|
ELSE state
|
|
END,
|
|
locked_until = CASE
|
|
WHEN failed_attempts + 1 >= ?1 THEN ?2
|
|
ELSE locked_until
|
|
END
|
|
WHERE principal_id = ?3
|
|
RETURNING principal_id, login, login_key, password_hash, password_changed_at, must_change_password, state, failed_attempts, locked_until, last_login_at, created_at
|
|
`
|
|
|
|
type RegisterFailedLoginParams struct {
|
|
MaxAttempts int64
|
|
LockedUntil sql.NullInt64
|
|
PrincipalID ident.UUID
|
|
}
|
|
|
|
func (q *Queries) RegisterFailedLogin(ctx context.Context, arg RegisterFailedLoginParams) (Account, error) {
|
|
row := q.db.QueryRowContext(ctx, registerFailedLogin, arg.MaxAttempts, arg.LockedUntil, arg.PrincipalID)
|
|
var i Account
|
|
err := row.Scan(
|
|
&i.PrincipalID,
|
|
&i.Login,
|
|
&i.LoginKey,
|
|
&i.PasswordHash,
|
|
&i.PasswordChangedAt,
|
|
&i.MustChangePassword,
|
|
&i.State,
|
|
&i.FailedAttempts,
|
|
&i.LockedUntil,
|
|
&i.LastLoginAt,
|
|
&i.CreatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const resetFailedAttempts = `-- name: ResetFailedAttempts :one
|
|
UPDATE account
|
|
SET failed_attempts = 0,
|
|
locked_until = NULL
|
|
WHERE principal_id = ?1
|
|
RETURNING principal_id, login, login_key, password_hash, password_changed_at, must_change_password, state, failed_attempts, locked_until, last_login_at, created_at
|
|
`
|
|
|
|
func (q *Queries) ResetFailedAttempts(ctx context.Context, principalID ident.UUID) (Account, error) {
|
|
row := q.db.QueryRowContext(ctx, resetFailedAttempts, principalID)
|
|
var i Account
|
|
err := row.Scan(
|
|
&i.PrincipalID,
|
|
&i.Login,
|
|
&i.LoginKey,
|
|
&i.PasswordHash,
|
|
&i.PasswordChangedAt,
|
|
&i.MustChangePassword,
|
|
&i.State,
|
|
&i.FailedAttempts,
|
|
&i.LockedUntil,
|
|
&i.LastLoginAt,
|
|
&i.CreatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const revokeACLEntry = `-- name: RevokeACLEntry :one
|
|
UPDATE acl_entry
|
|
SET revoked_at = ?1
|
|
WHERE document_id = ?2
|
|
AND subject_id = ?3
|
|
AND revoked_at IS NULL
|
|
RETURNING id, document_id, subject_id, permissions, propagate, expires_at, granted_by, created_at, revoked_at
|
|
`
|
|
|
|
type RevokeACLEntryParams struct {
|
|
RevokedAt sql.NullInt64
|
|
DocumentID ident.UUID
|
|
SubjectID ident.UUID
|
|
}
|
|
|
|
func (q *Queries) RevokeACLEntry(ctx context.Context, arg RevokeACLEntryParams) (AclEntry, error) {
|
|
row := q.db.QueryRowContext(ctx, revokeACLEntry, arg.RevokedAt, arg.DocumentID, arg.SubjectID)
|
|
var i AclEntry
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.DocumentID,
|
|
&i.SubjectID,
|
|
&i.Permissions,
|
|
&i.Propagate,
|
|
&i.ExpiresAt,
|
|
&i.GrantedBy,
|
|
&i.CreatedAt,
|
|
&i.RevokedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const revokeSession = `-- name: RevokeSession :one
|
|
UPDATE session
|
|
SET revoked_at = ?1
|
|
WHERE id = ?2
|
|
RETURNING id, token_hash, principal_id, kind, label, user_agent, ip_prefix, created_at, last_seen_at, expires_at, idle_expires_at, revoked_at
|
|
`
|
|
|
|
type RevokeSessionParams struct {
|
|
RevokedAt sql.NullInt64
|
|
ID ident.UUID
|
|
}
|
|
|
|
func (q *Queries) RevokeSession(ctx context.Context, arg RevokeSessionParams) (Session, error) {
|
|
row := q.db.QueryRowContext(ctx, revokeSession, arg.RevokedAt, arg.ID)
|
|
var i Session
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.TokenHash,
|
|
&i.PrincipalID,
|
|
&i.Kind,
|
|
&i.Label,
|
|
&i.UserAgent,
|
|
&i.IpPrefix,
|
|
&i.CreatedAt,
|
|
&i.LastSeenAt,
|
|
&i.ExpiresAt,
|
|
&i.IdleExpiresAt,
|
|
&i.RevokedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const revokeSessionByTokenHash = `-- name: RevokeSessionByTokenHash :one
|
|
UPDATE session
|
|
SET revoked_at = ?1
|
|
WHERE token_hash = ?2
|
|
RETURNING id, token_hash, principal_id, kind, label, user_agent, ip_prefix, created_at, last_seen_at, expires_at, idle_expires_at, revoked_at
|
|
`
|
|
|
|
type RevokeSessionByTokenHashParams struct {
|
|
RevokedAt sql.NullInt64
|
|
TokenHash []byte
|
|
}
|
|
|
|
func (q *Queries) RevokeSessionByTokenHash(ctx context.Context, arg RevokeSessionByTokenHashParams) (Session, error) {
|
|
row := q.db.QueryRowContext(ctx, revokeSessionByTokenHash, arg.RevokedAt, arg.TokenHash)
|
|
var i Session
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.TokenHash,
|
|
&i.PrincipalID,
|
|
&i.Kind,
|
|
&i.Label,
|
|
&i.UserAgent,
|
|
&i.IpPrefix,
|
|
&i.CreatedAt,
|
|
&i.LastSeenAt,
|
|
&i.ExpiresAt,
|
|
&i.IdleExpiresAt,
|
|
&i.RevokedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const touchSession = `-- name: TouchSession :one
|
|
UPDATE session
|
|
SET last_seen_at = ?1,
|
|
idle_expires_at = ?2
|
|
WHERE id = ?3
|
|
RETURNING id, token_hash, principal_id, kind, label, user_agent, ip_prefix, created_at, last_seen_at, expires_at, idle_expires_at, revoked_at
|
|
`
|
|
|
|
type TouchSessionParams struct {
|
|
LastSeenAt int64
|
|
IdleExpiresAt int64
|
|
ID ident.UUID
|
|
}
|
|
|
|
func (q *Queries) TouchSession(ctx context.Context, arg TouchSessionParams) (Session, error) {
|
|
row := q.db.QueryRowContext(ctx, touchSession, arg.LastSeenAt, arg.IdleExpiresAt, arg.ID)
|
|
var i Session
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.TokenHash,
|
|
&i.PrincipalID,
|
|
&i.Kind,
|
|
&i.Label,
|
|
&i.UserAgent,
|
|
&i.IpPrefix,
|
|
&i.CreatedAt,
|
|
&i.LastSeenAt,
|
|
&i.ExpiresAt,
|
|
&i.IdleExpiresAt,
|
|
&i.RevokedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const unlockAccount = `-- name: UnlockAccount :one
|
|
UPDATE account
|
|
SET state = 'active',
|
|
failed_attempts = 0,
|
|
locked_until = NULL
|
|
WHERE principal_id = ?1
|
|
RETURNING principal_id, login, login_key, password_hash, password_changed_at, must_change_password, state, failed_attempts, locked_until, last_login_at, created_at
|
|
`
|
|
|
|
func (q *Queries) UnlockAccount(ctx context.Context, principalID ident.UUID) (Account, error) {
|
|
row := q.db.QueryRowContext(ctx, unlockAccount, principalID)
|
|
var i Account
|
|
err := row.Scan(
|
|
&i.PrincipalID,
|
|
&i.Login,
|
|
&i.LoginKey,
|
|
&i.PasswordHash,
|
|
&i.PasswordChangedAt,
|
|
&i.MustChangePassword,
|
|
&i.State,
|
|
&i.FailedAttempts,
|
|
&i.LockedUntil,
|
|
&i.LastLoginAt,
|
|
&i.CreatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const updateAccountState = `-- name: UpdateAccountState :one
|
|
UPDATE account
|
|
SET state = ?1
|
|
WHERE principal_id = ?2
|
|
RETURNING principal_id, login, login_key, password_hash, password_changed_at, must_change_password, state, failed_attempts, locked_until, last_login_at, created_at
|
|
`
|
|
|
|
type UpdateAccountStateParams struct {
|
|
State string
|
|
PrincipalID ident.UUID
|
|
}
|
|
|
|
func (q *Queries) UpdateAccountState(ctx context.Context, arg UpdateAccountStateParams) (Account, error) {
|
|
row := q.db.QueryRowContext(ctx, updateAccountState, arg.State, arg.PrincipalID)
|
|
var i Account
|
|
err := row.Scan(
|
|
&i.PrincipalID,
|
|
&i.Login,
|
|
&i.LoginKey,
|
|
&i.PasswordHash,
|
|
&i.PasswordChangedAt,
|
|
&i.MustChangePassword,
|
|
&i.State,
|
|
&i.FailedAttempts,
|
|
&i.LockedUntil,
|
|
&i.LastLoginAt,
|
|
&i.CreatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const updateMembershipRole = `-- name: UpdateMembershipRole :one
|
|
UPDATE membership
|
|
SET role = ?1
|
|
WHERE group_id = ?2
|
|
AND member_id = ?3
|
|
RETURNING group_id, member_id, role, created_at
|
|
`
|
|
|
|
type UpdateMembershipRoleParams struct {
|
|
Role string
|
|
GroupID ident.UUID
|
|
MemberID ident.UUID
|
|
}
|
|
|
|
func (q *Queries) UpdateMembershipRole(ctx context.Context, arg UpdateMembershipRoleParams) (Membership, error) {
|
|
row := q.db.QueryRowContext(ctx, updateMembershipRole, arg.Role, arg.GroupID, arg.MemberID)
|
|
var i Membership
|
|
err := row.Scan(
|
|
&i.GroupID,
|
|
&i.MemberID,
|
|
&i.Role,
|
|
&i.CreatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const updatePassword = `-- name: UpdatePassword :one
|
|
UPDATE account
|
|
SET password_hash = ?1,
|
|
password_changed_at = ?2,
|
|
must_change_password = 0,
|
|
failed_attempts = 0,
|
|
locked_until = NULL
|
|
WHERE principal_id = ?3
|
|
RETURNING principal_id, login, login_key, password_hash, password_changed_at, must_change_password, state, failed_attempts, locked_until, last_login_at, created_at
|
|
`
|
|
|
|
type UpdatePasswordParams struct {
|
|
PasswordHash sql.NullString
|
|
PasswordChangedAt sql.NullInt64
|
|
PrincipalID ident.UUID
|
|
}
|
|
|
|
func (q *Queries) UpdatePassword(ctx context.Context, arg UpdatePasswordParams) (Account, error) {
|
|
row := q.db.QueryRowContext(ctx, updatePassword, arg.PasswordHash, arg.PasswordChangedAt, arg.PrincipalID)
|
|
var i Account
|
|
err := row.Scan(
|
|
&i.PrincipalID,
|
|
&i.Login,
|
|
&i.LoginKey,
|
|
&i.PasswordHash,
|
|
&i.PasswordChangedAt,
|
|
&i.MustChangePassword,
|
|
&i.State,
|
|
&i.FailedAttempts,
|
|
&i.LockedUntil,
|
|
&i.LastLoginAt,
|
|
&i.CreatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const updatePrincipalName = `-- name: UpdatePrincipalName :one
|
|
UPDATE principal
|
|
SET name = ?1
|
|
WHERE id = ?2
|
|
RETURNING id, kind, name, public_key, status, successor_id, created_at, disabled_at
|
|
`
|
|
|
|
type UpdatePrincipalNameParams struct {
|
|
Name string
|
|
ID ident.UUID
|
|
}
|
|
|
|
func (q *Queries) UpdatePrincipalName(ctx context.Context, arg UpdatePrincipalNameParams) (Principal, error) {
|
|
row := q.db.QueryRowContext(ctx, updatePrincipalName, arg.Name, arg.ID)
|
|
var i Principal
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Kind,
|
|
&i.Name,
|
|
&i.PublicKey,
|
|
&i.Status,
|
|
&i.SuccessorID,
|
|
&i.CreatedAt,
|
|
&i.DisabledAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const updatePrincipalPublicKey = `-- name: UpdatePrincipalPublicKey :one
|
|
UPDATE principal
|
|
SET public_key = ?1
|
|
WHERE id = ?2
|
|
RETURNING id, kind, name, public_key, status, successor_id, created_at, disabled_at
|
|
`
|
|
|
|
type UpdatePrincipalPublicKeyParams struct {
|
|
PublicKey []byte
|
|
ID ident.UUID
|
|
}
|
|
|
|
func (q *Queries) UpdatePrincipalPublicKey(ctx context.Context, arg UpdatePrincipalPublicKeyParams) (Principal, error) {
|
|
row := q.db.QueryRowContext(ctx, updatePrincipalPublicKey, arg.PublicKey, arg.ID)
|
|
var i Principal
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Kind,
|
|
&i.Name,
|
|
&i.PublicKey,
|
|
&i.Status,
|
|
&i.SuccessorID,
|
|
&i.CreatedAt,
|
|
&i.DisabledAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const updatePrincipalStatus = `-- name: UpdatePrincipalStatus :one
|
|
UPDATE principal
|
|
SET status = ?1,
|
|
disabled_at = ?2
|
|
WHERE id = ?3
|
|
RETURNING id, kind, name, public_key, status, successor_id, created_at, disabled_at
|
|
`
|
|
|
|
type UpdatePrincipalStatusParams struct {
|
|
Status string
|
|
DisabledAt sql.NullInt64
|
|
ID ident.UUID
|
|
}
|
|
|
|
func (q *Queries) UpdatePrincipalStatus(ctx context.Context, arg UpdatePrincipalStatusParams) (Principal, error) {
|
|
row := q.db.QueryRowContext(ctx, updatePrincipalStatus, arg.Status, arg.DisabledAt, arg.ID)
|
|
var i Principal
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Kind,
|
|
&i.Name,
|
|
&i.PublicKey,
|
|
&i.Status,
|
|
&i.SuccessorID,
|
|
&i.CreatedAt,
|
|
&i.DisabledAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const updatePrincipalSuccessor = `-- name: UpdatePrincipalSuccessor :one
|
|
UPDATE principal
|
|
SET successor_id = ?1
|
|
WHERE id = ?2
|
|
RETURNING id, kind, name, public_key, status, successor_id, created_at, disabled_at
|
|
`
|
|
|
|
type UpdatePrincipalSuccessorParams struct {
|
|
SuccessorID *ident.UUID
|
|
ID ident.UUID
|
|
}
|
|
|
|
func (q *Queries) UpdatePrincipalSuccessor(ctx context.Context, arg UpdatePrincipalSuccessorParams) (Principal, error) {
|
|
row := q.db.QueryRowContext(ctx, updatePrincipalSuccessor, arg.SuccessorID, arg.ID)
|
|
var i Principal
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Kind,
|
|
&i.Name,
|
|
&i.PublicKey,
|
|
&i.Status,
|
|
&i.SuccessorID,
|
|
&i.CreatedAt,
|
|
&i.DisabledAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const upsertACLEntry = `-- name: UpsertACLEntry :one
|
|
|
|
INSERT INTO acl_entry (
|
|
id,
|
|
document_id,
|
|
subject_id,
|
|
permissions,
|
|
propagate,
|
|
expires_at,
|
|
granted_by,
|
|
created_at
|
|
) VALUES (
|
|
?1,
|
|
?2,
|
|
?3,
|
|
?4,
|
|
?5,
|
|
?6,
|
|
?7,
|
|
?8
|
|
)
|
|
ON CONFLICT(document_id, subject_id) DO UPDATE SET
|
|
permissions = excluded.permissions,
|
|
propagate = excluded.propagate,
|
|
expires_at = excluded.expires_at,
|
|
granted_by = excluded.granted_by,
|
|
revoked_at = NULL
|
|
RETURNING id, document_id, subject_id, permissions, propagate, expires_at, granted_by, created_at, revoked_at
|
|
`
|
|
|
|
type UpsertACLEntryParams struct {
|
|
ID ident.UUID
|
|
DocumentID ident.UUID
|
|
SubjectID ident.UUID
|
|
Permissions int64
|
|
Propagate int64
|
|
ExpiresAt sql.NullInt64
|
|
GrantedBy ident.UUID
|
|
CreatedAt int64
|
|
}
|
|
|
|
// ============================================================================
|
|
// acl_entry
|
|
// ============================================================================
|
|
func (q *Queries) UpsertACLEntry(ctx context.Context, arg UpsertACLEntryParams) (AclEntry, error) {
|
|
row := q.db.QueryRowContext(ctx, upsertACLEntry,
|
|
arg.ID,
|
|
arg.DocumentID,
|
|
arg.SubjectID,
|
|
arg.Permissions,
|
|
arg.Propagate,
|
|
arg.ExpiresAt,
|
|
arg.GrantedBy,
|
|
arg.CreatedAt,
|
|
)
|
|
var i AclEntry
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.DocumentID,
|
|
&i.SubjectID,
|
|
&i.Permissions,
|
|
&i.Propagate,
|
|
&i.ExpiresAt,
|
|
&i.GrantedBy,
|
|
&i.CreatedAt,
|
|
&i.RevokedAt,
|
|
)
|
|
return i, err
|
|
}
|