user

package
v0.33.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUsernameExists = errors.New("username already exists")
	ErrEmailExists    = errors.New("email already exists")
)

注册/建号的**业务拒绝**哨兵错误(typed):撞名/撞邮箱是客户端可修复的 冲突(409),不是服务端故障。2026-11-02 实录:Create 此前返回普通 error、 handler 一律映射 500「服务器内部错误」⇒ 注册撞名在前端只显示内部错误, 用户无从得知该换用户名。文案保持 "already exists"(既有测试断言该子串)。

Functions

func GenerateRandomKey

func GenerateRandomKey() string

GenerateRandomKey 生成32位随机字符串(16字节 hex,crypto/rand 经 kit/uidgen 统一收口; crypto/rand 不可用属进程级灾难,uidgen 以 panic 上抛而非返回 error)

func GetKeyPrefix

func GetKeyPrefix(key string) string

GetKeyPrefix 获取密钥的前缀(用于展示)

func HashAPIKey

func HashAPIKey(key string) string

HashAPIKey 计算密钥的 SHA256 哈希

Types

type APIKeyData

type APIKeyData struct {
	ID         uint
	Name       string
	Prefix     string
	LastUsedAt *time.Time
	LastUsedIP string
	ExpiresAt  *time.Time
	CreatedAt  *time.Time
	Revoked    bool
	// RevokedAt 撤销时间(软撤销:DB 一直有值,但此前**响应里没有这个字段**
	// ⇒ 用户/前端看不到「这把 Key 是什么时候被撤销的」⇒ 审计信息断在回传面。
	// 2026-10-30 真机实录补齐;nil = 未撤销。
	RevokedAt *time.Time
	// Scopes 权限范围(空 = 全量)。
	Scopes []string
}

APIKeyData API Key 数据

type CreateAPIKeyReq

type CreateAPIKeyReq struct {
	Name          string
	ExpiresAt     *time.Time
	ExpiresInDays *int64
	// Scopes 权限范围(2026-10-30):该 Key 被允许的能力面;**空 = 全量**
	// (与 scopes 机制引入前完全一致 ⇒ 老 Key 无需重建)。
	Scopes []string
}

CreateAPIKeyReq 创建 API Key 请求

type CreateAPIKeyResp

type CreateAPIKeyResp struct {
	Key    string
	APIKey *APIKeyData
}

CreateAPIKeyResp 创建 API Key 响应

type CreateUserReq

type CreateUserReq struct {
	Username string
	Email    string
	Password string
	Nickname string
}

type DefaultsProvider

type DefaultsProvider interface {
	// DefaultStorageQuota 用户存储配额默认值(字节,0 = 不限)
	DefaultStorageQuota(ctx context.Context) int64
	// DefaultUploadSize 单次上传大小默认上限(字节,0 = 不限)
	DefaultUploadSize(ctx context.Context) int64
	// AllowUserRegistration 是否开放注册(管理后台"用户配置"持久化值优先)
	AllowUserRegistration(ctx context.Context) bool
}

DefaultsProvider 系统级用户默认值来源(管理后台持久化配置优先,回退 yaml)。 消费侧接口:bootstrap 注入 admin 域适配器;未注入时回退全局 conf—— 本域不依赖 admin 包(跨域依赖收口为装配点)。

type QuotaExceededError

type QuotaExceededError struct{ Msg string }

QuotaExceededError 存储配额超限(handler 侧可断言后返回 CodeStorageQuota)

func (*QuotaExceededError) ErrCode

func (e *QuotaExceededError) ErrCode() int

func (*QuotaExceededError) Error

func (e *QuotaExceededError) Error() string

type Service

type Service struct {
	// contains filtered or unexported fields
}

func NewService

func NewService(gormDB *gorm.DB) *Service

func (*Service) ChangePassword

func (s *Service) ChangePassword(ctx context.Context, userID uint, oldPassword, newPassword string) error

ChangePassword 修改密码

func (*Service) CheckQuota

func (s *Service) CheckQuota(ctx context.Context, userID uint, addBytes int64) error

CheckQuota 存储配额强制检查(上传前调用)。 生效优先级:用户级 MaxStorageQuota > 系统默认 user.user_storage_quota;0 = 不限。 已用配额以 file_codes 表实时聚合为准(比 users.total_storage 计数器更抗漂移)。

func (*Service) Create

func (s *Service) Create(ctx context.Context, req *CreateUserReq) (*model.UserResp, error)

func (*Service) CreateAPIKey

func (s *Service) CreateAPIKey(ctx context.Context, userID uint, req *CreateAPIKeyReq) (*CreateAPIKeyResp, error)

CreateAPIKey 为用户创建新的 API Key

func (*Service) Delete

func (s *Service) Delete(ctx context.Context, id uint) error

func (*Service) DeleteAPIKey

func (s *Service) DeleteAPIKey(ctx context.Context, userID, id uint) error

DeleteAPIKey 撤销指定的 API Key

func (*Service) GetByID

func (s *Service) GetByID(ctx context.Context, id uint) (*model.UserResp, error)

func (*Service) GetStats

func (s *Service) GetStats(ctx context.Context, userID uint) (*model.UserStats, error)

GetStats 获取用户统计信息

func (*Service) GetUploadSizeCap

func (s *Service) GetUploadSizeCap(ctx context.Context, userID uint) int64

GetUploadSizeCap 生效的单次上传大小上限(接线:此前 users.max_upload_size 字段存在但上传链路从不读取,管理员给用户降限无实际效果)。 优先级:用户级 MaxUploadSize > 系统默认 user.user_upload_size;0 = 不限。 用户不存在/查询失败返回 0(不阻断,配额类是运营约束)。

func (*Service) GetUserFiles

func (s *Service) GetUserFiles(ctx context.Context, userID uint, page, pageSize int) (*usermodel.UserFileList, error)

GetUserFiles 获取用户文件列表(支持分页)

func (*Service) List

func (s *Service) List(ctx context.Context, page, pageSize int) ([]*model.UserResp, int64, error)

func (*Service) ListAPIKeys

func (s *Service) ListAPIKeys(ctx context.Context, userID uint) ([]*APIKeyData, error)

ListAPIKeys 获取用户的全部 API Key 列表(包含已撤销)

func (*Service) Login

func (s *Service) Login(ctx context.Context, username, password string) (*model.UserResp, string, error)

Login 用户登录

func (*Service) LoginByEmail

func (s *Service) LoginByEmail(ctx context.Context, email, password string) (*model.UserResp, string, error)

LoginByEmail 通过邮箱登录

func (*Service) RegistrationAllowed added in v0.15.1

func (s *Service) RegistrationAllowed(ctx context.Context) bool

RegistrationAllowed 是否开放注册(系统级设置来源:注入 provider 的管理后台 持久化值优先,未注入回退 yaml)。gen/handler/user 的 Register 入口经此消费, 不再直接 import admin 域包级单例(跨面依赖收口为装配点,2026-10-10)。

func (*Service) RevokeAllAPIKeys

func (s *Service) RevokeAllAPIKeys(ctx context.Context, userID uint) (int64, error)

RevokeAllAPIKeys 一键吊销用户全部有效 API Key(应急止损),返回吊销数量。

func (*Service) SetDefaultsProvider

func (s *Service) SetDefaultsProvider(p DefaultsProvider)

SetDefaultsProvider 注入系统级默认值来源(bootstrap 装配调用)。

func (*Service) SetStorageQuota

func (s *Service) SetStorageQuota(ctx context.Context, userID uint, quota int64) (*model.UserResp, error)

SetStorageQuota 管理员设置用户存储配额(0 = 不限,回退系统默认)

func (*Service) SetUploadSize

func (s *Service) SetUploadSize(ctx context.Context, userID uint, size int64) (*model.UserResp, error)

SetUploadSize 管理员设置用户单次上传大小上限(0 = 用系统默认)

func (*Service) SystemDefaultStorageQuota added in v0.15.1

func (s *Service) SystemDefaultStorageQuota(ctx context.Context) int64

SystemDefaultStorageQuota 系统级默认存储配额(字节,0 = 不限)。 与域内配额闸(defaultStorageQuota)同源;供 handler 展示层消费 (用户级 max_storage_quota 覆盖仍由调用方自行优先)。

func (*Service) Update

func (s *Service) Update(ctx context.Context, id uint, req *UpdateUserReq) (*model.UserResp, error)

func (*Service) UpdateUserStats

func (s *Service) UpdateUserStats(userID uint, statsType string, value int64) error

UpdateUserStats 更新用户统计信息。 用 SQL 表达式原子自增(此前为 GetByID+Update 读改写,并发下丢更新)。

type UpdateUserReq

type UpdateUserReq struct {
	Nickname string
	Avatar   string
	Role     string
	Status   string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL