Documentation ¶
Overview ¶
Package rbac RBAC 的简单实现
rbac := rbac.New(...) group := rbac.NewGroup("user", web.Phrase("user")) view := group.New("view", web.Phrase("view info")) // 返回判断权限的中间件 del := group.New("del", web.Phrase("delete user")) // 返回判断权限的中间件 router.Get("/users", view(func(*web.Context)web.Responser{ // do somthing })) router.Delete("/users/{id}", del(func(*web.Context)web.Responser{ // do somthing }))
Index ¶
- type GetUIDFunc
- type RBAC
- type Resource
- type ResourceGroup
- type Role
- func (role *Role[T]) Allow(res ...string) error
- func (role *Role[T]) Del() error
- func (role *Role[T]) Descendants(all bool) ([]*Role[T], error)
- func (role *Role[T]) IsDescendant(rid string) bool
- func (role *Role[T]) Link(uid T) error
- func (role *Role[T]) Resource() *RoleResource
- func (role *Role[T]) Set(name, desc string) error
- func (role *Role[T]) Unlink(uid T) error
- type RoleGroup
- type RoleResource
- type Store
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type GetUIDFunc ¶
type GetUIDFunc[T comparable] func(*web.Context) (T, web.Responser)
GetUIDFunc 从 web.Context 获得当前的登录用户 ID
type RBAC ¶
type RBAC[T comparable] struct { // contains filtered or unexported fields }
RBAC RBAC 实现
T 表示的是用户的 ID 类型。
func New ¶
func New[T comparable](s web.Server, store Store[T], getUID GetUIDFunc[T]) *RBAC[T]
New 声明 RBAC
getUID 参考 GetUIDFunc; loadInterval 如果大于 0,表示以该频率从 Store 加载数据;
func (*RBAC[T]) NewResourceGroup ¶
func (rbac *RBAC[T]) NewResourceGroup(id string, title web.LocaleStringer) *ResourceGroup[T]
NewResourceGroup 声明一组资源
id 为该资源组的唯一 ID; title 对该资源组的描述;
func (*RBAC[T]) ResourceGroup ¶
func (rbac *RBAC[T]) ResourceGroup(id string) *ResourceGroup[T]
type ResourceGroup ¶
type ResourceGroup[T comparable] struct { // contains filtered or unexported fields }
ResourceGroup 表示一组资源
func (*ResourceGroup[T]) New ¶
func (g *ResourceGroup[T]) New(id string, desc web.LocaleStringer) web.MiddlewareFunc
New 添加新的资源
返回的是用于判断是否拥有当前资源权限的中间件。
func (*ResourceGroup[T]) RBAC ¶
func (g *ResourceGroup[T]) RBAC() *RBAC[T]
type Role ¶
type Role[T comparable] struct { ID string Parent string Name string Desc string Resources []string // 当前角色关联的资源 Users []T // 当前角色关联的用户 // contains filtered or unexported fields }
Role 角色信息
func (*Role[T]) IsDescendant ¶
IsDescendant 判断角色 rid 是否为当前角色的子角色
type RoleGroup ¶
type RoleGroup[T comparable] struct { // contains filtered or unexported fields }
RoleGroup 角色分组
当一个用户系统有多个独立的权限模块时,可以很好地用 RoleGroup 进行表示, 比如商家系统,每个商家拥有自己的操作人员,可为每个商家创建 RoleGroup。
type RoleResource ¶
type RoleResource struct { // Current 角色当前能访问的资源 Current []string `json:"current" xml:"current" yaml:"current" cbor:"current"` // Parent 角色的父类能访问的资源 // // 必然也是当前角色所能访问的最大资源列表。 // // Parent 肯定是包含了 Current 的所有值。 Parent []string `json:"parent" xml:"parent" yaml:"parent" cbor:"parent"` }
RoleResource 表示某个角色所能访问的资源
type Store ¶
type Store[T comparable] interface { // Load 加载 gid 下的所有角色 Load(gid string) (map[string]*Role[T], error) // Del 删除指定角色 // // 如果角色下面还有子角色或是用户时,不应该删除。 Del(gid, roleID string) error // Set 修改角色信息 // // 如果修改了角色可访问的资源列表,应该检测子角色是否拥有该资源。 Set(gid string, r *Role[T]) error // Add 添加角色信息 // // r.id 是由 [Server.UniqueID] 保证不重复的,如果出现重复的情况,应该直接 panic. Add(gid string, r *Role[T]) error }
Store 存储接口
func NewCacheStore ¶
func NewCacheStore[T comparable](s web.Server, prefix string) Store[T]
NewCacheStore 声明基于 web.Cache 的 Store 实现
NOTE: 缓存是易失性的,不太具备实用性,可用于测试。
Click to show internal directories.
Click to hide internal directories.