modelauthz

package
v0.10.14 Latest Latest
Warning

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

Go to latest
Published: May 18, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MenuPlatformAll     = "all"
	MenuPlatformWeb     = "web"
	MenuPlatformMobile  = "mobile"
	MenuPlatformDesktop = "desktop"
)

Variables

View Source
var (
	RootID      = model.RootID
	RootName    = model.RootName
	UnknownID   = model.UnknownID
	UnknownName = model.UnknownName
	NoneID      = model.NoneID
	NoneName    = model.NoneName

	KeyName = model.KeyName
	KeyID   = model.KeyID
)
View Source
var MenuRoot = &Menu{ParentID: model.RootID, Base: model.Base{ID: RootID}}

Functions

This section is empty.

Types

type API

type API struct {
	model.Empty
}

type APIRsp

type APIRsp = []string

type Button

type Button struct {
	Name string // button display name
	Code string // unique button code/identifier
	Icon string // icon name from backend

	Status   int   // 0: disabled, 1: enabled
	Visiable *bool // whether the button is rendered in UI

	MenuID string // parent menu id

	model.Base
}

func (Button) Purge

func (Button) Purge() bool

type CasbinRule

type CasbinRule struct {
	// ID uint64 `json:"id" gorm:"primaryKey"`
	ID    uint64 `json:"id" gorm:"primaryKey;autoIncrement:true"`
	Ptype string `json:"ptype" gorm:"size:100" schema:"ptype"`
	V0    string `json:"v0,omitempty" gorm:"size:100" schema:"v0"`
	V1    string `json:"v1,omitempty" gorm:"size:100" schema:"v1"`
	V2    string `json:"v2,omitempty" gorm:"size:100" schema:"v2"`
	V3    string `json:"v3,omitempty" gorm:"size:100" schema:"v3"`
	V4    string `json:"v4,omitempty" gorm:"size:100" schema:"v4"`
	V5    string `json:"v5,omitempty" gorm:"size:100" schema:"v5"`

	User string `json:"user,omitempty" schema:"user"` // 只是用来记录一些信息, V0 为 user_id
	Role string `json:"role,omitempty" schema:"role"` // 只是用来记录一些信息, V1 为 role code

	model.Base
}

CasbinRule 对象 RBAC 包中会通过 gormadapter.NewAdapterByDBWithCustomTable(database.DB, &model.CasbinRule{}) 或 gormadapter.NewAdapterByDB(database.DB) 来创建; NOTE: ID 类型必须是整型

-- 权限策略 INSERT INTO casbin_rule (ptype, v0, v1, v2, v3) VALUES ('p', 'role_admin', '/api/config/*', 'GET', 'allow'), ('p', 'role_admin', '/api/config/*', 'POST', 'allow'), ('p', 'role_user', '/api/config/file', 'GET', 'allow');

-- 角色关系 INSERT INTO casbin_rule (ptype, v0, v1) VALUES ('g', 'alice', 'role_admin'), ('g', 'bob', 'role_user'), ('g', 'role_admin', 'admin'); -- admin 超级角色

func (CasbinRule) GetTableName

func (cr CasbinRule) GetTableName() string

GetTableName 用来指定 CasbinRule 在数据库中的表名为 casbin_rule. gormadapter.NewAdapterByDBWithCustomTable 创建的表名总是 casbin_rule, 但是 gorm 默认创建的表名为 casbin_rules, 为了统一,直接就用 casbin_rule 了.

func (*CasbinRule) SetID

func (cr *CasbinRule) SetID(id ...string)

SetID 为一个空的函数,不允许自动设置ID, 因为 gormadapter.NewAdapterByDBWithCustomTable 创建的表的ID总是为 autoIncrement. 如果设置了自定义ID则会报错.

type Menu struct {
	API   datatypes.JSONSlice[string] `json:"api,omitempty" schema:"api"`     // 后端路由, 如果为空则使用 "/api" + Path
	Path  string                      `json:"path,omitempty" schema:"path"`   // path should not add `omitempty` tag, empty value means default router in react route6.x.
	Label string                      `json:"label,omitempty" schema:"label"` // 页面组件左侧的菜单名
	Icon  string                      `json:"icon,omitempty" schema:"icon"`   // 页面组件左侧的菜单图标

	Visiable *bool  `json:"visiable,omitempty" schema:"visiable" gorm:"default:1"`                                                   // 前端页面路由是否可见
	Default  string `json:"default,omitempty" schema:"default"`                                                                      // 子路由中的默认路由, 如果有 Children, Default 才可能存在
	Status   *uint  `json:"status,omitempty" gorm:"type:smallint;default:1;comment:status(0: disabled, 1: enabled)" schema:"status"` // 该路由是否启用

	ParentID string  `json:"parent_id,omitempty" gorm:"size:191" schema:"parent_id"`
	Children []*Menu `json:"children,omitempty" gorm:"foreignKey:ParentID"`             // 子路由
	Parent   *Menu   `json:"parent,omitempty" gorm:"foreignKey:ParentID;references:ID"` // 父路由

	// the empty value of `Platform` means all.
	Platform MenuPlatform `json:"platform,omitempty" schema:"platform"`

	DomainPattern string `json:"domain_pattern,omitempty" schema:"domain_pattern" gorm:"default:.*"`

	model.Base
}
func (m *Menu) CreateBefore(ctx *types.ModelContext) (err error)
func (m *Menu) DeleteBefore(ctx *types.ModelContext) error

DeleteBefore will delete the role's permissions

func (m *Menu) Excludes() map[string][]any
func (m *Menu) Expands() []string
func (m *Menu) MarshalLogObject(enc zapcore.ObjectEncoder) error
func (m *Menu) Purge() bool
func (m *Menu) UpdateAfter(ctx *types.ModelContext) error

UpdateAfter will query all roles and check whether the role contains the current menu. If role contains current menu and the menu's API changed, then call "role.UpdatePermission" to updates the role's permissions.

func (m *Menu) UpdateBefore(ctx *types.ModelContext) error
type MenuPlatform string

type Permission

type Permission struct {
	Resource string `json:"resource,omitempty" schema:"resource"`
	Action   string `json:"action,omitempty" schema:"action"`

	model.Base
}

func (*Permission) CreateBefore

func (p *Permission) CreateBefore(*types.ModelContext) error

func (*Permission) MarshalLogObject

func (p *Permission) MarshalLogObject(enc zapcore.ObjectEncoder) error

func (*Permission) Purge

func (p *Permission) Purge() bool

type Role

type Role struct {
	Name    string `json:"name,omitempty" schema:"name" gorm:"size:191;unique"`
	Code    string `json:"code,omitempty" schema:"code" gorm:"size:191;unique"`
	Default *bool  `json:"default,omitempty" schema:"default"`

	// Scope holds generic constraints for regional roles.
	// Keys and values are user-defined and framework-agnostic.
	Scope datatypes.JSONMap `json:"scope,omitempty"`

	// Menu permissions owned by this role
	MenuIDs        datatypes.JSONSlice[string] `json:"menu_ids,omitempty"`
	MenuPartialIDs datatypes.JSONSlice[string] `json:"menu_partial_ids,omitempty"`
	ButtonIDs      datatypes.JSONSlice[string] `json:"button_ids,omitempty"`

	Menus        []*Menu `json:"menus,omitempty" gorm:"-"`
	MenuPartials []*Menu `json:"menu_partials,omitempty" gorm:"-"`

	model.Base
}

func (*Role) CreateAfter

func (r *Role) CreateAfter(ctx *types.ModelContext) error

CreateAfter will creates the role's permissions.

func (*Role) CreateBefore

func (r *Role) CreateBefore(ctx *types.ModelContext) error

func (*Role) DeleteBefore

func (r *Role) DeleteBefore(ctx *types.ModelContext) error

DeleteBefore will delete the role's permissions

func (*Role) MarshalLogObject

func (r *Role) MarshalLogObject(enc zapcore.ObjectEncoder) error

func (*Role) Purge

func (r *Role) Purge() bool

func (*Role) UpdateBefore

func (r *Role) UpdateBefore(ctx *types.ModelContext) error

UpdateBefore will delete the old role's permissions and create the new role's permissions. more details see "UpdatePermission".

func (*Role) UpdatePermission

func (r *Role) UpdatePermission(ctx *types.ModelContext) error

UpdatePermission must run in the "UpdateBefore" hook. It uses a brute-force strategy: revoke all existing policies for the role, then grant permissions derived from the current menus. This avoids any unknown leftovers in the casbin_rule table and ensures strong consistency.

type RolePermission

type RolePermission struct {
	Role string `json:"role" schema:"role"`

	Resource string        `json:"resource" schema:"resource"`
	Action   string        `json:"action" schema:"action"`
	Effect   consts.Effect `json:"effect" schema:"effect"`

	model.Base
}

RolePermission is a permission for a role

Deprecated

RolePermission is deprecated, operations CasbinRule directly.

func (*RolePermission) CreateAfter

func (r *RolePermission) CreateAfter(*types.ModelContext) error

func (*RolePermission) CreateBefore

func (r *RolePermission) CreateBefore(*types.ModelContext) error

func (*RolePermission) DeleteBefore

func (r *RolePermission) DeleteBefore(ctx *types.ModelContext) error

func (*RolePermission) MarshalLogObject

func (r *RolePermission) MarshalLogObject(enc zapcore.ObjectEncoder) error

func (*RolePermission) Purge

func (r *RolePermission) Purge() bool

type UserRole

type UserRole struct {
	UserID string `json:"user_id,omitempty" schema:"user_id"`
	RoleID string `json:"role_id,omitempty" schema:"role_id"`

	RoleCode string `json:"rolecode,omitempty" schema:"rolecode"` // 角色Code, 用于 RBAC 角色控制和前端查询
	Username string `json:"username,omitempty" schema:"username"` // 用户名, 用于 RBAC 用户控制和前端查询

	User *modeliamuser.User `json:"user,omitempty" gorm:"-"`
	Role *Role              `json:"role,omitempty" gorm:"-"`

	model.Base
}

func (*UserRole) CreateAfter

func (r *UserRole) CreateAfter(ctx *types.ModelContext) error

func (*UserRole) CreateBefore

func (r *UserRole) CreateBefore(ctx *types.ModelContext) error

func (*UserRole) DeleteBefore

func (r *UserRole) DeleteBefore(ctx *types.ModelContext) error

func (*UserRole) MarshalLogObject

func (r *UserRole) MarshalLogObject(enc zapcore.ObjectEncoder) error

func (*UserRole) Purge

func (r *UserRole) Purge() bool

Jump to

Keyboard shortcuts

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