casbin

package
v0.0.0-...-05d1b2a Latest Latest
Warning

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

Go to latest
Published: May 6, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

README

Casbin

技术方案

  • model.conf 默认值见 default.go
  • policy.csv 使用的是 MySQL 动态调整,驱动使用gorm
  • 分布式消息的监视器,使用的是Redis

依赖

公共库
  • go-redis
  • cast
  • gin
  • gin-jwt
内置库
  • mysql
  • zap
  • response
  • redis

Documentation

Index

Constants

View Source
const (
	PrefixUserID  = "u"
	PrefixRoleID  = "r"
	UserIDCtxName = "casbinUserId"
	SuperUserID   = 1
)

Variables

View Source
var (
	ErrUidEmpty = errors.New("uid not found")
	ErrCasbin   = errors.New("auth model exception")
)

Functions

func CheckPermission

func CheckPermission() gin.HandlerFunc

CheckPermission 根据不同的错误返回不同的 http code - 未找到用户,403 - casbin 报错,500 - 没有权限,401

func GormAdapter

func GormAdapter(dbConf mysql.Options) (*gormadapter.Adapter, error)

Types

type Casbiner

type Casbiner interface {
	RedisWatcher(channel string) error
	CheckPermissionForUser(userId int64, permission ...any) (bool, error)

	AddPermissionForRole(roleId int64, permission ...string) error
	AddPermissionsForRole(roleId int64, permission ...[]string) error
	DeletePermissionForRole(roleId int64, permission ...string) error

	DeleteRole(roleIds []int64) error
	DeleteUser(userIds []int64) error

	AddRoleForUser(userId int64, roleIds []int64) error
	DeleteRoleForUser(userId int64, roleIds []int64) error
	DeleteRolesForUser(userId int64) error

	Enforcer() *casbin.Enforcer
}

func Instance

func Instance(opts ...Option) Casbiner

type Option

type Option func(*Options)

func DBOptions

func DBOptions(opts mysql.Options) Option

func Model

func Model(model string) Option

func RedisOptions

func RedisOptions(opts redis.Options) Option

type Options

type Options struct {
	Model        string
	DBOptions    mysql.Options
	RedisOptions redis.Options
}

Jump to

Keyboard shortcuts

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