veza/veza-backend-api/internal/models/user.go
senke 95682e3029 feat(v0.13.3): complete - Polish Sécurité Avancée
TASK-SECADV-001: WebAuthn/Passkeys (F022)
- WebAuthn credential model, service, handler
- Registration/authentication ceremony endpoints
- CRUD operations (list, rename, delete passkeys)
- Routes: GET/POST/PUT/DELETE /auth/passkeys/*

TASK-SECADV-002: Configurable password policy (F015)
- PasswordPolicyConfig with MinLength, MaxLength, RequireUpper/Lower/Number/Special
- NewPasswordValidatorWithPolicy constructor
- PasswordPolicyFromEnv() reads env vars (PASSWORD_MIN_LENGTH, etc.)
- All character class checks now respect policy configuration

TASK-SECADV-003: Géolocalisation connexions (F025)
- GeoIPResolver interface + GeoIPService implementation
- Country/city columns added to login_history table
- LoginHistoryService.Record() performs GeoIP lookup
- GetUserHistory returns geolocation data
- GET /auth/login-history endpoint

TASK-SECADV-004: Password expiration (F016)
- password_changed_at column on users table
- CheckPasswordExpiration() method on PasswordService
- All password change/reset methods now set password_changed_at
- NewPasswordServiceWithPolicy() supports expiration days config

Migration: 971_security_advanced_v0133.sql

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-13 10:09:01 +01:00

80 lines
3.9 KiB
Go

package models
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
// User représente un utilisateur dans le système
// MIGRATION UUID: User.ID est maintenant un UUID pour cohérence Go↔Rust et alignment ORIGIN
type User struct {
ID uuid.UUID `gorm:"type:uuid;primary_key" json:"id" db:"id"`
Username string `gorm:"not null;size:30" json:"username" db:"username"`
Slug string `gorm:"size:255" json:"slug" db:"slug"`
Email string `gorm:"not null;size:255" json:"email" db:"email"`
PasswordHash string `gorm:"size:255" json:"-" db:"password_hash"`
Password string `gorm:"-" json:"password,omitempty"` // Virtual field for input
TokenVersion int `gorm:"default:0;not null" json:"token_version" db:"token_version"`
FirstName string `gorm:"size:100" json:"first_name" db:"first_name"`
LastName string `gorm:"size:100" json:"last_name" db:"last_name"`
Avatar string `gorm:"type:text" json:"avatar" db:"avatar"`
BannerURL string `gorm:"type:text" json:"banner_url" db:"banner_url"`
Bio string `gorm:"type:text" json:"bio" db:"bio"`
Location string `gorm:"size:100" json:"location" db:"location"`
Birthdate *time.Time `json:"birthdate" db:"birthdate"`
Gender string `gorm:"size:20" json:"gender" db:"gender"`
UsernameChangedAt *time.Time `json:"username_changed_at" db:"username_changed_at"`
Role string `gorm:"type:user_role;not null;default:'user'" json:"role" db:"role"`
IsActive bool `gorm:"default:true" json:"is_active" db:"is_active"`
IsVerified bool `gorm:"default:false" json:"is_verified" db:"is_verified"`
IsBanned bool `gorm:"default:false;not null" json:"is_banned" db:"is_banned"`
IsAdmin bool `gorm:"default:false" json:"is_admin" db:"is_admin"`
IsPublic bool `gorm:"default:true" json:"is_public" db:"is_public"`
LastLoginAt *time.Time `json:"last_login_at" db:"last_login_at"`
LoginCount int `gorm:"default:0;not null" json:"login_count" db:"login_count"`
PasswordChangedAt *time.Time `json:"password_changed_at,omitempty" db:"password_changed_at"` // F016: Password expiration tracking
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at" db:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at" db:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
SocialLinks string `gorm:"type:jsonb;default:'{}'" json:"social_links" db:"social_links"`
// Relations
Roles []Role `gorm:"many2many:user_roles;" json:"-"`
TrackLikes []TrackLike `gorm:"foreignKey:UserID;constraint:OnDelete:CASCADE" json:"-"`
}
// BeforeCreate hook GORM pour générer UUID si non défini
func (u *User) BeforeCreate(tx *gorm.DB) error {
if u.ID == uuid.Nil {
u.ID = uuid.New()
}
return nil
}
// TableName définit le nom de la table pour GORM
func (User) TableName() string {
return "users"
}
// SellableContent représente du contenu vendable
// MIGRATION UUID: UserID migré vers UUID
type SellableContent struct {
ID uuid.UUID `json:"id" gorm:"type:uuid;primaryKey"`
UserID uuid.UUID `gorm:"type:uuid;not null" json:"user_id" db:"user_id"`
Title string `json:"title" db:"title"`
Description string `json:"description" db:"description"`
Price float64 `json:"price" db:"price"`
IsActive bool `json:"is_active" db:"is_active"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}
// BeforeCreate hook GORM pour générer UUID si non défini
func (m *SellableContent) BeforeCreate(tx *gorm.DB) error {
if m.ID == uuid.Nil {
m.ID = uuid.New()
}
return nil
}