user

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2025 License: MIT Imports: 31 Imported by: 0

Documentation

Overview

Package user 提供用户账号的相关功能

user 提供了两种方式表示用户 ID:

  • string 主要用于向公共区域展示的用户 ID;
  • int64 内部使用的用户 ID,从 1 开始,由数据库自增列表示;

Index

Constants

View Source
const SpecialUserID = 0

SpecialUserID 特殊的用户 ID

表示不真实存在于用户表中的用户 ID,比如 settings 由此值表示所有用户的默认设置对象。

Variables

Functions

func Install

func Install(mod *cmfx.Module)

Install 安装当前的环境

func StateValidator

func StateValidator(v State) bool

func UsernameValidator

func UsernameValidator(id string) bool

UsernameValidator 账号名的验证器

Types

type Config

type Config struct {
	// 路由地址的前缀
	//
	// 可以为空。
	URLPrefix string `json:"urlPrefix,omitempty" xml:"urlPrefix,omitempty" yaml:"urlPrefix,omitempty" toml:"urlPrefix,omitempty"`

	// 访问令牌的过期时间。
	AccessExpired config.Duration `` /* 127-byte string literal not displayed */

	// 刷新令牌的过期时间,单位为秒,如果为 0 则采用用 expires * 2 作为默认值。
	RefreshExpired config.Duration `` /* 131-byte string literal not displayed */
}

Config 当前模块的配置项

func (*Config) SanitizeConfig

func (o *Config) SanitizeConfig() *web.FieldError

SanitizeConfig 用于检测和修正配置项的内容

type IdentityVO added in v0.8.7

type IdentityVO struct {
	XMLName struct{} `json:"-" cbor:"-" yaml:"-" xml:"identity"`

	ID       string `json:"id" xml:"id" cbor:"id" yaml:"id" comment:"passport id"`
	Identity string `json:"identity" xml:"identity" cbor:"identity" yaml:"identity" comment:"user identity for current passport"`
	State    int8   `json:"state" xml:"state,attr" cbor:"state" yaml:"state" comment:"the state for passport and identity"`
}

type LogVO

type LogVO struct {
	XMLName   struct{}  `xml:"log" json:"-" cbor:"-" yaml:"-"`
	Content   string    `json:"content" xml:",cdata" cbor:"content" yaml:"content" comment:"log content"`
	IP        string    `json:"ip" xml:"ip,attr" cbor:"ip" yaml:"ip" comment:"log IP"`
	UserAgent string    `json:"ua" xml:"ua" cbor:"ua" yaml:"ua" comment:"log user agent"`
	Created   time.Time `xml:"created" json:"created" cbor:"created" yaml:"created" comment:"created time"`
}

type Passport

type Passport interface {
	// ID 该适配器对象的唯一标记
	ID() string

	// Description 对当前实例的描述信息
	Description() web.LocaleStringer

	// Identity uid 在当前适配器中的唯一 ID
	//
	// identity 在当前适配中的唯一 ID;
	// state identity 与适配器的状态,可以有以下几种状态:
	//  - -1 该用户未与当前适配器适配,该状态下,identity 也为空值;
	//  - 0 identity 已与当前适配器完成匹配;
	//  - 其它正整数,由适配各种定义;
	Identity(uid int64) (identity string, state int8)

	// Delete 解绑用户
	Delete(uid int64) error
}

Passport 身份验证的适配器

type State

type State int8

State 表示管理员的状态

const (
	StateNormal  State = iota // 正常
	StateLocked               // 锁定
	StateDeleted              // 删除
)

func ParseState

func ParseState(v string) (State, error)

func (State) IsValid

func (s State) IsValid() bool

func (State) MarshalCBOR added in v0.8.14

func (s State) MarshalCBOR() ([]byte, error)

func (State) MarshalText

func (s State) MarshalText() ([]byte, error)

func (State) OpenAPISchema

func (State) OpenAPISchema(s *openapi.Schema)

func (State) PrimitiveType

func (State) PrimitiveType() core.PrimitiveType

func (*State) Scan

func (s *State) Scan(src any) error

Scan sql.Scanner

func (State) String

func (s State) String() string

String fmt.Stringer

func (*State) UnmarshalCBOR added in v0.8.14

func (s *State) UnmarshalCBOR(p []byte) error

func (*State) UnmarshalText

func (s *State) UnmarshalText(p []byte) error

func (State) Value

func (s State) Value() (driver.Value, error)

Value driver.Valuer

type Statistic added in v0.8.3

type Statistic struct {
	XMLName struct{} `orm:"-" xml:"statistic" json:"-" yaml:"-" cbor:"-"`

	Online int `orm:"name(online)" xml:"online" json:"online" yaml:"online" cbor:"online"` // 在线用户数,10 分钟之内登录的用户。
	Active int `orm:"name(active)" xml:"active" json:"active" yaml:"active" cbor:"active"` // 活跃用户数,一月之内登录的用户。
	All    int `orm:"name(all)" xml:"all" json:"all" yaml:"all" cbor:"all"`                // 所有用户数
	Month  int `orm:"name(month)" xml:"month" json:"month" yaml:"month" cbor:"month"`      // 本月新增用户数
	Week   int `orm:"name(week)" xml:"week" json:"week" yaml:"week" cbor:"week"`           // 本周新增用户数
	Day    int `orm:"name(day)" xml:"day" json:"day" yaml:"day" cbor:"day"`                // 今日新增用户数
}

Statistic 用户统计信息

type User

type User struct {
	XMLName struct{} `orm:"-" json:"-" xml:"user" cbor:"-" yaml:"-"`

	State   State     `orm:"name(state)" json:"state" xml:"state,attr" cbor:"state" yaml:"state" comment:"user state"`             // 状态
	Created time.Time `orm:"name(created)" json:"created" xml:"created,attr" cbor:"created" yaml:"created" comment:"created time"` // 添加时间
	Last    time.Time `orm:"name(last)" json:"last,omitzero" xml:"last,omitzero" cbor:"last,omitzero" yaml:"last,omitempty"`       // 末次登录时间

	ID int64  `orm:"name(id);ai" json:"id" xml:"id,attr" cbor:"id" yaml:"id" comment:"user id"`            // 用户的自增 ID
	NO string `orm:"name(no);len(32);unique(no)" json:"no" xml:"no" cbor:"no" yaml:"no" comment:"user no"` // 用户编号,唯一且无序。

	// 登录信息,username 不唯一,保证在标记为删除的情况下,不影响相同值的数据添加。
	Username string `` /* 150-byte string literal not displayed */
	Password []byte `` /* 150-byte string literal not displayed */
}

func (*User) BeforeInsert

func (u *User) BeforeInsert() error

func (*User) GetUID

func (u *User) GetUID() string

func (*User) TableName

func (*User) TableName() string

type Users added in v0.13.0

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

Users 用户账号模块

func NewUsers added in v0.13.0

func NewUsers(mod *cmfx.Module, conf *Config) *Users

NewUsers 声明 Users 对象

func (*Users) AddPassport added in v0.13.0

func (m *Users) AddPassport(adp Passport)

func (*Users) AddSecurityLog added in v0.13.0

func (m *Users) AddSecurityLog(tx *orm.Tx, uid int64, ip, ua, content string) error

AddSecurityLog 添加一条记录

tx 如果为空,表示由 AddSecurityLog 直接提交数据;

func (*Users) AddSecurityLogFromContext added in v0.13.0

func (m *Users) AddSecurityLogFromContext(tx *orm.Tx, uid int64, ctx *web.Context, content web.LocaleStringer) error

func (*Users) CreateToken added in v0.13.0

func (m *Users) CreateToken(ctx *web.Context, u *User, p Passport) web.Responser

CreateToken 为用户 u 生成登录令牌

func (*Users) CurrentUser added in v0.13.0

func (m *Users) CurrentUser(ctx *web.Context) *User

CurrentUser 获取当前登录的用户信息

func (*Users) GetUser added in v0.13.0

func (m *Users) GetUser(uid int64) (*User, error)

GetUser 获取指定 uid 的用户

func (*Users) GetUserByNO added in v0.13.0

func (m *Users) GetUserByNO(no string) (*User, error)

func (*Users) GetUserByUsername added in v0.13.0

func (m *Users) GetUserByUsername(username string) (*User, error)

GetUserByUsername 根据账号名称查找用户对象

NOTE: 用户名在数据表中不具备唯一性,只能保证非删除的数据是唯一的。 所以查找的数据不包含被标记为删除的数据。

func (*Users) Identities added in v0.13.0

func (m *Users) Identities(uid int64) iter.Seq[*IdentityVO]

Identities 获取 uid 已经关联的适配器

返回值键名为验证器 id,键值为该适配器对应的账号。

func (*Users) LeftJoin added in v0.13.0

func (m *Users) LeftJoin(sql *sqlbuilder.SelectStmt, alias, on string, states []State)

LeftJoin 将 [User.State] 以 LEFT JOIN 的形式插入到 sql 语句中

alias 为 User 表的别名,on 为 LEFT JOIN 的条件。

func (*Users) Middleware added in v0.13.0

func (m *Users) Middleware(next web.HandlerFunc, method, path, router string) web.HandlerFunc

Middleware 验证是否登录

func (*Users) Module added in v0.13.0

func (m *Users) Module() *cmfx.Module

func (*Users) New added in v0.13.0

func (m *Users) New(s State, username, password string, ip, ua, content string) (*User, error)

New 添加新用户

返回新添加的用户 ID tx 如果不为空,则在此事务中执行; ip 客户的 IP; ua 客户端的标记; content 添加时的备注;

func (*Users) OnAdd added in v0.13.0

func (m *Users) OnAdd(f func(*User)) context.CancelFunc

OnAdd 添加新用户时的事件

func (*Users) OnDelete added in v0.13.0

func (m *Users) OnDelete(f func(*User)) context.CancelFunc

OnDelete 删除用户时的事件

func (*Users) OnLogin added in v0.13.0

func (m *Users) OnLogin(f func(*User)) context.CancelFunc

OnLogin 注册登录事件

func (*Users) OnLogout added in v0.13.0

func (m *Users) OnLogout(f func(*User)) context.CancelFunc

OnLogout 注册用户主动退出时的事

func (*Users) SetState added in v0.13.0

func (m *Users) SetState(tx *orm.Tx, u *User, s State) error

SetState 设置用户状态

如果状态为非 StateNormal,那么也将会被禁止登录。

NOTE: 需要保证 u.ID、u.State 和 u.NO 是有效的。

func (*Users) Statistic added in v0.13.0

func (m *Users) Statistic(now time.Time) (*Statistic, error)

Statistic 统计用户信息

now 为当前时间,用于往前推荐对应时间的各类数据。

func (*Users) URLPrefix added in v0.13.0

func (m *Users) URLPrefix() string

Directories

Path Synopsis
Package currency 提供定义货币支付的相关功能
Package currency 提供定义货币支付的相关功能
passport
fido/passkey
Package passkey 提供 webauthn、windows hello、faceID 等的服务端 [webauthn]: https://webauthn.io/
Package passkey 提供 webauthn、windows hello、faceID 等的服务端 [webauthn]: https://webauthn.io/
otp/code
Package code 发送一次性验证码类型的验证
Package code 发送一次性验证码类型的验证
otp/code/codetest
Package codetest 为 code 提供测试内容
Package codetest 为 code 提供测试内容
otp/totp
Package totp 提供基于 TOTP 的 [passport.Passport] 实现 [TOTP]: https://datatracker.ietf.org/doc/html/rfc6238
Package totp 提供基于 TOTP 的 [passport.Passport] 实现 [TOTP]: https://datatracker.ietf.org/doc/html/rfc6238
utils
Package utils 提供 passport 的一些工具
Package utils 提供 passport 的一些工具
Package rbac 简单的 RBAC 权限规则实现
Package rbac 简单的 RBAC 权限规则实现
Package settings 与用户关联的设置信息
Package settings 与用户关联的设置信息
Package usertest 提供简单的 user 包测试方法
Package usertest 提供简单的 user 包测试方法

Jump to

Keyboard shortcuts

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