mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-08 14:26:37 +08:00
feat: make user limit configurable from admin user management (#18)
Store the per-instance user cap in settings (default 20) and expose GET/PUT /admin/users/limit endpoints. The user management page now lets admins view and update the limit without touching system settings. Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: truewhile <truewhile@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/truewhile/MeBox/internal/repository"
|
||||
)
|
||||
|
||||
const (
|
||||
// SettingMaxUsers stores the per-instance user cap in the settings table.
|
||||
SettingMaxUsers = "user.max_users"
|
||||
|
||||
// DefaultMaxUsers is used when the setting is missing or invalid.
|
||||
DefaultMaxUsers = 20
|
||||
|
||||
// MaxUsersHardCap prevents absurd values from admin input.
|
||||
MaxUsersHardCap = 10000
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidMaxUsers = errors.New("invalid max users")
|
||||
)
|
||||
|
||||
// LoadMaxUsers reads the configured user cap, falling back to DefaultMaxUsers.
|
||||
func LoadMaxUsers(ctx context.Context, repo *repository.Container) (int, error) {
|
||||
if repo == nil || repo.Setting == nil {
|
||||
return DefaultMaxUsers, nil
|
||||
}
|
||||
raw, err := repo.Setting.Get(ctx, SettingMaxUsers)
|
||||
if err != nil {
|
||||
return DefaultMaxUsers, err
|
||||
}
|
||||
return parseMaxUsersSetting(raw), nil
|
||||
}
|
||||
|
||||
func parseMaxUsersSetting(raw string) int {
|
||||
n, err := strconv.Atoi(strings.TrimSpace(raw))
|
||||
if err != nil || n < 1 {
|
||||
return DefaultMaxUsers
|
||||
}
|
||||
if n > MaxUsersHardCap {
|
||||
return MaxUsersHardCap
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// ValidateMaxUsers checks an admin-provided cap before persisting it.
|
||||
func ValidateMaxUsers(n int) error {
|
||||
if n < 1 {
|
||||
return fmt.Errorf("%w: must be at least 1", ErrInvalidMaxUsers)
|
||||
}
|
||||
if n > MaxUsersHardCap {
|
||||
return fmt.Errorf("%w: must be at most %d", ErrInvalidMaxUsers, MaxUsersHardCap)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SaveMaxUsers persists the user cap in settings.
|
||||
func SaveMaxUsers(ctx context.Context, repo *repository.Container, n int) error {
|
||||
if err := ValidateMaxUsers(n); err != nil {
|
||||
return err
|
||||
}
|
||||
if repo == nil || repo.Setting == nil {
|
||||
return errors.New("settings repository unavailable")
|
||||
}
|
||||
return repo.Setting.Set(ctx, SettingMaxUsers, strconv.Itoa(n))
|
||||
}
|
||||
Reference in New Issue
Block a user