all good
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
*.db
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"mediarr/node"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
err, nodeA := node.NewNode(node.NewNodeParams{DatabaseName: "database"})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
nodeA.Up()
|
||||||
|
|
||||||
|
principal := nodeA.GetSystemPrincipal()
|
||||||
|
|
||||||
|
nodeA.CreateDocument()
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"embed"
|
||||||
|
"errors"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
_ "modernc.org/sqlite"
|
||||||
|
|
||||||
|
"github.com/pressly/goose/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DatabaseClient struct {
|
||||||
|
DB *sql.DB
|
||||||
|
Queries *Queries
|
||||||
|
}
|
||||||
|
|
||||||
|
// content holds migrations files ( embeded into the go program )
|
||||||
|
//
|
||||||
|
//go:embed migrations/*
|
||||||
|
var embedMigrations embed.FS
|
||||||
|
|
||||||
|
type NewDatabaseParams struct {
|
||||||
|
DatabaseName *string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDatabase(params NewDatabaseParams) (error, *DatabaseClient) {
|
||||||
|
|
||||||
|
databaseName := "database"
|
||||||
|
|
||||||
|
if params.DatabaseName != nil {
|
||||||
|
databaseName = *params.DatabaseName
|
||||||
|
}
|
||||||
|
|
||||||
|
err, normalizedDatabaseName := normalizeDatabaseName(databaseName)
|
||||||
|
if err != nil {
|
||||||
|
return err, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
db, err := sql.Open("sqlite", "./"+normalizedDatabaseName+".db")
|
||||||
|
if err != nil {
|
||||||
|
return err, nil
|
||||||
|
}
|
||||||
|
// setup database
|
||||||
|
|
||||||
|
goose.SetBaseFS(embedMigrations)
|
||||||
|
|
||||||
|
if err := goose.SetDialect("sqlite3"); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := goose.Up(db, "migrations"); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, &DatabaseClient{
|
||||||
|
DB: db,
|
||||||
|
Queries: New(db),
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizeDatabaseName(databaseName string) (error, string) {
|
||||||
|
|
||||||
|
t := strings.TrimSpace(databaseName)
|
||||||
|
|
||||||
|
l := strings.ToLower(t)
|
||||||
|
|
||||||
|
pattern := "[a-zA-Z]+"
|
||||||
|
|
||||||
|
re, err := regexp.Compile(pattern)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err, ""
|
||||||
|
}
|
||||||
|
|
||||||
|
s := re.MatchString(l)
|
||||||
|
|
||||||
|
if s == false {
|
||||||
|
return errors.New(databaseName + " doesn't follow the pattern " + pattern), ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, l
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// sqlc v1.31.1
|
||||||
|
|
||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DBTX interface {
|
||||||
|
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
|
||||||
|
PrepareContext(context.Context, string) (*sql.Stmt, error)
|
||||||
|
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
|
||||||
|
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(db DBTX) *Queries {
|
||||||
|
return &Queries{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Queries struct {
|
||||||
|
db DBTX
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
|
||||||
|
return &Queries{
|
||||||
|
db: tx,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
-- +goose Up
|
||||||
|
SELECT 'up SQL query';
|
||||||
|
|
||||||
|
-- +goose Down
|
||||||
|
SELECT 'down SQL query';
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
-- +goose Up
|
||||||
|
SELECT 'up SQL query';
|
||||||
|
|
||||||
|
CREATE TABLE principal (
|
||||||
|
id TEXT PRIMARY KEY,
|
||||||
|
kind TEXT NOT NULL CHECK ( kind IN ('cluster','user','guest','group','system') ),
|
||||||
|
name TEXT NOT NULL CHECK ( length(name) > 0 ),
|
||||||
|
public_key BLOB,
|
||||||
|
status TEXT NOT NULL CHECK ( status IN ('active','disabled','revoked') ),
|
||||||
|
created_at INTEGER NOT NULL,
|
||||||
|
disabled_at INTEGER
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE membership (
|
||||||
|
group_id TEXT NOT NULL REFERENCES principal(id),
|
||||||
|
member_id TEXT NOT NULL REFERENCES principal(id),
|
||||||
|
role TEXT NOT NULL DEFAULT 'member' CHECK ( role IN ('member','admin') ),
|
||||||
|
created_at INTEGER NOT NULL,
|
||||||
|
disabled_at INTEGER,
|
||||||
|
PRIMARY KEY (group_id, member_id)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE account (
|
||||||
|
principal_id TEXT PRIMARY KEY REFERENCES principal(id) ON DELETE CASCADE,
|
||||||
|
password_hash TEXT,
|
||||||
|
password_changed_at INTEGER,
|
||||||
|
must_change_password INTEGER NOT NULL DEFAULT 0 CHECK ( must_change_password IN (0,1) ),
|
||||||
|
state TEXT NOT NULL DEFAULT 'active' CHECK ( state IN ('active','locked','disabled') ),
|
||||||
|
failed_attempts INTEGER NOT NULL DEFAULT 0 CHECK ( failed_attempts >= 0 ),
|
||||||
|
locked_until INTEGER,
|
||||||
|
last_login_at INTEGER,
|
||||||
|
created_at INTEGER NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
-- +goose Down
|
||||||
|
SELECT 'down SQL query';
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
-- +goose Up
|
||||||
|
SELECT 'up SQL query';
|
||||||
|
|
||||||
|
-- +goose Down
|
||||||
|
SELECT 'down SQL query';
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// sqlc v1.31.1
|
||||||
|
|
||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Account struct {
|
||||||
|
PrincipalID string
|
||||||
|
PasswordHash sql.NullString
|
||||||
|
PasswordChangedAt sql.NullInt64
|
||||||
|
MustChangePassword int64
|
||||||
|
State string
|
||||||
|
FailedAttempts int64
|
||||||
|
LockedUntil sql.NullInt64
|
||||||
|
LastLoginAt sql.NullInt64
|
||||||
|
CreatedAt int64
|
||||||
|
}
|
||||||
|
|
||||||
|
type Membership struct {
|
||||||
|
GroupID string
|
||||||
|
MemberID string
|
||||||
|
Role string
|
||||||
|
CreatedAt int64
|
||||||
|
DisabledAt sql.NullInt64
|
||||||
|
}
|
||||||
|
|
||||||
|
type Principal struct {
|
||||||
|
ID string
|
||||||
|
Kind string
|
||||||
|
Name string
|
||||||
|
PublicKey []byte
|
||||||
|
Status string
|
||||||
|
CreatedAt int64
|
||||||
|
DisabledAt sql.NullInt64
|
||||||
|
}
|
||||||
@@ -0,0 +1,938 @@
|
|||||||
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// sqlc v1.31.1
|
||||||
|
// source: query.sql
|
||||||
|
|
||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
)
|
||||||
|
|
||||||
|
const createAccount = `-- name: CreateAccount :one
|
||||||
|
|
||||||
|
INSERT INTO account (
|
||||||
|
principal_id,
|
||||||
|
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
|
||||||
|
)
|
||||||
|
RETURNING principal_id, password_hash, password_changed_at, must_change_password, state, failed_attempts, locked_until, last_login_at, created_at
|
||||||
|
`
|
||||||
|
|
||||||
|
type CreateAccountParams struct {
|
||||||
|
PrincipalID 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.PasswordHash,
|
||||||
|
arg.PasswordChangedAt,
|
||||||
|
arg.MustChangePassword,
|
||||||
|
arg.State,
|
||||||
|
arg.FailedAttempts,
|
||||||
|
arg.LockedUntil,
|
||||||
|
arg.LastLoginAt,
|
||||||
|
arg.CreatedAt,
|
||||||
|
)
|
||||||
|
var i Account
|
||||||
|
err := row.Scan(
|
||||||
|
&i.PrincipalID,
|
||||||
|
&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,
|
||||||
|
disabled_at
|
||||||
|
) VALUES (
|
||||||
|
?1,
|
||||||
|
?2,
|
||||||
|
?3,
|
||||||
|
?4,
|
||||||
|
?5
|
||||||
|
)
|
||||||
|
RETURNING group_id, member_id, role, created_at, disabled_at
|
||||||
|
`
|
||||||
|
|
||||||
|
type CreateMembershipParams struct {
|
||||||
|
GroupID string
|
||||||
|
MemberID string
|
||||||
|
Role string
|
||||||
|
CreatedAt int64
|
||||||
|
DisabledAt sql.NullInt64
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================================================
|
||||||
|
// 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,
|
||||||
|
arg.DisabledAt,
|
||||||
|
)
|
||||||
|
var i Membership
|
||||||
|
err := row.Scan(
|
||||||
|
&i.GroupID,
|
||||||
|
&i.MemberID,
|
||||||
|
&i.Role,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.DisabledAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const createPrincipal = `-- name: CreatePrincipal :one
|
||||||
|
|
||||||
|
INSERT INTO principal (
|
||||||
|
id,
|
||||||
|
kind,
|
||||||
|
name,
|
||||||
|
public_key,
|
||||||
|
status,
|
||||||
|
created_at,
|
||||||
|
disabled_at
|
||||||
|
) VALUES (
|
||||||
|
?1,
|
||||||
|
?2,
|
||||||
|
?3,
|
||||||
|
?4,
|
||||||
|
?5,
|
||||||
|
?6,
|
||||||
|
?7
|
||||||
|
)
|
||||||
|
RETURNING id, kind, name, public_key, status, created_at, disabled_at
|
||||||
|
`
|
||||||
|
|
||||||
|
type CreatePrincipalParams struct {
|
||||||
|
ID string
|
||||||
|
Kind string
|
||||||
|
Name string
|
||||||
|
PublicKey []byte
|
||||||
|
Status string
|
||||||
|
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.CreatedAt,
|
||||||
|
arg.DisabledAt,
|
||||||
|
)
|
||||||
|
var i Principal
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.Kind,
|
||||||
|
&i.Name,
|
||||||
|
&i.PublicKey,
|
||||||
|
&i.Status,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.DisabledAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const deleteAccount = `-- name: DeleteAccount :exec
|
||||||
|
DELETE FROM account
|
||||||
|
WHERE principal_id = ?1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) DeleteAccount(ctx context.Context, principalID string) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, deleteAccount, principalID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
const deleteMembership = `-- name: DeleteMembership :exec
|
||||||
|
DELETE FROM membership
|
||||||
|
WHERE group_id = ?1
|
||||||
|
AND member_id = ?2
|
||||||
|
`
|
||||||
|
|
||||||
|
type DeleteMembershipParams struct {
|
||||||
|
GroupID string
|
||||||
|
MemberID string
|
||||||
|
}
|
||||||
|
|
||||||
|
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 string) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, deletePrincipal, id)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
const disableMembership = `-- name: DisableMembership :one
|
||||||
|
UPDATE membership
|
||||||
|
SET disabled_at = ?1
|
||||||
|
WHERE group_id = ?2
|
||||||
|
AND member_id = ?3
|
||||||
|
RETURNING group_id, member_id, role, created_at, disabled_at
|
||||||
|
`
|
||||||
|
|
||||||
|
type DisableMembershipParams struct {
|
||||||
|
DisabledAt sql.NullInt64
|
||||||
|
GroupID string
|
||||||
|
MemberID string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) DisableMembership(ctx context.Context, arg DisableMembershipParams) (Membership, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, disableMembership, arg.DisabledAt, arg.GroupID, arg.MemberID)
|
||||||
|
var i Membership
|
||||||
|
err := row.Scan(
|
||||||
|
&i.GroupID,
|
||||||
|
&i.MemberID,
|
||||||
|
&i.Role,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.DisabledAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const enableMembership = `-- name: EnableMembership :one
|
||||||
|
UPDATE membership
|
||||||
|
SET disabled_at = NULL
|
||||||
|
WHERE group_id = ?1
|
||||||
|
AND member_id = ?2
|
||||||
|
RETURNING group_id, member_id, role, created_at, disabled_at
|
||||||
|
`
|
||||||
|
|
||||||
|
type EnableMembershipParams struct {
|
||||||
|
GroupID string
|
||||||
|
MemberID string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) EnableMembership(ctx context.Context, arg EnableMembershipParams) (Membership, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, enableMembership, arg.GroupID, arg.MemberID)
|
||||||
|
var i Membership
|
||||||
|
err := row.Scan(
|
||||||
|
&i.GroupID,
|
||||||
|
&i.MemberID,
|
||||||
|
&i.Role,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.DisabledAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const getAccount = `-- name: GetAccount :one
|
||||||
|
SELECT principal_id, 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 string) (Account, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, getAccount, principalID)
|
||||||
|
var i Account
|
||||||
|
err := row.Scan(
|
||||||
|
&i.PrincipalID,
|
||||||
|
&i.PasswordHash,
|
||||||
|
&i.PasswordChangedAt,
|
||||||
|
&i.MustChangePassword,
|
||||||
|
&i.State,
|
||||||
|
&i.FailedAttempts,
|
||||||
|
&i.LockedUntil,
|
||||||
|
&i.LastLoginAt,
|
||||||
|
&i.CreatedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const getMembership = `-- name: GetMembership :one
|
||||||
|
SELECT group_id, member_id, role, created_at, disabled_at
|
||||||
|
FROM membership
|
||||||
|
WHERE group_id = ?1
|
||||||
|
AND member_id = ?2
|
||||||
|
`
|
||||||
|
|
||||||
|
type GetMembershipParams struct {
|
||||||
|
GroupID string
|
||||||
|
MemberID string
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
&i.DisabledAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const getPrincipal = `-- name: GetPrincipal :one
|
||||||
|
SELECT id, kind, name, public_key, status, created_at, disabled_at
|
||||||
|
FROM principal
|
||||||
|
WHERE id = ?1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) GetPrincipal(ctx context.Context, id string) (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.CreatedAt,
|
||||||
|
&i.DisabledAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const getPrincipalByName = `-- name: GetPrincipalByName :one
|
||||||
|
SELECT id, kind, name, public_key, status, 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.CreatedAt,
|
||||||
|
&i.DisabledAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const incrementFailedAttempts = `-- name: IncrementFailedAttempts :one
|
||||||
|
UPDATE account
|
||||||
|
SET failed_attempts = failed_attempts + 1
|
||||||
|
WHERE principal_id = ?1
|
||||||
|
RETURNING principal_id, 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 string) (Account, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, incrementFailedAttempts, principalID)
|
||||||
|
var i Account
|
||||||
|
err := row.Scan(
|
||||||
|
&i.PrincipalID,
|
||||||
|
&i.PasswordHash,
|
||||||
|
&i.PasswordChangedAt,
|
||||||
|
&i.MustChangePassword,
|
||||||
|
&i.State,
|
||||||
|
&i.FailedAttempts,
|
||||||
|
&i.LockedUntil,
|
||||||
|
&i.LastLoginAt,
|
||||||
|
&i.CreatedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const listActivePrincipals = `-- name: ListActivePrincipals :many
|
||||||
|
SELECT id, kind, name, public_key, status, 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.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.created_at, p.disabled_at
|
||||||
|
FROM membership m
|
||||||
|
JOIN principal p ON p.id = m.member_id
|
||||||
|
WHERE m.group_id = ?1
|
||||||
|
AND m.disabled_at IS NULL
|
||||||
|
ORDER BY p.name
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) ListGroupMembers(ctx context.Context, groupID string) ([]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.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 listMemberGroups = `-- name: ListMemberGroups :many
|
||||||
|
SELECT p.id, p.kind, p.name, p.public_key, p.status, p.created_at, p.disabled_at
|
||||||
|
FROM membership m
|
||||||
|
JOIN principal p ON p.id = m.group_id
|
||||||
|
WHERE m.member_id = ?1
|
||||||
|
AND m.disabled_at IS NULL
|
||||||
|
ORDER BY p.name
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) ListMemberGroups(ctx context.Context, memberID string) ([]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.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 listMembershipsByGroup = `-- name: ListMembershipsByGroup :many
|
||||||
|
SELECT group_id, member_id, role, created_at, disabled_at
|
||||||
|
FROM membership
|
||||||
|
WHERE group_id = ?1
|
||||||
|
ORDER BY created_at
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) ListMembershipsByGroup(ctx context.Context, groupID string) ([]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,
|
||||||
|
&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 listMembershipsByMember = `-- name: ListMembershipsByMember :many
|
||||||
|
SELECT group_id, member_id, role, created_at, disabled_at
|
||||||
|
FROM membership
|
||||||
|
WHERE member_id = ?1
|
||||||
|
ORDER BY created_at
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) ListMembershipsByMember(ctx context.Context, memberID string) ([]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,
|
||||||
|
&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 listPrincipals = `-- name: ListPrincipals :many
|
||||||
|
SELECT id, kind, name, public_key, status, 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.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, 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.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 lockAccount = `-- name: LockAccount :one
|
||||||
|
UPDATE account
|
||||||
|
SET state = 'locked',
|
||||||
|
locked_until = ?1
|
||||||
|
WHERE principal_id = ?2
|
||||||
|
RETURNING principal_id, 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 string
|
||||||
|
}
|
||||||
|
|
||||||
|
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.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, 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 string
|
||||||
|
}
|
||||||
|
|
||||||
|
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.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, 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 string) (Account, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, resetFailedAttempts, principalID)
|
||||||
|
var i Account
|
||||||
|
err := row.Scan(
|
||||||
|
&i.PrincipalID,
|
||||||
|
&i.PasswordHash,
|
||||||
|
&i.PasswordChangedAt,
|
||||||
|
&i.MustChangePassword,
|
||||||
|
&i.State,
|
||||||
|
&i.FailedAttempts,
|
||||||
|
&i.LockedUntil,
|
||||||
|
&i.LastLoginAt,
|
||||||
|
&i.CreatedAt,
|
||||||
|
)
|
||||||
|
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, 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 string) (Account, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, unlockAccount, principalID)
|
||||||
|
var i Account
|
||||||
|
err := row.Scan(
|
||||||
|
&i.PrincipalID,
|
||||||
|
&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, password_hash, password_changed_at, must_change_password, state, failed_attempts, locked_until, last_login_at, created_at
|
||||||
|
`
|
||||||
|
|
||||||
|
type UpdateAccountStateParams struct {
|
||||||
|
State string
|
||||||
|
PrincipalID string
|
||||||
|
}
|
||||||
|
|
||||||
|
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.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, disabled_at
|
||||||
|
`
|
||||||
|
|
||||||
|
type UpdateMembershipRoleParams struct {
|
||||||
|
Role string
|
||||||
|
GroupID string
|
||||||
|
MemberID string
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
&i.DisabledAt,
|
||||||
|
)
|
||||||
|
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, 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 string
|
||||||
|
}
|
||||||
|
|
||||||
|
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.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, created_at, disabled_at
|
||||||
|
`
|
||||||
|
|
||||||
|
type UpdatePrincipalNameParams struct {
|
||||||
|
Name string
|
||||||
|
ID string
|
||||||
|
}
|
||||||
|
|
||||||
|
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.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, created_at, disabled_at
|
||||||
|
`
|
||||||
|
|
||||||
|
type UpdatePrincipalPublicKeyParams struct {
|
||||||
|
PublicKey []byte
|
||||||
|
ID string
|
||||||
|
}
|
||||||
|
|
||||||
|
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.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, created_at, disabled_at
|
||||||
|
`
|
||||||
|
|
||||||
|
type UpdatePrincipalStatusParams struct {
|
||||||
|
Status string
|
||||||
|
DisabledAt sql.NullInt64
|
||||||
|
ID string
|
||||||
|
}
|
||||||
|
|
||||||
|
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.CreatedAt,
|
||||||
|
&i.DisabledAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
@@ -0,0 +1,242 @@
|
|||||||
|
-- ============================================================================
|
||||||
|
-- principal
|
||||||
|
-- ============================================================================
|
||||||
|
|
||||||
|
-- name: CreatePrincipal :one
|
||||||
|
INSERT INTO principal (
|
||||||
|
id,
|
||||||
|
kind,
|
||||||
|
name,
|
||||||
|
public_key,
|
||||||
|
status,
|
||||||
|
created_at,
|
||||||
|
disabled_at
|
||||||
|
) VALUES (
|
||||||
|
sqlc.arg(id),
|
||||||
|
sqlc.arg(kind),
|
||||||
|
sqlc.arg(name),
|
||||||
|
sqlc.narg(public_key),
|
||||||
|
sqlc.arg(status),
|
||||||
|
sqlc.arg(created_at),
|
||||||
|
sqlc.narg(disabled_at)
|
||||||
|
)
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: GetPrincipal :one
|
||||||
|
SELECT *
|
||||||
|
FROM principal
|
||||||
|
WHERE id = sqlc.arg(id);
|
||||||
|
|
||||||
|
-- name: GetPrincipalByName :one
|
||||||
|
SELECT *
|
||||||
|
FROM principal
|
||||||
|
WHERE name = sqlc.arg(name);
|
||||||
|
|
||||||
|
-- name: ListPrincipals :many
|
||||||
|
SELECT *
|
||||||
|
FROM principal
|
||||||
|
ORDER BY name;
|
||||||
|
|
||||||
|
-- name: ListPrincipalsByKind :many
|
||||||
|
SELECT *
|
||||||
|
FROM principal
|
||||||
|
WHERE kind = sqlc.arg(kind)
|
||||||
|
ORDER BY name;
|
||||||
|
|
||||||
|
-- name: ListActivePrincipals :many
|
||||||
|
SELECT *
|
||||||
|
FROM principal
|
||||||
|
WHERE status = 'active'
|
||||||
|
ORDER BY name;
|
||||||
|
|
||||||
|
-- name: UpdatePrincipalName :one
|
||||||
|
UPDATE principal
|
||||||
|
SET name = sqlc.arg(name)
|
||||||
|
WHERE id = sqlc.arg(id)
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: UpdatePrincipalPublicKey :one
|
||||||
|
UPDATE principal
|
||||||
|
SET public_key = sqlc.narg(public_key)
|
||||||
|
WHERE id = sqlc.arg(id)
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: UpdatePrincipalStatus :one
|
||||||
|
UPDATE principal
|
||||||
|
SET status = sqlc.arg(status),
|
||||||
|
disabled_at = sqlc.narg(disabled_at)
|
||||||
|
WHERE id = sqlc.arg(id)
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: DeletePrincipal :exec
|
||||||
|
DELETE FROM principal
|
||||||
|
WHERE id = sqlc.arg(id);
|
||||||
|
|
||||||
|
-- ============================================================================
|
||||||
|
-- membership
|
||||||
|
-- ============================================================================
|
||||||
|
|
||||||
|
-- name: CreateMembership :one
|
||||||
|
INSERT INTO membership (
|
||||||
|
group_id,
|
||||||
|
member_id,
|
||||||
|
role,
|
||||||
|
created_at,
|
||||||
|
disabled_at
|
||||||
|
) VALUES (
|
||||||
|
sqlc.arg(group_id),
|
||||||
|
sqlc.arg(member_id),
|
||||||
|
sqlc.arg(role),
|
||||||
|
sqlc.arg(created_at),
|
||||||
|
sqlc.narg(disabled_at)
|
||||||
|
)
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: GetMembership :one
|
||||||
|
SELECT *
|
||||||
|
FROM membership
|
||||||
|
WHERE group_id = sqlc.arg(group_id)
|
||||||
|
AND member_id = sqlc.arg(member_id);
|
||||||
|
|
||||||
|
-- name: ListMembershipsByGroup :many
|
||||||
|
SELECT *
|
||||||
|
FROM membership
|
||||||
|
WHERE group_id = sqlc.arg(group_id)
|
||||||
|
ORDER BY created_at;
|
||||||
|
|
||||||
|
-- name: ListMembershipsByMember :many
|
||||||
|
SELECT *
|
||||||
|
FROM membership
|
||||||
|
WHERE member_id = sqlc.arg(member_id)
|
||||||
|
ORDER BY created_at;
|
||||||
|
|
||||||
|
-- name: ListGroupMembers :many
|
||||||
|
SELECT p.*
|
||||||
|
FROM membership m
|
||||||
|
JOIN principal p ON p.id = m.member_id
|
||||||
|
WHERE m.group_id = sqlc.arg(group_id)
|
||||||
|
AND m.disabled_at IS NULL
|
||||||
|
ORDER BY p.name;
|
||||||
|
|
||||||
|
-- name: ListMemberGroups :many
|
||||||
|
SELECT p.*
|
||||||
|
FROM membership m
|
||||||
|
JOIN principal p ON p.id = m.group_id
|
||||||
|
WHERE m.member_id = sqlc.arg(member_id)
|
||||||
|
AND m.disabled_at IS NULL
|
||||||
|
ORDER BY p.name;
|
||||||
|
|
||||||
|
-- name: UpdateMembershipRole :one
|
||||||
|
UPDATE membership
|
||||||
|
SET role = sqlc.arg(role)
|
||||||
|
WHERE group_id = sqlc.arg(group_id)
|
||||||
|
AND member_id = sqlc.arg(member_id)
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: DisableMembership :one
|
||||||
|
UPDATE membership
|
||||||
|
SET disabled_at = sqlc.arg(disabled_at)
|
||||||
|
WHERE group_id = sqlc.arg(group_id)
|
||||||
|
AND member_id = sqlc.arg(member_id)
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: EnableMembership :one
|
||||||
|
UPDATE membership
|
||||||
|
SET disabled_at = NULL
|
||||||
|
WHERE group_id = sqlc.arg(group_id)
|
||||||
|
AND member_id = sqlc.arg(member_id)
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: DeleteMembership :exec
|
||||||
|
DELETE FROM membership
|
||||||
|
WHERE group_id = sqlc.arg(group_id)
|
||||||
|
AND member_id = sqlc.arg(member_id);
|
||||||
|
|
||||||
|
-- ============================================================================
|
||||||
|
-- account
|
||||||
|
-- ============================================================================
|
||||||
|
|
||||||
|
-- name: CreateAccount :one
|
||||||
|
INSERT INTO account (
|
||||||
|
principal_id,
|
||||||
|
password_hash,
|
||||||
|
password_changed_at,
|
||||||
|
must_change_password,
|
||||||
|
state,
|
||||||
|
failed_attempts,
|
||||||
|
locked_until,
|
||||||
|
last_login_at,
|
||||||
|
created_at
|
||||||
|
) VALUES (
|
||||||
|
sqlc.arg(principal_id),
|
||||||
|
sqlc.narg(password_hash),
|
||||||
|
sqlc.narg(password_changed_at),
|
||||||
|
sqlc.arg(must_change_password),
|
||||||
|
sqlc.arg(state),
|
||||||
|
sqlc.arg(failed_attempts),
|
||||||
|
sqlc.narg(locked_until),
|
||||||
|
sqlc.narg(last_login_at),
|
||||||
|
sqlc.arg(created_at)
|
||||||
|
)
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: GetAccount :one
|
||||||
|
SELECT *
|
||||||
|
FROM account
|
||||||
|
WHERE principal_id = sqlc.arg(principal_id);
|
||||||
|
|
||||||
|
-- name: UpdatePassword :one
|
||||||
|
UPDATE account
|
||||||
|
SET password_hash = sqlc.arg(password_hash),
|
||||||
|
password_changed_at = sqlc.arg(password_changed_at),
|
||||||
|
must_change_password = 0,
|
||||||
|
failed_attempts = 0,
|
||||||
|
locked_until = NULL
|
||||||
|
WHERE principal_id = sqlc.arg(principal_id)
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: UpdateAccountState :one
|
||||||
|
UPDATE account
|
||||||
|
SET state = sqlc.arg(state)
|
||||||
|
WHERE principal_id = sqlc.arg(principal_id)
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: IncrementFailedAttempts :one
|
||||||
|
UPDATE account
|
||||||
|
SET failed_attempts = failed_attempts + 1
|
||||||
|
WHERE principal_id = sqlc.arg(principal_id)
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: ResetFailedAttempts :one
|
||||||
|
UPDATE account
|
||||||
|
SET failed_attempts = 0,
|
||||||
|
locked_until = NULL
|
||||||
|
WHERE principal_id = sqlc.arg(principal_id)
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: LockAccount :one
|
||||||
|
UPDATE account
|
||||||
|
SET state = 'locked',
|
||||||
|
locked_until = sqlc.arg(locked_until)
|
||||||
|
WHERE principal_id = sqlc.arg(principal_id)
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: UnlockAccount :one
|
||||||
|
UPDATE account
|
||||||
|
SET state = 'active',
|
||||||
|
failed_attempts = 0,
|
||||||
|
locked_until = NULL
|
||||||
|
WHERE principal_id = sqlc.arg(principal_id)
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: RecordLogin :one
|
||||||
|
UPDATE account
|
||||||
|
SET last_login_at = sqlc.arg(last_login_at),
|
||||||
|
failed_attempts = 0,
|
||||||
|
locked_until = NULL
|
||||||
|
WHERE principal_id = sqlc.arg(principal_id)
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: DeleteAccount :exec
|
||||||
|
DELETE FROM account
|
||||||
|
WHERE principal_id = sqlc.arg(principal_id);
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package document
|
||||||
|
|
||||||
|
type Document struct {
|
||||||
|
Kind string
|
||||||
|
Hidden string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDocument() {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
module mediarr
|
||||||
|
|
||||||
|
go 1.26.7
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/pressly/goose/v3 v3.30.1
|
||||||
|
modernc.org/sqlite v1.62.0
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/dustin/go-humanize v1.1.0 // indirect
|
||||||
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.24 // indirect
|
||||||
|
github.com/mfridman/interpolate v0.2.0 // indirect
|
||||||
|
github.com/ncruces/go-strftime v1.1.0 // indirect
|
||||||
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||||
|
github.com/sethvargo/go-retry v0.5.0 // indirect
|
||||||
|
go.uber.org/multierr v1.11.0 // indirect
|
||||||
|
golang.org/x/sync v0.25.0 // indirect
|
||||||
|
golang.org/x/sys v0.50.0 // indirect
|
||||||
|
modernc.org/libc v1.80.2 // indirect
|
||||||
|
modernc.org/mathutil v1.7.1 // indirect
|
||||||
|
modernc.org/memory v1.12.1 // indirect
|
||||||
|
)
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
github.com/dustin/go-humanize v1.1.0 h1:dbKTrvD0klcbBV/h4AWJdMuZogJACoMlvWIWZ5b2xWg=
|
||||||
|
github.com/dustin/go-humanize v1.1.0/go.mod h1:hc1CvRkJMsgxqjmjMQF3QNRAZBwY8AXBAzKYoSX9sFI=
|
||||||
|
github.com/google/pprof v0.0.0-20261016150608-1facf8f91b50 h1:PlNWJRxu5APSV0pWqfIsZ0qt9OOGHSWfZQ4zvx+WgWs=
|
||||||
|
github.com/google/pprof v0.0.0-20261016150608-1facf8f91b50/go.mod h1:yUo1AMMpsPIDFGuwDK8nLcf8+vvAd5l2O6JDZ6hpwrM=
|
||||||
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
|
||||||
|
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
|
||||||
|
github.com/mattn/go-isatty v0.0.24 h1:tGZZoVgT/KiqK1c8ocVLeDS8BSWMRd47J3Lbz7vsReI=
|
||||||
|
github.com/mattn/go-isatty v0.0.24/go.mod h1:nMCL3Zebbrt45jsMDgnfIwz6ydEQApk5oEI3HqDio6A=
|
||||||
|
github.com/mfridman/interpolate v0.2.0 h1:HM9ID8p6CUxoMJX+fECOtGQ2Mu7TW/GIMPyu48nnvUg=
|
||||||
|
github.com/mfridman/interpolate v0.2.0/go.mod h1:8SfDy95IaPNnIokZh9mvIppmc3fGl8fx+Gu7uusuO9Q=
|
||||||
|
github.com/ncruces/go-strftime v1.1.0 h1:vtPmPWMkb539RGV9WpWT9EwJGvrOCKvMNI4obWL47Hk=
|
||||||
|
github.com/ncruces/go-strftime v1.1.0/go.mod h1:yVaq7iwCN09vHRrKBrrflXT0M9tTMG2KmZ9Fj7xeG/g=
|
||||||
|
github.com/pressly/goose/v3 v3.30.1 h1:KO28meBdbAUIdMbTP7bl9dp1FYZGXcYWb2O7miT09lE=
|
||||||
|
github.com/pressly/goose/v3 v3.30.1/go.mod h1:CHGwuG2acDerUaXwz2i5T18VSauXpYDiL4WxzDvcOIM=
|
||||||
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||||
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||||
|
github.com/sethvargo/go-retry v0.5.0 h1:JYfFKmVJ8ER06GReN1efzc5cm52BrVsFVzmcHiKJwfY=
|
||||||
|
github.com/sethvargo/go-retry v0.5.0/go.mod h1:tvsjdKG6xfiCx4LSiUZ06kcv38xvdVQwv8R6/VnnVWg=
|
||||||
|
github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE=
|
||||||
|
github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg=
|
||||||
|
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
|
||||||
|
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
||||||
|
go.yaml.in/yaml/v3 v3.0.6 h1:zcDfvjXEqTQmpjniUBLc91xYJMB2c4QOFxEF/LCEckM=
|
||||||
|
go.yaml.in/yaml/v3 v3.0.6/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
|
||||||
|
golang.org/x/mod v0.42.0 h1:ICeL3t2L30ljSPHHxDnR4fqVcBt/zR2zJAb9juavmQg=
|
||||||
|
golang.org/x/mod v0.42.0/go.mod h1:u5fzIrRInkL8MaO1cbRkUHT64v303YIT5KEqpNr7jRs=
|
||||||
|
golang.org/x/sync v0.25.0 h1:dJnifr0nMV8/SBnBvdo4eSGGnrc3O4FFx6IqvtLcmRs=
|
||||||
|
golang.org/x/sync v0.25.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
|
||||||
|
golang.org/x/sys v0.50.0 h1:dkKZEFTEKaLr1sjhs0WiKqoi8Jj5nHbS3Njmx1UoMJs=
|
||||||
|
golang.org/x/sys v0.50.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
||||||
|
golang.org/x/tools v0.52.0 h1:ryxg4U63oaUYk7uOLTF37n43f4qgtGvqV4Ioit1BaEs=
|
||||||
|
golang.org/x/tools v0.52.0/go.mod h1:ItcP5UNaVGG+Yc4eIYmaFLNJ4soEopkx5Y2d4lcEurs=
|
||||||
|
modernc.org/cc/v4 v4.30.6-0.20261101114202-1b9d1a076f6b h1:WE5SLOURcNd8s6uPR/n4WV4weQxxYKi5OpEZkfNVvdE=
|
||||||
|
modernc.org/cc/v4 v4.30.6-0.20261101114202-1b9d1a076f6b/go.mod h1:OnovgIhbbMXMu1aISnJ0wvVD1KnW+cAUJkIrAWh+kVI=
|
||||||
|
modernc.org/ccgo/v4 v4.38.1 h1:wdxJVBuFT8JufZnnQyS6Bbz3NOWhViD0xJOGR96Bpd4=
|
||||||
|
modernc.org/ccgo/v4 v4.38.1/go.mod h1:kOpEkJRbFwBvjUWwn4j1j9B7iFHbl1+orhgTMCDkDBk=
|
||||||
|
modernc.org/fileutil v1.4.0 h1:j6ZzNTftVS054gi281TyLjHPp6CPHr2KCxEXjEbD6SM=
|
||||||
|
modernc.org/fileutil v1.4.0/go.mod h1:EqdKFDxiByqxLk8ozOxObDSfcVOv/54xDs/DUHdvCUU=
|
||||||
|
modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI=
|
||||||
|
modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito=
|
||||||
|
modernc.org/gc/v3 v3.2.0 h1:/Y3LGDMQaoYf55PebP1qsZ5FDGgAZyG1RYKEHeQzh1o=
|
||||||
|
modernc.org/gc/v3 v3.2.0/go.mod h1:HFK/6AGESC7Ex+EZJhJ2Gni6cTaYpSMmU/cT9RmlfYY=
|
||||||
|
modernc.org/goabi0 v0.2.0 h1:HvEowk7LxcPd0eq6mVOAEMai46V+i7Jrj13t4AzuNks=
|
||||||
|
modernc.org/goabi0 v0.2.0/go.mod h1:CEFRnnJhKvWT1c1JTI3Avm+tgOWbkOu5oPA8eH8LnMI=
|
||||||
|
modernc.org/libc v1.80.2 h1:3+a4fJZvvzg920ZC0mOepnKPGn7cqCaVeDxm/UnrM1A=
|
||||||
|
modernc.org/libc v1.80.2/go.mod h1:pvo25Rs3OJEbDqhUV9jdEGevxeLJrJeDmNid/AcQV1g=
|
||||||
|
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
|
||||||
|
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
|
||||||
|
modernc.org/memory v1.12.1 h1:nFMiWrpStgZczNl6XI9GnIk/rWhYIyHGUaR04pGbp9g=
|
||||||
|
modernc.org/memory v1.12.1/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
|
||||||
|
modernc.org/opt v0.2.0 h1:tGyef5ApycA7FSEOMraay9SaTk5zmbx7Tu+cJs4QKZg=
|
||||||
|
modernc.org/opt v0.2.0/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns=
|
||||||
|
modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w=
|
||||||
|
modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE=
|
||||||
|
modernc.org/sqlite v1.62.0 h1:gMaCjSM1qpl+iOjiWhR1LYP3zqrYDI+5GYLGt6veP2g=
|
||||||
|
modernc.org/sqlite v1.62.0/go.mod h1:Jzjh1mt4s0HuQ0KvtB1LIuHiGRmU2D5pgj8dUhW7/SE=
|
||||||
|
modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0=
|
||||||
|
modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A=
|
||||||
|
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
|
||||||
|
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package node
|
||||||
|
|
||||||
|
import (
|
||||||
|
"mediarr/database"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Node struct {
|
||||||
|
db database.DatabaseClient
|
||||||
|
}
|
||||||
|
|
||||||
|
type NewNodeParams struct {
|
||||||
|
DatabaseName string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewNode(params NewNodeParams) (error, Node) {
|
||||||
|
|
||||||
|
err, db := database.NewDatabase(database.NewDatabaseParams{
|
||||||
|
DatabaseName: ¶ms.DatabaseName,
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err, Node{}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, Node{
|
||||||
|
db: *db,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package node
|
||||||
|
|
||||||
|
func (n *Node) AddDocument() {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
package node
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"mediarr/database"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (n *Node) Up() error {
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// Create principal system if not found
|
||||||
|
err, defaultPrincipals := getRequiredNodePrincipals()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var nodePrincipals = make([]database.Principal, len(defaultPrincipals))
|
||||||
|
|
||||||
|
for _, principal := range defaultPrincipals {
|
||||||
|
foundedPrincipal, err := n.db.Queries.GetPrincipalByName(ctx, principal.Name)
|
||||||
|
if err == nil {
|
||||||
|
nodePrincipals = append(nodePrincipals, foundedPrincipal)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
createdPrincipal, err := n.db.Queries.CreatePrincipal(ctx, principal)
|
||||||
|
if err == nil {
|
||||||
|
nodePrincipals = append(nodePrincipals, createdPrincipal)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func getRequiredNodePrincipals() (error, []database.CreatePrincipalParams) {
|
||||||
|
|
||||||
|
var principals []database.CreatePrincipalParams
|
||||||
|
|
||||||
|
err, defaultSystemPrincipal := getDefaultSystemPrincipal()
|
||||||
|
if err != nil {
|
||||||
|
return err, []database.CreatePrincipalParams{}
|
||||||
|
}
|
||||||
|
|
||||||
|
principals = append(principals, defaultSystemPrincipal)
|
||||||
|
|
||||||
|
return nil, principals
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func getDefaultSystemPrincipal() (error, database.CreatePrincipalParams) {
|
||||||
|
|
||||||
|
principalUuid, err := uuid.NewV7()
|
||||||
|
if err != nil {
|
||||||
|
return err, database.CreatePrincipalParams{}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, database.CreatePrincipalParams{
|
||||||
|
ID: principalUuid.String(),
|
||||||
|
Kind: "system",
|
||||||
|
Name: "System",
|
||||||
|
PublicKey: []byte{},
|
||||||
|
Status: "active",
|
||||||
|
CreatedAt: time.Now().Unix(),
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
version: "2"
|
||||||
|
sql:
|
||||||
|
- engine: "sqlite"
|
||||||
|
queries: "./database/query/query.sql"
|
||||||
|
schema: "./database/migrations"
|
||||||
|
gen:
|
||||||
|
go:
|
||||||
|
package: "database"
|
||||||
|
out: "database"
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package storage
|
||||||
|
|
||||||
|
type Storage struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewStorage() Storage {
|
||||||
|
return Storage{}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package user
|
||||||
|
|
||||||
|
const (
|
||||||
|
ADMIN = "admin"
|
||||||
|
MEMBER = "member"
|
||||||
|
)
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package user
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
Email *string
|
||||||
|
Role string
|
||||||
|
}
|
||||||
|
|
||||||
|
type NewUserParams struct {
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUser(params NewUserParams) User {
|
||||||
|
|
||||||
|
return User{
|
||||||
|
Username: params.Username,
|
||||||
|
Password: params.Password,
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *User) Is(role string) bool {
|
||||||
|
return role == u.Role
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user