You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
685 B
35 lines
685 B
3 years ago
|
package acs
|
||
|
|
||
|
import (
|
||
|
"github.com/casbin/casbin/v2"
|
||
|
"github.com/casbin/gorm-adapter/v3"
|
||
|
"gorm.io/gorm"
|
||
|
)
|
||
|
|
||
|
var Enforcer *casbin.Enforcer
|
||
|
|
||
|
type Config struct {
|
||
|
Db *gorm.DB
|
||
|
ModelFile string
|
||
|
Prefix string
|
||
|
TableName string
|
||
|
Log bool
|
||
|
}
|
||
|
|
||
|
// InitEnforcer @Title 初始化
|
||
|
func InitEnforcer(config Config) error {
|
||
|
// mysql 适配器
|
||
|
adapter, err := gormadapter.NewAdapterByDBUseTableName(config.Db, config.Prefix, config.TableName)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
// 通过mysql适配器新建一个enforcer
|
||
|
Enforcer, err = casbin.NewEnforcer(config.ModelFile, adapter)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
// 日志记录
|
||
|
Enforcer.EnableLog(config.Log)
|
||
|
return nil
|
||
|
}
|