user

package
v0.7.13 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2024 License: MIT Imports: 30 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"`

	// 访问令牌的过期时间。
	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 Load

func Load(mod *cmfx.Module, conf *Config) *Module

Load 加载当前模块的环境

func (*Module) AddPassport

func (m *Module) AddPassport(adp Passport)

func (*Module) AddSecurityLog

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

AddSecurityLog 添加一条记录

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

func (*Module) AddSecurityLogFromContext

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

func (*Module) CreateToken

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

CreateToken 为用户 u 生成登录令牌

func (*Module) CurrentUser

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

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

func (*Module) GetUser

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

GetUser 获取指定 uid 的用户

func (*Module) GetUserByUsername

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

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

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

func (*Module) Identities

func (p *Module) Identities(uid int64) iter.Seq2[string, string]

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

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

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) Module

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

func (*Module) New

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

New 添加新用户

返回新添加的用户 ID ip 客户的 IP; ua 客户端的标记; content 添加时的备注;

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) OnLogin

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

OnLogin 注册登录事件

func (*Module) OnLogout

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

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

func (*Module) SetState

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

SetState 设置用户状态

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

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

func (*Module) URLPrefix

func (m *Module) URLPrefix() string

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 表示管理员的状态

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

func ParseState

func ParseState(v string) (State, error)

func (State) IsValid

func (s State) IsValid() bool

func (State) MarshalText

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

MarshalText encoding.TextMarshaler

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) UnmarshalText

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

UnmarshalText encoding.TextUnmarshaler

func (State) Value

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

Value driver.Valuer

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

func (u *User) BeforeInsert() error

func (*User) GetUID

func (u *User) GetUID() string

func (*User) TableName

func (*User) TableName() string

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 包测试方法

Jump to

Keyboard shortcuts

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