Documentation
¶
Overview ¶
Package user 提供用户账号的相关功能
user 提供了两种方式表示用户 ID:
- string 主要用于向公共区域展示的用户 ID;
- int64 内部使用的用户 ID,从 1 开始,由数据库自增列表示;
Index ¶
- Constants
- Variables
- func Install(mod *cmfx.Module)
- func StateValidator(v State) bool
- func UsernameValidator(id string) bool
- type Config
- type LogVO
- type Module
- func (m *Module) AddPassport(adp Passport)
- func (m *Module) AddSecurityLog(tx *orm.Tx, uid int64, ip, ua, content string) error
- func (m *Module) AddSecurityLogFromContext(tx *orm.Tx, uid int64, ctx *web.Context, content web.LocaleStringer) error
- func (m *Module) CreateToken(ctx *web.Context, u *User, p Passport) web.Responser
- func (m *Module) CurrentUser(ctx *web.Context) *User
- func (m *Module) GetUser(uid int64) (*User, error)
- func (m *Module) GetUserByUsername(username string) (*User, error)
- func (p *Module) Identities(uid int64) iter.Seq2[string, string]
- func (m *Module) LeftJoin(sql *sqlbuilder.SelectStmt, alias, on string, states []State)
- func (m *Module) Middleware(next web.HandlerFunc, method, path, router string) web.HandlerFunc
- func (m *Module) Module() *cmfx.Module
- func (m *Module) New(s State, username, password string, ip, ua, content string) (int64, error)
- func (m *Module) OnAdd(f func(*User)) context.CancelFunc
- func (m *Module) OnDelete(f func(*User)) context.CancelFunc
- func (m *Module) OnLogin(f func(*User)) context.CancelFunc
- func (m *Module) OnLogout(f func(*User)) context.CancelFunc
- func (m *Module) SetState(tx *orm.Tx, u *User, s State) error
- func (m *Module) URLPrefix() string
- type Passport
- type State
- func (s State) IsValid() bool
- func (s State) MarshalText() ([]byte, error)
- func (State) OpenAPISchema(s *openapi.Schema)
- func (State) PrimitiveType() core.PrimitiveType
- func (s *State) Scan(src any) error
- func (s State) String() string
- func (s *State) UnmarshalText(p []byte) error
- func (s State) Value() (driver.Value, error)
- type User
Constants ¶
View Source
const SpecialUserID = 0
SpecialUserID 特殊的用户 ID
表示不真实存在于用户表中的用户 ID,比如 settings 由此值表示所有用户的默认设置对象。
Variables ¶
View Source
var ( StateRule = filter.V(StateValidator, locales.InvalidValue) StateSliceRule = filter.SV[[]State](StateValidator, locales.InvalidValue) StateFilter = filter.NewBuilder(StateRule) StateSliceFilter = filter.NewBuilder(StateSliceRule) )
Functions ¶
func StateValidator ¶
Types ¶
type Config ¶
type Config struct {
// 路由地址的前缀
//
// 可以为空。
URLPrefix string `json:"urlPrefix,omitempty" xml:"urlPrefix,omitempty" yaml:"urlPrefix,omitempty"`
// 访问令牌的过期时间。
AccessExpired config.Duration `json:"accessExpired,omitempty" xml:"accessExpired,attr,omitempty" yaml:"accessExpired,omitempty"`
// 刷新令牌的过期时间,单位为秒,如果为 0 则采用用 expires * 2 作为默认值。
RefreshExpired config.Duration `json:"refreshExpired,omitempty" xml:"refreshExpired,attr,omitempty" yaml:"refreshExpired,omitempty"`
}
Config 当前模块的配置项
func (*Config) SanitizeConfig ¶
func (o *Config) SanitizeConfig() *web.FieldError
SanitizeConfig 用于检测和修正配置项的内容
type LogVO ¶
type LogVO struct {
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 Module ¶
type Module struct {
// contains filtered or unexported fields
}
Module 用户账号模块
func (*Module) AddPassport ¶
func (*Module) AddSecurityLogFromContext ¶
func (*Module) CreateToken ¶
CreateToken 为用户 u 生成登录令牌
func (*Module) CurrentUser ¶
CurrentUser 获取当前登录的用户信息
func (*Module) GetUserByUsername ¶
GetUserByUsername 根据账号名称查找用户对象
NOTE: 用户名在数据表中不具备唯一性,只能保证非删除的数据是唯一的。 所以查找的数据不包含被标记为删除的数据。
func (*Module) LeftJoin ¶
func (m *Module) LeftJoin(sql *sqlbuilder.SelectStmt, alias, on string, states []State)
LeftJoin 将 [User.State] 以 LEFT JOIN 的形式插入到 sql 语句中
alias 为 User 表的别名,on 为 LEFT JOIN 的条件。
func (*Module) Middleware ¶
func (m *Module) Middleware(next web.HandlerFunc, method, path, router string) web.HandlerFunc
Middleware 验证是否登录
func (*Module) OnAdd ¶ added in v0.7.8
func (m *Module) OnAdd(f func(*User)) context.CancelFunc
OnAdd 添加新用户时的事件
func (*Module) OnDelete ¶ added in v0.7.8
func (m *Module) OnDelete(f func(*User)) context.CancelFunc
OnDelete 删除用户时的事件
func (*Module) OnLogout ¶
func (m *Module) OnLogout(f func(*User)) context.CancelFunc
OnLogout 注册用户主动退出时的事
func (*Module) SetState ¶
SetState 设置用户状态
如果状态为非 StateNormal,那么也将会被禁止登录。
NOTE: 需要保证 u.ID、u.State 和 u.NO 是有效的。
type Passport ¶
type Passport interface {
// ID 该适配器对象的唯一标记
ID() string
// Description 对当前实例的描述信息
Description() web.LocaleStringer
// Identity uid 在当前适配器中的 ID 名称
Identity(uid int64) (identity string)
// Delete 解绑用户
Delete(uid int64) error
}
Passport 身份验证的适配器
type State ¶
type State int8
State 表示管理员的状态
func ParseState ¶
func (State) MarshalText ¶
MarshalText encoding.TextMarshaler
func (State) OpenAPISchema ¶
func (State) PrimitiveType ¶
func (State) PrimitiveType() core.PrimitiveType
func (*State) UnmarshalText ¶
UnmarshalText encoding.TextUnmarshaler
type User ¶
type User struct {
XMLName struct{} `orm:"-" json:"-" xml:"user" cbor:"-"`
Created time.Time `orm:"name(created)" json:"created" xml:"created,attr" cbor:"created" yaml:"created" comment:"created time"` // 添加时间
State State `orm:"name(state)" json:"state" xml:"state,attr" cbor:"state" yaml:"state" comment:"user state"` // 状态
// 用户的自增 ID
ID int64 `orm:"name(id);ai" json:"id" xml:"id,attr" cbor:"id" yaml:"id" comment:"user 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 ¶
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package currency 提供定义货币支付的相关功能
|
Package currency 提供定义货币支付的相关功能 |
|
passport
|
|
|
otp/code
Package code 发送一次性验证码类型的验证
|
Package code 发送一次性验证码类型的验证 |
|
otp/code/codetest
Package codetest 为 code 提供测试内容
|
Package codetest 为 code 提供测试内容 |
|
otp/totp
Package hotp 提供基于 TOTP 的 [passport.Adapter] 实现
[TOTP]: https://datatracker.ietf.org/doc/html/rfc6238
|
Package hotp 提供基于 TOTP 的 [passport.Adapter] 实现 [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 包测试方法 |
Click to show internal directories.
Click to hide internal directories.