fix(security): synchronize report lifecycle and current roles

Revalidate admin roles from the database and serialize report generation registration with owner-scoped deletion.

Refs #9

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-11-20 21:35:38 +01:00
parent 0ebc531124
commit 609dbff7c1
16 changed files with 668 additions and 35 deletions

View File

@ -0,0 +1,58 @@
package api_test
import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"github.com/tradarr/backend/internal/models"
"io"
"time"
)
// Isolated database adapter for route validation tests. The role comes from
// this account store fixture, never from JWT claims.
type roleConnector struct{}
func (roleConnector) Connect(context.Context) (driver.Conn, error) { return roleConn{}, nil }
func (roleConnector) Driver() driver.Driver { return roleDriver{} }
type roleDriver struct{}
func (roleDriver) Open(string) (driver.Conn, error) { return roleConn{}, nil }
type roleConn struct{}
func (roleConn) Prepare(string) (driver.Stmt, error) { return nil, errors.New("unsupported") }
func (roleConn) Close() error { return nil }
func (roleConn) Begin() (driver.Tx, error) { return nil, errors.New("unsupported") }
func (roleConn) QueryContext(_ context.Context, _ string, args []driver.NamedValue) (driver.Rows, error) {
id := args[0].Value.(string)
role := "user"
if id == "admin-id" {
role = "admin"
}
return &roleRows{id: id, role: role}, nil
}
type roleRows struct {
id, role string
done bool
}
func (*roleRows) Columns() []string {
return []string{"id", "email", "password_hash", "role", "created_at", "updated_at"}
}
func (*roleRows) Close() error { return nil }
func (r *roleRows) Next(dest []driver.Value) error {
if r.done {
return io.EOF
}
r.done = true
copy(dest, []driver.Value{r.id, "fixture@example.test", "hash", r.role, time.Unix(1, 0), time.Unix(1, 0)})
return nil
}
var currentRoleDB = sql.OpenDB(roleConnector{})
func currentRoleRepository() *models.Repository { return models.NewRepository(currentRoleDB) }