Files
template-vitrine/backend/internal/modules/users/service.go
T
Xor290 919c807004
ci-api / test (push) Failing after 8m7s
ci-web / test (push) Failing after 5m5s
chore: build
2026-09-20 12:18:33 +02:00

128 lines
3.1 KiB
Go

package users
import (
"context"
"errors"
"fmt"
"github.com/google/uuid"
"backend/internal/platform/config"
"backend/internal/platform/security"
)
// ErrCustomerAccountsDisabled is returned by Create when trying to create a
// customer-role user while neither customer login nor registration is
// enabled -- such an account would have no way to sign in.
var ErrCustomerAccountsDisabled = errors.New("customer accounts are disabled")
// AccountsGate lets the users module check the site-wide customer-accounts
// toggles without importing the site module directly.
type AccountsGate interface {
CustomerAccountsAvailable(ctx context.Context) (bool, error)
}
type Service struct {
repo Repository
gate AccountsGate
cfg config.SeedConfig
}
func NewService(repo Repository, gate AccountsGate, cfg config.SeedConfig) *Service {
return &Service{repo: repo, gate: gate, cfg: cfg}
}
func (s *Service) List(ctx context.Context) ([]*User, error) {
return s.repo.List(ctx)
}
func (s *Service) FindByID(ctx context.Context, id uuid.UUID) (*User, error) {
return s.repo.FindByID(ctx, id)
}
func (s *Service) CountByRole(ctx context.Context, role string) (int64, error) {
return s.repo.CountByRole(ctx, role)
}
func (s *Service) FindByUsername(ctx context.Context, username string) (*User, error) {
return s.repo.FindByUsername(ctx, username)
}
func (s *Service) Create(ctx context.Context, username, password, role string) (*User, error) {
if role != RoleAdmin && role != RoleCustomer {
return nil, fmt.Errorf("invalid role %q", role)
}
if role == RoleAdmin {
count, err := s.repo.CountByRole(ctx, RoleAdmin)
if err != nil {
return nil, err
}
if count >= s.cfg.AdminNumber {
return nil, ErrAdminLimitReached
}
}
if role == RoleCustomer {
available, err := s.gate.CustomerAccountsAvailable(ctx)
if err != nil {
return nil, err
}
if !available {
return nil, ErrCustomerAccountsDisabled
}
}
hash, err := security.HashPassword(password)
if err != nil {
return nil, fmt.Errorf("hash password: %w", err)
}
user := &User{
ID: uuid.New(),
Username: username,
PasswordHash: hash,
Role: role,
IsActive: true,
}
if err := s.repo.Create(ctx, user); err != nil {
return nil, err
}
return user, nil
}
// UpdateProfile updates the mutable, non-security fields of a user.
func (s *Service) UpdateProfile(ctx context.Context, id uuid.UUID, username string, isActive bool) (*User, error) {
user, err := s.repo.FindByID(ctx, id)
if err != nil {
return nil, err
}
user.Username = username
user.IsActive = isActive
if err := s.repo.Update(ctx, user); err != nil {
return nil, err
}
return user, nil
}
func (s *Service) SetPassword(ctx context.Context, id uuid.UUID, newPassword string) error {
user, err := s.repo.FindByID(ctx, id)
if err != nil {
return err
}
hash, err := security.HashPassword(newPassword)
if err != nil {
return fmt.Errorf("hash password: %w", err)
}
user.PasswordHash = hash
return s.repo.Update(ctx, user)
}
func (s *Service) Delete(ctx context.Context, id uuid.UUID) error {
return s.repo.Delete(ctx, id)
}