replace static status definition by const

This commit is contained in:
venture
2026-11-14 09:57:52 +01:00
parent 276a9fe320
commit f16363cb60
10 changed files with 65 additions and 48 deletions
+6
View File
@@ -24,6 +24,12 @@ type Account struct {
CreatedAt int64
}
const (
AccountStateActive = "active"
AccountStateLocked = "locked"
AccountStateDisabled = "disabled"
)
var (
ErrInvalidCredentials = errors.New("identifiants invalides")
ErrAccountLocked = errors.New("compte verrouillé")
+6 -5
View File
@@ -12,6 +12,7 @@ import (
"mediarr/database"
"mediarr/ident"
"mediarr/identity"
)
const (
@@ -57,7 +58,7 @@ func (s *Service) CreateSession(ctx context.Context, params CreateSessionParams)
}
return nil, "", err
}
if principal.Status != "active" {
if principal.Status != identity.StatusActive {
return nil, "", ErrInvalidSession
}
@@ -67,10 +68,10 @@ func (s *Service) CreateSession(ctx context.Context, params CreateSessionParams)
switch {
case err == nil:
switch account.State {
case "active":
case "disabled":
case AccountStateActive:
case AccountStateDisabled:
return nil, "", ErrAccountDisabled
case "locked":
case AccountStateLocked:
if !account.LockedUntil.Valid || account.LockedUntil.Int64 > now {
return nil, "", ErrAccountLocked
}
@@ -152,7 +153,7 @@ func (s *Service) ValidateSession(ctx context.Context, token string) (*Session,
}
return nil, nil, err
}
if user.Principal.Status != "active" || user.Account.State == "disabled" {
if user.Principal.Status != identity.StatusActive || user.Account.State == AccountStateDisabled {
return nil, nil, ErrInvalidSession
}
+3 -3
View File
@@ -149,8 +149,8 @@ func TestGuestSession(t *testing.T) {
if authenticated.Account.Login != "jean" {
t.Fatalf("login = %q, attendu jean", authenticated.Account.Login)
}
if authenticated.Account.State != "active" {
t.Fatalf("state = %q, attendu active", authenticated.Account.State)
if authenticated.Account.State != AccountStateActive {
t.Fatalf("state = %q, attendu %q", authenticated.Account.State, AccountStateActive)
}
}
@@ -161,7 +161,7 @@ func TestCreateSessionRejectsInactivePrincipal(t *testing.T) {
created := mustCreateUser(t, service, "alice", "correct horse battery staple", identity.RoleMember)
_, err := service.db.WriteQueries.UpdatePrincipalStatus(ctx, database.UpdatePrincipalStatusParams{
Status: "disabled",
Status: identity.StatusDisabled,
ID: created.Principal.ID,
})
if err != nil {
+8 -8
View File
@@ -70,7 +70,7 @@ func (s *Service) CreateUser(ctx context.Context, params CreateUserParams) (*Aut
ID: userID,
Kind: identity.KindUser,
Name: display,
Status: "active",
Status: identity.StatusActive,
CreatedAt: now,
}); err != nil {
return err
@@ -89,7 +89,7 @@ func (s *Service) CreateUser(ctx context.Context, params CreateUserParams) (*Aut
Valid: true,
},
MustChangePassword: 0,
State: "active",
State: AccountStateActive,
FailedAttempts: 0,
CreatedAt: now,
}); err != nil {
@@ -135,23 +135,23 @@ func (s *Service) Authenticate(ctx context.Context, login, password string) (*Au
}
return nil, err
}
if principal.Status != "active" {
if principal.Status != identity.StatusActive {
return nil, ErrInvalidCredentials
}
now := time.Now().UnixMilli()
switch account.State {
case "disabled":
case AccountStateDisabled:
return nil, ErrAccountDisabled
case "locked":
case AccountStateLocked:
if !account.LockedUntil.Valid || account.LockedUntil.Int64 > now {
return nil, ErrAccountLocked
}
if _, err := s.db.WriteQueries.UnlockAccount(ctx, account.PrincipalID); err != nil {
return nil, err
}
account.State = "active"
account.State = AccountStateActive
account.FailedAttempts = 0
account.LockedUntil = sql.NullInt64{}
}
@@ -177,7 +177,7 @@ func (s *Service) Authenticate(ctx context.Context, login, password string) (*Au
if err != nil {
return nil, err
}
if updated.State == "locked" {
if updated.State == AccountStateLocked {
return nil, ErrAccountLocked
}
return nil, ErrInvalidCredentials
@@ -241,7 +241,7 @@ func (s *Service) loadAuthenticatedUser(ctx context.Context, id ident.UUID) (*Au
Account: Account{
PrincipalID: id,
Login: principal.Name,
State: "active",
State: AccountStateActive,
},
}, nil
default:
+9 -9
View File
@@ -32,11 +32,11 @@ func TestCreateUser(t *testing.T) {
if created.Principal.Name != "Alice" {
t.Fatalf("name = %q, attendu Alice", created.Principal.Name)
}
if created.Principal.Status != "active" {
t.Fatalf("status = %q, attendu active", created.Principal.Status)
if created.Principal.Status != identity.StatusActive {
t.Fatalf("status = %q, attendu %q", created.Principal.Status, identity.StatusActive)
}
if created.Account.State != "active" {
t.Fatalf("état du compte = %q, attendu active", created.Account.State)
if created.Account.State != AccountStateActive {
t.Fatalf("état du compte = %q, attendu %q", created.Account.State, AccountStateActive)
}
if len(created.Principal.Groups) != 1 || created.Principal.Groups[0].Name != "members" {
t.Fatalf("groupes = %+v, attendu members", created.Principal.Groups)
@@ -198,8 +198,8 @@ func TestAuthenticateLocksAccountAfterTooManyFailures(t *testing.T) {
if err != nil {
t.Fatalf("GetAccount : %v", err)
}
if account.State != "locked" {
t.Fatalf("state = %q, attendu locked", account.State)
if account.State != AccountStateLocked {
t.Fatalf("state = %q, attendu %q", account.State, AccountStateLocked)
}
if !account.LockedUntil.Valid || account.LockedUntil.Int64 <= time.Now().UnixMilli() {
t.Fatalf("locked_until = %+v, attendu dans le futur", account.LockedUntil)
@@ -232,8 +232,8 @@ func TestAuthenticateUnlocksExpiredAccount(t *testing.T) {
if err != nil {
t.Fatalf("GetAccount : %v", err)
}
if account.State != "active" {
t.Fatalf("state = %q, attendu active", account.State)
if account.State != AccountStateActive {
t.Fatalf("state = %q, attendu %q", account.State, AccountStateActive)
}
if account.FailedAttempts != 0 {
t.Fatalf("failed_attempts = %d, attendu 0", account.FailedAttempts)
@@ -247,7 +247,7 @@ func TestAuthenticateRejectsDisabledAccount(t *testing.T) {
created := mustCreateUser(t, service, "alice", "correct horse battery staple", identity.RoleMember)
_, err := service.db.WriteQueries.UpdateAccountState(ctx, database.UpdateAccountStateParams{
State: "disabled",
State: AccountStateDisabled,
PrincipalID: created.Principal.ID,
})
if err != nil {