Documentation ¶
Overview ¶
Package rbac
@author: xwc1125
Package rbac ¶
@author: xwc1125
Package rbac ¶
@author: xwc1125
Package rbac ¶
@author: xwc1125
Index ¶
- Constants
- Variables
- func GetGroupKey(gid int64) string
- func GetPermKey(pid int64) string
- func GetRoleKey(rid int64) string
- func GetUserKey(uid int64) string
- type Adapter
- func NewAdapter(driverName string, dataSourceName string, params ...interface{}) (*Adapter, error)
- func NewAdapterByDB(db *gorm.DB) (*Adapter, error)
- func NewAdapterByDBUseTableName(db *gorm.DB, prefix string, tableName string) (*Adapter, error)
- func NewAdapterByDBWithCustomTable(db *gorm.DB, t interface{}, tableName ...string) (*Adapter, error)
- func NewAdapterByMulDb(dbPool DbPool, dbName string, prefix string, tableName string) (*Adapter, error)
- func NewFilteredAdapter(driverName string, dataSourceName string, params ...interface{}) (*Adapter, error)
- func (a *Adapter) AddLogger(l logger.Interface)
- func (a *Adapter) AddPolicies(sec string, pType string, rules [][]string) error
- func (a *Adapter) AddPolicy(sec string, pType string, rule []string) error
- func (a *Adapter) Close() error
- func (a *Adapter) IsFiltered() bool
- func (a *Adapter) LoadFilteredPolicy(model model.Model, filter interface{}) error
- func (a *Adapter) LoadPolicy(model model.Model) error
- func (a *Adapter) Open() error
- func (a *Adapter) RemoveFilteredPolicy(sec string, pType string, fieldIndex int, fieldValues ...string) error
- func (a *Adapter) RemovePolicies(sec string, pType string, rules [][]string) error
- func (a *Adapter) RemovePolicy(sec string, pType string, rule []string) error
- func (a *Adapter) SavePolicy(model model.Model) error
- func (a *Adapter) UpdateFilteredPolicies(sec string, pType string, newPolicies [][]string, fieldIndex int, ...) ([][]string, error)
- func (a *Adapter) UpdatePolicies(sec string, pType string, oldRules, newRules [][]string) error
- func (a *Adapter) UpdatePolicy(sec string, pType string, oldRule, newPolicy []string) error
- type CasbinRule
- type DbPool
- type Filter
- type Logger
- func (l *Logger) EnableLog(enable bool)
- func (l *Logger) IsEnabled() bool
- func (l *Logger) LogEnforce(matcher string, request []interface{}, result bool, explains [][]string)
- func (l *Logger) LogModel(model [][]string)
- func (l *Logger) LogPolicy(policy map[string][][]string)
- func (l *Logger) LogRole(roles []string)
- type Menu
- type Owner
- type OwnerType
- type Permission
- type RBAC
- func NewRbacByGorm(db *gorm.DB, tablePrefix string, tableName string) (*RBAC, error)
- func NewRbacByGormWithModel(rbacModelOrFile string, db *gorm.DB, tablePrefix string, tableName string) (*RBAC, error)
- func NewRbacByXorm(db *xorm.Engine, tablePrefix string, tableName string) (*RBAC, error)
- func NewRbacByXormWithModel(rbacModelOrFile string, db *xorm.Engine, tablePrefix string, tableName string) (*RBAC, error)
- func (r *RBAC) AddPermissionsForOwner(pType string, owner Owner, permissions []Permission, domain ...string) (bool, error)
- func (r *RBAC) AddProvidersForOwner(pType string, owner Owner, providers []Owner, domain ...string) (bool, error)
- func (r *RBAC) DeleteAllProvidersForOwner(pType string, owner Owner, domain ...string) (bool, error)
- func (r *RBAC) DeleteOwner(pType string, owner Owner, domain ...string) (bool, error)
- func (r *RBAC) DeleteOwnerLike(pType string, owner Owner, providerPrefix string, domain ...string) (bool, error)
- func (r *RBAC) DeletePermissionsForOwner(pType string, owner Owner, params []string, domain ...string) (bool, error)
- func (r *RBAC) DeletePolicyForOwner(pType string, owner Owner, domain ...string) (bool, error)
- func (r *RBAC) DeleteProviderForOwner(pType string, owner Owner, provider Owner, domain ...string) (bool, error)
- func (r *RBAC) DeleteProvidersForOwnerLike(pType string, owner Owner, providerPrefix string, domain ...string) (bool, error)
- func (r *RBAC) DeleteResource(pType string, perm Permission, params []string, domain ...string) (bool, error)
- func (r *RBAC) GetAllResourcesByOwner(owner Owner, domain ...string) map[string]interface{}
- func (r *RBAC) GetEnforcer() *casbin.SyncedEnforcer
- func (r *RBAC) GetOwnersForProvider(pType string, provider Owner, domain ...string) ([]string, error)
- func (r *RBAC) GetProvidersForOwner(pType string, owner Owner, domain ...string) ([]string, error)
- func (r *RBAC) HasPermission(owner Owner, path string, method string, extra []string, domain ...string) (bool, error)
- func (r *RBAC) HasPermission2(subject string, resource string, action string, extra []string, ...) (bool, error)
- func (r *RBAC) HasProviderForOwner(pType string, owner Owner, provider Owner, domain ...string) (bool, error)
- type RightType
- type RoleDefine
Constants ¶
const ( PrefixUserID = "u" PrefixRoleID = "r" PrefixGroupID = "g" PrefixPermID = "p" )
const ( RType = "r" PType = "p" EType = "e" MType = "m" )
const ( TYPE_MODULE = 1 // 模块 TYPE_MENU = 2 // 菜单 TYPE_OPERATE = 3 // 操作 )
const ( AbleAccessible = RightType(0) // 可访问 AbleAuthorize = RightType(1) // 可授权 )
Variables ¶
var (
DefaultDomain = "default"
)
var (
Separator = ":"
)
Functions ¶
func GetGroupKey ¶
func GetPermKey ¶
func GetRoleKey ¶
func GetUserKey ¶
Types ¶
type Adapter ¶
type Adapter struct {
// contains filtered or unexported fields
}
Adapter represents the Gorm adapter for policy storage.
func NewAdapter ¶
NewAdapter is the constructor for Adapter. Params : databaseName,tableName,dbSpecified
databaseName,{tableName/dbSpecified} {database/dbSpecified}
databaseName and tableName are user defined. Their default value are "casbin" and "casbin_rule"
dbSpecified is an optional bool parameter. The default value is false. It's up to whether you have specified an existing DB in dataSourceName. If dbSpecified == true, you need to make sure the DB in dataSourceName exists. If dbSpecified == false, the adapter will automatically create a DB named databaseName.
func NewAdapterByDB ¶
NewAdapterByDB creates gorm-adapter by an existing Gorm instance
func NewAdapterByDBUseTableName ¶
NewAdapterByDBUseTableName creates gorm-adapter by an existing Gorm instance and the specified table prefix and table name Example: gormadapter.NewAdapterByDBUseTableName(&db, "cms", "casbin") Automatically generate table name like this "cms_casbin"
func NewAdapterByMulDb ¶
func NewFilteredAdapter ¶
func NewFilteredAdapter(driverName string, dataSourceName string, params ...interface{}) (*Adapter, error)
NewFilteredAdapter is the constructor for FilteredAdapter. Casbin will not automatically call LoadPolicy() for a filtered adapter.
func (*Adapter) AddPolicies ¶
AddPolicies adds multiple policy rules to the storage.
func (*Adapter) IsFiltered ¶
IsFiltered returns true if the loaded policy has been filtered.
func (*Adapter) LoadFilteredPolicy ¶
LoadFilteredPolicy loads only policy rules that match the filter.
func (*Adapter) LoadPolicy ¶
LoadPolicy loads policy from database.
func (*Adapter) RemoveFilteredPolicy ¶
func (a *Adapter) RemoveFilteredPolicy(sec string, pType string, fieldIndex int, fieldValues ...string) error
RemoveFilteredPolicy removes policy rules that match the filter from the storage.
func (*Adapter) RemovePolicies ¶
RemovePolicies removes multiple policy rules from the storage.
func (*Adapter) RemovePolicy ¶
RemovePolicy removes a policy rule from the storage.
func (*Adapter) SavePolicy ¶
SavePolicy saves policy to database.
func (*Adapter) UpdateFilteredPolicies ¶
func (*Adapter) UpdatePolicies ¶
type CasbinRule ¶
type CasbinRule struct { ID uint `xorm:"pk autoincr notnull" gorm:"primaryKey;autoIncrement"` PType string `xorm:"varchar(100) index not null default ''" gorm:"column:p_type;size:100"` V0 string `xorm:"varchar(100) index not null default ''" gorm:"size:100"` V1 string `xorm:"varchar(100) index not null default ''" gorm:"size:100"` V2 string `xorm:"varchar(100) index not null default ''" gorm:"size:100"` V3 string `xorm:"varchar(100) index not null default ''" gorm:"size:100"` V4 string `xorm:"varchar(100) index not null default ''" gorm:"size:100"` V5 string `xorm:"varchar(100) index not null default ''" gorm:"size:100"` }
CasbinRule casbin规则
func (CasbinRule) TableName ¶
func (CasbinRule) TableName() string
type DbPool ¶
type DbPool struct {
// contains filtered or unexported fields
}
func InitDbResolver ¶
InitDbResolver multiple databases support Example usage: dbPool,err := InitDbResolver([]gorm.Dialector{mysql.Open(dsn),mysql.Open(dsn2)},[]string{"casbin1","casbin2"}) a := initAdapterWithGormInstanceByMulDb(t,dbPool,"casbin1","","casbin_rule1") a = initAdapterWithGormInstanceByMulDb(t,dbPool,"casbin2","","casbin_rule2")/*
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
func (*Logger) LogEnforce ¶
type Permission ¶
type Permission struct { Id int64 // 资源ID Resource string // 资源内容 ResourceType string // 资源类型 Action string // 操作动作 RightType RightType // 权限类型 Extra interface{} // 扩展内容 }
Permission 资源
type RBAC ¶
type RBAC struct { *casbin.SyncedEnforcer // contains filtered or unexported fields }
func NewRbacByGorm ¶
NewRbacByGorm 根据gorm初始化casbin
func NewRbacByGormWithModel ¶
func NewRbacByXorm ¶
NewRbacByXorm 根据xorm初始化casbin
func NewRbacByXormWithModel ¶
func (*RBAC) AddPermissionsForOwner ¶
func (r *RBAC) AddPermissionsForOwner(pType string, owner Owner, permissions []Permission, domain ...string) (bool, error)
AddPermissionsForOwner 给owner设置资源
func (*RBAC) AddProvidersForOwner ¶
func (r *RBAC) AddProvidersForOwner(pType string, owner Owner, providers []Owner, domain ...string) (bool, error)
AddProvidersForOwner 给Owner添加提供者
func (*RBAC) DeleteAllProvidersForOwner ¶
func (r *RBAC) DeleteAllProvidersForOwner(pType string, owner Owner, domain ...string) (bool, error)
DeleteAllProvidersForOwner 删除Owner的所有角色
func (*RBAC) DeleteOwner ¶
DeleteOwner 删除用户
func (*RBAC) DeleteOwnerLike ¶
func (r *RBAC) DeleteOwnerLike(pType string, owner Owner, providerPrefix string, domain ...string) (bool, error)
DeleteOwnerLike 删除带前缀为providerPrefix的用户
func (*RBAC) DeletePermissionsForOwner ¶
func (r *RBAC) DeletePermissionsForOwner(pType string, owner Owner, params []string, domain ...string) (bool, error)
DeletePermissionsForOwner 删除Owner的资源权限[p] policy_definition规则
func (*RBAC) DeletePolicyForOwner ¶
DeletePolicyForOwner 删除用户对应的权限[g] role_definition规则
func (*RBAC) DeleteProviderForOwner ¶
func (r *RBAC) DeleteProviderForOwner(pType string, owner Owner, provider Owner, domain ...string) (bool, error)
DeleteProviderForOwner 删除Owner的某个角色
func (*RBAC) DeleteProvidersForOwnerLike ¶
func (r *RBAC) DeleteProvidersForOwnerLike(pType string, owner Owner, providerPrefix string, domain ...string) (bool, error)
DeleteProvidersForOwnerLike 删除带前缀为providerPrefix的provider
func (*RBAC) DeleteResource ¶
func (*RBAC) GetAllResourcesByOwner ¶
GetAllResourcesByOwner 通过uid获取用户的所有资源[用于解析权限]
func (*RBAC) GetEnforcer ¶
func (r *RBAC) GetEnforcer() *casbin.SyncedEnforcer
func (*RBAC) GetOwnersForProvider ¶
func (r *RBAC) GetOwnersForProvider(pType string, provider Owner, domain ...string) ([]string, error)
GetOwnersForProvider 根据rid获取用户集合
func (*RBAC) GetProvidersForOwner ¶
GetProvidersForOwner 根据Owner获取提供者
func (*RBAC) HasPermission ¶
func (r *RBAC) HasPermission(owner Owner, path string, method string, extra []string, domain ...string) (bool, error)
HasPermission 检查用户是否有权限【middleware中调用】