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

@ -2,8 +2,12 @@ package handlers
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/tradarr/backend/internal/auth"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/tradarr/backend/internal/ai"
@ -26,9 +30,9 @@ func (h *Handler) GetCredentials(c *gin.Context) {
return
}
type credResponse struct {
SourceID string `json:"source_id"`
SourceName string `json:"source_name"`
Username string `json:"username"`
SourceID string `json:"source_id"`
SourceName string `json:"source_name"`
Username string `json:"username"`
HasPassword bool `json:"has_password"`
}
var result []credResponse
@ -233,7 +237,8 @@ func (h *Handler) ListSources(c *gin.Context) {
}
type updateSourceRequest struct {
Enabled bool `json:"enabled"`
Enabled bool `json:"enabled"`
ArticleLimit *int `json:"article_limit" binding:"omitempty,min=1,max=200"`
}
func (h *Handler) UpdateSource(c *gin.Context) {
@ -242,7 +247,7 @@ func (h *Handler) UpdateSource(c *gin.Context) {
httputil.BadRequest(c, err)
return
}
if err := h.repo.UpdateSource(c.Param("id"), req.Enabled); err != nil {
if err := h.repo.UpdateSource(c.Param("id"), req.Enabled, req.ArticleLimit); err != nil {
httputil.InternalError(c, err)
return
}
@ -299,13 +304,22 @@ func (h *Handler) UpdateSettings(c *gin.Context) {
return
}
for _, s := range req.Settings {
if err := h.repo.SetSetting(s.Key, s.Value); err != nil {
httputil.InternalError(c, err)
return
if s.Key == "summary_max_articles" {
limit, err := strconv.Atoi(s.Value)
if err != nil || limit < 1 || limit > 500 {
httputil.BadRequest(c, fmt.Errorf("summary_max_articles must be between 1 and 500"))
return
}
}
}
if err := h.scheduler.Reload(); err != nil {
fmt.Printf("settings reload scheduler: %v\n", err)
if err := h.repo.SetSettings(req.Settings); err != nil {
httputil.InternalError(c, err)
return
}
if h.scheduler != nil {
if err := h.scheduler.Reload(); err != nil {
fmt.Printf("settings reload scheduler: %v\n", err)
}
}
httputil.OK(c, gin.H{"ok": true})
}
@ -494,30 +508,53 @@ func (h *Handler) UpdateAdminUser(c *gin.Context) {
}
user, err := h.repo.UpdateUser(id, req.Email, models.Role(req.Role))
if err != nil {
httputil.InternalError(c, err)
accountError(c, err)
return
}
httputil.OK(c, user)
}
func (h *Handler) DeleteAdminUser(c *gin.Context) {
id := c.Param("id")
// Empêcher la suppression du dernier admin
user, err := h.repo.GetUserByID(id)
if err != nil || user == nil {
func accountError(c *gin.Context, err error) {
switch {
case errors.Is(err, models.ErrLastAdmin):
c.JSON(http.StatusBadRequest, gin.H{"error": "cannot remove the last admin"})
case errors.Is(err, sql.ErrNoRows):
httputil.NotFound(c)
return
case models.IsDuplicateEmail(err):
c.JSON(http.StatusConflict, gin.H{"error": "email already in use"})
default:
c.JSON(http.StatusInternalServerError, gin.H{"error": "account operation failed"})
}
if user.Role == "admin" {
count, _ := h.repo.CountAdmins()
if count <= 1 {
c.JSON(http.StatusBadRequest, gin.H{"error": "cannot delete the last admin"})
return
}
}
if err := h.repo.DeleteUser(id); err != nil {
httputil.InternalError(c, err)
}
func (h *Handler) DeleteAdminUser(c *gin.Context) {
if err := h.repo.DeleteUser(c.Param("id")); err != nil {
accountError(c, err)
return
}
httputil.NoContent(c)
}
type createUserRequest struct {
Email string `json:"email" binding:"required,email"`
Password string `json:"password" binding:"required,min=6,max=72"`
Role string `json:"role" binding:"required,oneof=admin user"`
}
func (h *Handler) CreateAdminUser(c *gin.Context) {
var req createUserRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid account fields"})
return
}
hash, err := auth.HashPassword(req.Password)
if err != nil {
accountError(c, err)
return
}
user, err := h.repo.CreateUser(req.Email, hash, models.Role(req.Role))
if err != nil {
accountError(c, err)
return
}
httputil.Created(c, user)
}