Files
llmgw/internal/store/guard.go
T

40 lines
1.3 KiB
Go

package store
import (
"context"
"database/sql"
"time"
"gitea.mynodes.xyz/ilker/llmgw/internal/guard"
)
// GuardAttachments lists which policy is attached where.
func (s *DB) GuardAttachments(ctx context.Context) ([]guard.Attachment, error) {
return queryAll(ctx, s.db, `SELECT scope, scope_id, policy FROM guard_attachments ORDER BY scope, scope_id, policy`, nil,
func(rows *sql.Rows) (guard.Attachment, error) {
var a guard.Attachment
err := rows.Scan(&a.Scope, &a.ScopeID, &a.Policy)
return a, err
})
}
// AttachGuard attaches a policy to a scope; attaching it again changes
// nothing.
func (s *DB) AttachGuard(ctx context.Context, a guard.Attachment, by string) error {
_, err := s.db.ExecContext(ctx, `INSERT INTO guard_attachments (scope, scope_id, policy, added_at, added_by)
VALUES (?, ?, ?, ?, ?) ON CONFLICT (scope, scope_id, policy) DO NOTHING`,
a.Scope, a.ScopeID, a.Policy, millis(time.Now()), by)
return err
}
// DetachGuard removes one attachment, reporting whether it was there.
func (s *DB) DetachGuard(ctx context.Context, a guard.Attachment) (bool, error) {
res, err := s.db.ExecContext(ctx, `DELETE FROM guard_attachments WHERE scope = ? AND scope_id = ? AND policy = ?`,
a.Scope, a.ScopeID, a.Policy)
if err != nil {
return false, err
}
n, _ := res.RowsAffected()
return n > 0, nil
}