feat(accounts): add admin accounts and user preferences

Remove public registration, support administrator-managed accounts, and persist personal preferences.

Refs #2 #5

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-11-20 19:54:06 +01:00
parent 1b0625f91c
commit 50bde816ed
46 changed files with 1362 additions and 256 deletions

View File

@ -0,0 +1,37 @@
package main
import (
"github.com/tradarr/backend/internal/auth"
"github.com/tradarr/backend/internal/config"
"github.com/tradarr/backend/internal/models"
"testing"
)
type bootstrapRepo struct {
existing *models.User
created *models.User
}
func (r *bootstrapRepo) GetUserByEmail(string) (*models.User, error) { return r.existing, nil }
func (r *bootstrapRepo) CreateUser(email, hash string, role models.Role) (*models.User, error) {
r.created = &models.User{Email: email, PasswordHash: hash, Role: role}
return r.created, nil
}
func TestBootstrap(t *testing.T) {
for _, role := range []models.Role{models.RoleAdmin, models.RoleUser} {
r := &bootstrapRepo{existing: &models.User{Role: role, PasswordHash: "unchanged"}}
if err := ensureAdmin(r, &config.Config{AdminEmail: "existing@example.test"}); err != nil || r.created != nil || r.existing.PasswordHash != "unchanged" || r.existing.Role != role {
t.Fatal("existing account changed", err)
}
}
r := &bootstrapRepo{}
if err := ensureAdmin(r, &config.Config{AdminEmail: "admin@example.test"}); err == nil || r.created != nil {
t.Fatal("empty bootstrap password accepted")
}
if err := ensureAdmin(r, &config.Config{AdminEmail: "admin@example.test", AdminPassword: "secret123"}); err != nil {
t.Fatal(err)
}
if r.created.Role != models.RoleAdmin || !auth.CheckPassword(r.created.PasswordHash, "secret123") {
t.Fatal("invalid bootstrap account")
}
}

View File

@ -66,7 +66,12 @@ func main() {
}
}
func ensureAdmin(repo *models.Repository, cfg *config.Config) error {
type adminRepository interface {
GetUserByEmail(string) (*models.User, error)
CreateUser(string, string, models.Role) (*models.User, error)
}
func ensureAdmin(repo adminRepository, cfg *config.Config) error {
if cfg.AdminEmail == "" {
return nil
}
@ -77,6 +82,9 @@ func ensureAdmin(repo *models.Repository, cfg *config.Config) error {
if existing != nil {
return nil
}
if cfg.AdminPassword == "" {
return fmt.Errorf("ADMIN_PASSWORD is required to create the bootstrap admin")
}
hash, err := auth.HashPassword(cfg.AdminPassword)
if err != nil {
return err