rbac

package module
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2025 License: MIT Imports: 7 Imported by: 61

README

rbac

为 starter 提供对 RBAC (Role-Based Access Control) 的支持

Documentation

Index

Constants

View Source
const (
	ActionLogin          = "login"
	ActionSignUp         = "sign-up"
	ActionResetPassword  = "reset-password"
	ActionChangePassword = "change-password"
	ActionSendCode       = "send-code"
)

定义几种常用的授权动作

View Source
const (
	MechanismPassword = "password"
	MechanismEmail    = "email"
	MechanismPhone    = "sms"
	MechanismSMS      = "sms"
)

定义几种常用的验证机制

View Source
const (
	StepInit     = "init"     // 初始化
	StepPrepare  = "prepare"  // 准备
	StepHelp     = "help"     // 获取帮助信息
	StepSendCode = "sendcode" // 发送验证码
	StepApply    = "apply"    // 应用
	StepAuth     = "auth"     // 验证与授权
)

定义几个常用的验证步骤

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthDTO

type AuthDTO struct {
	BaseDTO

	// 采用的验证机制
	Mechanism string `json:"mechanism"`

	// 最终要执行的动作,
	// 例如: login(登录), sign_up(注册), reset_password(重置密码), 等
	Action string `json:"action"`

	// 表示验证的步骤
	Step string `json:"step"`

	// 将要验证的账号,
	// 它可能是 UserName | UserID | UserEmail | UserPhone | ...
	Account string `json:"account"`

	// 将要验证的机密内容, 例如:密码, 等...
	Secret lang.Base64 `json:"secret"`

	// 其它扩展属性
	Parameters map[string]string `json:"parameters"`
}

AuthDTO 用于身份认证

type AuthService

type AuthService interface {
	Handle(c context.Context, action string, a []*AuthDTO) ([]*AuthDTO, error)
}

AuthService 是针对 AuthDTO 的服务

type AuthVO

type AuthVO struct {
	BaseVO

	Auth []*AuthDTO `json:"auth"` // 用于验证的信息
}

AuthVO ...

type AvatarDTO added in v0.0.7

type AvatarDTO struct {
	ID AvatarID `json:"id"`

	BaseDTO

	Label string `json:"label"`
	Name  string `json:"name"`
	URL   string `json:"url"`
}

AvatarDTO 表示一个头像资源

type AvatarID added in v0.0.7

type AvatarID int64

AvatarID 是 Avatar 的实体 ID

type AvatarQuery added in v0.0.7

type AvatarQuery struct {
	Conditions Conditions
	Pagination Pagination
	All        bool // 查询全部条目
}

AvatarQuery ...

type AvatarService added in v0.0.7

type AvatarService interface {
	Find(c context.Context, id AvatarID) (*AvatarDTO, error)
	List(c context.Context, q *AvatarQuery) ([]*AvatarDTO, error)
	ListAll(c context.Context) ([]*AvatarDTO, error)
}

AvatarService ...

type AvatarVO added in v0.0.7

type AvatarVO struct {
	BaseVO

	Avatars []*AvatarDTO `json:"avatars"`
}

AvatarVO ...

type BaseDTO

type BaseDTO struct {
	UUID lang.UUID `json:"uuid"`

	CreatedAt lang.Time `json:"created_at"`
	UpdatedAt lang.Time `json:"updated_at"`
	DeletedAt lang.Time `json:"deleted_at"`

	Group   GroupID `json:"group"` // 该对象的默认权限分组
	Owner   UserID  `json:"owner"`
	Creator UserID  `json:"creator"`
	Updater UserID  `json:"updater"`
}

BaseDTO 是基本的 DTO

type BaseVO

type BaseVO struct {
	Status     int         `json:"status"`
	Message    string      `json:"message"`
	Error      string      `json:"error"`
	Time       time.Time   `json:"time"`
	Timestamp  lang.Time   `json:"timestamp"`
	Pagination *Pagination `json:"pagination"`
}

BaseVO 是通用的基本 VO 结构

func (*BaseVO) GetVO

func (inst *BaseVO) GetVO() *BaseVO

GetVO 实现 BaseGetter

type Conditions

type Conditions struct {
	Query string   `json:"query"`
	Args  []string `json:"args"`
}

Conditions 表示查询条件, 取值参考 gorm.DB.Where()

type CurrentUser added in v0.0.4

type CurrentUser struct {
	User      UserID       `json:"user"`
	Nickname  string       `json:"nickname"`
	Language  string       `json:"language"` // 该用户的本地化语言,取值示例:("zh_cn")
	Avatar    string       `json:"avatar"`
	Roles     RoleNameList `json:"roles"`
	StartedAt lang.Time    `json:"started_at"` // 生效时间
	ExpiredAt lang.Time    `json:"expired_at"` // 过期时间

	Properties map[string]string `json:"properties"`
}

CurrentUser 表示当前用户

func (*CurrentUser) GetProperty added in v0.0.8

func (inst *CurrentUser) GetProperty(name string) string

GetProperty ...

func (*CurrentUser) SetPeriod added in v0.0.8

func (inst *CurrentUser) SetPeriod(from time.Time, maxAge time.Duration)

SetPeriod 以 (t0 + max_age) 的形式设置 StartedAt & ExpiredAt

func (*CurrentUser) SetProperty added in v0.0.8

func (inst *CurrentUser) SetProperty(name, value string)

SetProperty ...

type EmailAddress

type EmailAddress string

EmailAddress 表示 'user@domain' 形式的邮件地址

func (EmailAddress) String

func (addr EmailAddress) String() string

type EmailAddressDTO

type EmailAddressDTO struct {
	ID EmailAddressID `json:"id"`

	BaseDTO

	Address EmailAddress `json:"address"`
}

EmailAddressDTO ...

type EmailAddressID

type EmailAddressID int64

EmailAddressID ...

type EmailAddressQuery

type EmailAddressQuery struct {
	Conditions Conditions
	Pagination Pagination
	All        bool // 查询全部条目
}

EmailAddressQuery 查询参数

type EmailAddressService

EmailAddressService ...

type FullPhoneNumber

type FullPhoneNumber PhoneNumber

FullPhoneNumber 表示完整的电话号码, like: "+86-123-4567-8901"

func ParseFullPhoneNumber

func ParseFullPhoneNumber(str string) (FullPhoneNumber, error)

ParseFullPhoneNumber 把输入的字符串解析为完整的电话号码

func (FullPhoneNumber) Normalize

func (num FullPhoneNumber) Normalize() FullPhoneNumber

Normalize 标准化

func (FullPhoneNumber) Parts

Parts 拆解为:区号 + SimplePhoneNumber

func (FullPhoneNumber) Pure

func (num FullPhoneNumber) Pure() PurePhoneNumber

Pure 转换为纯号码形式

func (FullPhoneNumber) String

func (num FullPhoneNumber) String() string

type GroupDTO added in v0.0.2

type GroupDTO struct {
	ID GroupID `json:"id"`

	BaseDTO

	Name        GroupName `json:"name"`
	Label       string    `json:"label"`
	Description string    `json:"description"`

	Roles   RoleNameList `json:"roles"`
	Enabled bool         `json:"enabled"`
}

GroupDTO 表示 Group 的 REST 网络对象

type GroupID added in v0.0.2

type GroupID int64

GroupID 是通用的用户组标识符

func ParseGroupID added in v0.0.2

func ParseGroupID(s string) (GroupID, error)

ParseGroupID 把字符串解析为 GroupID

func (GroupID) String added in v0.0.2

func (id GroupID) String() string

type GroupName added in v0.0.2

type GroupName string

GroupName 表示用户分组名称

func (GroupName) String added in v0.0.2

func (name GroupName) String() string

type GroupQuery added in v0.0.2

type GroupQuery struct {
	Conditions Conditions
	Pagination Pagination
	All        bool // 查询全部条目
}

GroupQuery 是 Group 的查询参数

type GroupService added in v0.0.2

type GroupService interface {
	Insert(c context.Context, o *GroupDTO) (*GroupDTO, error)
	Update(c context.Context, id GroupID, o *GroupDTO) (*GroupDTO, error)
	Delete(c context.Context, id GroupID) error

	Find(c context.Context, id GroupID) (*GroupDTO, error)
	List(c context.Context, q *GroupQuery) ([]*GroupDTO, error)
}

GroupService 是针对 GroupDTO 的服务

type GroupVO added in v0.0.2

type GroupVO struct {
	BaseVO

	Groups []*GroupDTO `json:"groups"`
}

GroupVO ...

type ModuleInfo added in v0.0.12

type ModuleInfo interface {
	Name() string
	Version() string
	Revision() int
}

func GetModuleInfo added in v0.0.12

func GetModuleInfo() ModuleInfo

type Pagination

type Pagination struct {
	Page  int64 `json:"page"`  // 页码, first=1
	Size  int   `json:"size"`  // 页大小
	Total int64 `json:"total"` // 所有页面的条目总数
}

Pagination 是通用的分页参数

func (*Pagination) Limit

func (inst *Pagination) Limit() int64

Limit 用于 SQL 查询的页面大小

func (*Pagination) Offset

func (inst *Pagination) Offset() int64

Offset 用于 SQL 查询的条目位置

type PermissionCache

type PermissionCache interface {
	Clear()
	Find(c context.Context, want *PermissionDTO) (*PermissionDTO, error)
}

PermissionCache 是一个带缓存的 Permission 查询接口

type PermissionChecker added in v0.0.12

type PermissionChecker interface {
	GetSubjectContext() *SubjectContext

	// 添加用户具有的角色
	AddRolesHad(roles ...RoleName) PermissionChecker

	// 添加可接受访问的用户角色
	AddRolesAccepted(roles ...RoleName) PermissionChecker

	// 接受匿名者访问
	AcceptAnonymous() PermissionChecker

	// 检查目标对象的访问权限
	CheckObject(o *BaseDTO) error

	// 完成最终的检查
	Check() error
}

type PermissionDTO

type PermissionDTO struct {
	ID PermissionID `json:"id"`

	BaseDTO

	Method      string       `json:"method"`
	Path        string       `json:"path"`
	AcceptRoles RoleNameList `json:"accept_roles"`
	Enabled     bool         `json:"enabled"`
}

PermissionDTO 表示 Permission 的 REST 网络对象

type PermissionID

type PermissionID int64

PermissionID 是 Permission 的实体 ID

type PermissionQuery

type PermissionQuery struct {
	Conditions Conditions
	Pagination Pagination
	All        bool // 查询全部条目
}

PermissionQuery 查询参数

type PermissionService

type PermissionService interface {
	Insert(c context.Context, o *PermissionDTO) (*PermissionDTO, error)
	Update(c context.Context, id PermissionID, o *PermissionDTO) (*PermissionDTO, error)
	Delete(c context.Context, id PermissionID) error

	Find(c context.Context, id PermissionID) (*PermissionDTO, error)
	List(c context.Context, q *PermissionQuery) ([]*PermissionDTO, error)
	ListAll(c context.Context) ([]*PermissionDTO, error)

	GetCache() PermissionCache
}

PermissionService 是针对 PermissionDTO 的服务

type PermissionVO

type PermissionVO struct {
	BaseVO

	Permissions []*PermissionDTO `json:"permissions"`
}

PermissionVO ...

type PhoneNumber

type PhoneNumber string

PhoneNumber 表示电话号码

func (PhoneNumber) String

func (num PhoneNumber) String() string

type PhoneNumberDTO

type PhoneNumberDTO struct {
	ID PhoneNumberID `json:"id"`

	BaseDTO

	RegionCode2  RegionPhoneCode   `json:"region"`
	SimpleNumber SimplePhoneNumber `json:"simple_number"`
	FullNumber   FullPhoneNumber   `json:"full_number"`
}

PhoneNumberDTO ...

type PhoneNumberID

type PhoneNumberID int64

PhoneNumberID ...

type PhoneNumberQuery

type PhoneNumberQuery struct {
	Conditions Conditions
	Pagination Pagination
	All        bool // 查询全部条目
}

PhoneNumberQuery 查询参数

type PhoneNumberService

PhoneNumberService ...

type PurePhoneNumber

type PurePhoneNumber PhoneNumber

PurePhoneNumber 表示完整(且纯粹)的电话号码, like: "8612345678901"

func (PurePhoneNumber) Normalize

func (num PurePhoneNumber) Normalize() PurePhoneNumber

Normalize 标准化

func (PurePhoneNumber) String

func (num PurePhoneNumber) String() string

type RegionCode2

type RegionCode2 string

RegionCode2 是 ISO 3166-1 标准的二字节地区码 例如:中国(CN), 法国(FR), 俄国(RU), 美国(US), 英国(GB)

func (RegionCode2) Normalize

func (code RegionCode2) Normalize() RegionCode2

Normalize 标准化代码

func (RegionCode2) String

func (code RegionCode2) String() string

type RegionCode3

type RegionCode3 string

RegionCode3 是 ISO 3166-1 标准的三字节地区码 例如:中国(CHN), 法国(FRA), 俄国(RUS), 美国(USA), 英国(GBR)

func (RegionCode3) Normalize

func (code RegionCode3) Normalize() RegionCode3

Normalize 标准化代码

func (RegionCode3) String

func (code RegionCode3) String() string

type RegionDTO

type RegionDTO struct {
	ID RegionID `json:"id"`

	BaseDTO

	FlagURL     string          `json:"flag_url"`    // 国旗(或区旗)图标的URL
	DisplayName string          `json:"label"`       // 显示名称,通常是本地化的名称
	SimpleName  string          `json:"simple_name"` // 区域简称,例如:chn(中国), fra(France), usa(United States)
	FullName    string          `json:"full_name"`   // 完整的名称,例如:中华人民共和国(PRC)
	Code2       RegionCode2     `json:"code_xx"`     // 二字符区域码
	Code3       RegionCode3     `json:"code_xxx"`    // 三字符区域码
	PhoneCode   RegionPhoneCode `json:"phone_code"`  // 电话区域码
}

RegionDTO 表示国际区号

type RegionID

type RegionID int

RegionID ...

type RegionPhoneCode

type RegionPhoneCode string

RegionPhoneCode 是数字形式的国际电话区号, 例如:中国(86), 法国(33), 俄国(7), 美国(1), 英国(44)

func (RegionPhoneCode) Normalize

func (code RegionPhoneCode) Normalize() RegionPhoneCode

Normalize 标准化代码

func (RegionPhoneCode) String

func (code RegionPhoneCode) String() string

type RegionQuery

type RegionQuery struct {
	Conditions Conditions
	Pagination Pagination
	All        bool // 查询全部条目
}

RegionQuery 查询参数

type RegionService

type RegionService interface {
	Insert(c context.Context, o *RegionDTO) (*RegionDTO, error)
	Update(c context.Context, id RegionID, o *RegionDTO) (*RegionDTO, error)
	Delete(c context.Context, id RegionID) error

	Find(c context.Context, id RegionID) (*RegionDTO, error)
	List(c context.Context, q *RegionQuery) ([]*RegionDTO, error)
}

RegionService ...

type RoleDTO

type RoleDTO struct {
	ID RoleID `json:"id"`

	BaseDTO

	Name        RoleName `json:"name"`
	Description string   `json:"description"`
}

RoleDTO 表示 Role 的 REST 网络对象

type RoleID

type RoleID int64

RoleID 是 Role 的实体 ID

type RoleName

type RoleName string

RoleName 是 Role 的正式名称

const (
	RoleAdmin  RoleName = "admin"  // 管理员
	RoleAnonym RoleName = "anonym" // 匿名者
	RoleAny    RoleName = "any"    // 任何人
	RoleFriend RoleName = "friend" // 盆友
	RoleGuest  RoleName = "guest"  // 访客
	RoleOwner  RoleName = "owner"  // 资源持有者
	RoleRoot   RoleName = "root"   // 超级管理员
	RoleUser   RoleName = "user"   // 普通用户 (已登录验证的)
)

定义一些常用的角色

func (RoleName) String

func (name RoleName) String() string

type RoleNameList

type RoleNameList string

RoleNameList 是一组以逗号分隔的 RoleName

func NewRoleNameList

func NewRoleNameList(names ...RoleName) RoleNameList

NewRoleNameList 新建角色列表

func (RoleNameList) List

func (list RoleNameList) List() []RoleName

List 拆分成单项列表

func (RoleNameList) Normalize

func (list RoleNameList) Normalize() RoleNameList

Normalize 标准化

type RoleQuery

type RoleQuery struct {
	Conditions Conditions
	Pagination Pagination
	All        bool // 查询全部条目
}

RoleQuery 查询参数

type RoleService

type RoleService interface {
	Insert(c context.Context, o *RoleDTO) (*RoleDTO, error)
	Update(c context.Context, id RoleID, o *RoleDTO) (*RoleDTO, error)
	Delete(c context.Context, id RoleID) error

	Find(c context.Context, id RoleID) (*RoleDTO, error)
	List(c context.Context, q *RoleQuery) ([]*RoleDTO, error)
}

RoleService 是针对 RoleDTO 的服务

type RoleVO

type RoleVO struct {
	BaseVO

	Roles []*RoleDTO `json:"roles"`
}

RoleVO ...

type SessionDTO

type SessionDTO struct {
	ID SessionID `json:"id"`

	BaseDTO
	CurrentUser

	Authenticated bool `json:"authenticated"` // 是否已验证
}

SessionDTO 表示会话信息

type SessionID added in v0.0.6

type SessionID SessionIID

SessionID 用 int64 来作为会话的标识符

type SessionIID added in v0.0.12

type SessionIID int64

SessionIID : int-id of session

type SessionService added in v0.0.6

type SessionService interface {
	Find(c context.Context, id SessionID) (*SessionDTO, error)

	Insert(c context.Context, se *SessionDTO) (*SessionDTO, error)

	Update(c context.Context, id SessionID, se *SessionDTO) (*SessionDTO, error)
}

SessionService ...

type SessionUUID added in v0.0.12

type SessionUUID lang.UUID

SessionUUID : uuid of session

type SessionVO

type SessionVO struct {
	BaseVO

	Sessions []*SessionDTO `json:"sessions"`
}

SessionVO ...

type SimplePhoneNumber

type SimplePhoneNumber PhoneNumber

SimplePhoneNumber 表示简短的电话号码, 标准化的取值为纯数字形式,不含任何其它字符,like:"12345678901"

func (SimplePhoneNumber) Normalize

func (num SimplePhoneNumber) Normalize() SimplePhoneNumber

Normalize 标准化

func (SimplePhoneNumber) String

func (num SimplePhoneNumber) String() string

type SubjectContext added in v0.0.12

type SubjectContext struct {
	Authenticated bool

	Token   *TokenDTO
	Session *SessionDTO
	Subject *SubjectDTO

	Checker PermissionChecker
	Service SubjectService

	// 当前用户具有的角色
	HaveRoles []RoleName

	// 当前操作可接受的角色
	AcceptedRoles []RoleName
}

type SubjectDTO added in v0.0.3

type SubjectDTO struct {
	ID SubjectID `json:"id"`

	Authenticated bool       `json:"authenticated"` // 是否已验证
	Token         TokenDTO   `json:"token"`
	Session       SessionDTO `json:"session"`
}

SubjectDTO 表示 操作主体 的 REST 网络对象

type SubjectID added in v0.0.3

type SubjectID int64

SubjectID 是通用的操作主体标识符

type SubjectService added in v0.0.3

type SubjectService interface {
	GetCurrent(c context.Context) (*SubjectDTO, error)

	GetCurrentChecker(c context.Context) (PermissionChecker, error)

	GetCurrentContext(c context.Context) (*SubjectContext, error)
}

SubjectService 是针对 SubjectDTO 的服务; 它提供对 Token & Session 的联合查询

type SubjectVO added in v0.0.3

type SubjectVO struct {
	BaseVO

	Subject *SubjectDTO `json:"subject"`
}

SubjectVO ...

type TokenDTO

type TokenDTO struct {
	SessionID   SessionID   `json:"session_id"`   // 会话的 ID
	SessionUUID SessionUUID `json:"session_uuid"` // 会话的 UUID

}

TokenDTO 表示令牌信息

type TokenService added in v0.0.6

type TokenService interface {
	GetCurrent(c context.Context) (*TokenDTO, error)

	PutCurrent(c context.Context, token *TokenDTO) (*TokenDTO, error)
}

TokenService ...

type TokenVO added in v0.0.6

type TokenVO struct {
	BaseVO

	Tokens []*TokenDTO `json:"tokens"`
}

TokenVO ...

type UserDTO

type UserDTO struct {
	ID UserID `json:"id"`

	BaseDTO

	Name     UserName     `json:"name"`
	NickName string       `json:"nickname"`
	Avatar   string       `json:"avatar"`
	Phone    string       `json:"phone"`
	Email    string       `json:"email"`
	Language string       `json:"language"`
	Roles    RoleNameList `json:"roles"`
	Enabled  bool         `json:"enabled"`
}

UserDTO 表示 User 的 REST 网络对象

type UserID

type UserID int64

UserID 是通用的用户标识符

func ParseUserID

func ParseUserID(s string) (UserID, error)

ParseUserID 把字符串解析为 UserID

func (UserID) String

func (id UserID) String() string

type UserInfo

type UserInfo struct {
	CurrentUser
}

UserInfo 包含当前用户的基本信息

func GetUserInfo

func GetUserInfo(cc context.Context) (*UserInfo, error)

GetUserInfo 从上下文中获取已绑定的用户信息

type UserInfoBinding

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

UserInfoBinding 负责把用户信息绑定到当前上下文

func NewUserInfoBinding

func NewUserInfoBinding(info *UserInfo) *UserInfoBinding

NewUserInfoBinding 新建并初始化一个 UserInfoBinding

func (*UserInfoBinding) Key

func (inst *UserInfoBinding) Key() string

Key 取绑定键名

func (*UserInfoBinding) UserInfo

func (inst *UserInfoBinding) UserInfo() *UserInfo

UserInfo 取用户信息

type UserName

type UserName string

UserName 表示用户名

func (UserName) String

func (name UserName) String() string

type UserQuery

type UserQuery struct {
	Conditions Conditions
	Pagination Pagination
	All        bool // 查询全部条目
}

UserQuery 是 User 的查询参数

type UserService

type UserService interface {
	Insert(c context.Context, o *UserDTO) (*UserDTO, error)
	Update(c context.Context, id UserID, o *UserDTO) (*UserDTO, error)
	Delete(c context.Context, id UserID) error

	Find(c context.Context, id UserID) (*UserDTO, error)
	List(c context.Context, q *UserQuery) ([]*UserDTO, error)
}

UserService 是针对 UserDTO 的服务

type UserVO

type UserVO struct {
	BaseVO

	Users []*UserDTO `json:"users"`
}

UserVO ...

type VOGetter

type VOGetter interface {
	GetVO() *BaseVO
}

VOGetter 是获取 BaseVO 的接口

Jump to

Keyboard shortcuts

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