未验证 提交 3ec886dd 编写于 作者: Sliver_Horn's avatar Sliver_Horn 提交者: GitHub

fix and optimize casbin (#1121)

* - 删除rabac_model.conf, 改为字符串代码, 方便部署
- 删除casbin相关配置文件

* fix: delete api时只传id导致casbin_rules表清空bug
上级 899c0fa9
...@@ -35,10 +35,6 @@ email: ...@@ -35,10 +35,6 @@ email:
secret: 'xxx' secret: 'xxx'
nickname: 'test' nickname: 'test'
# casbin configuration
casbin:
model-path: './resource/rbac_model.conf'
# system configuration # system configuration
system: system:
env: 'public' # Change to "develop" to skip authentication for development mode env: 'public' # Change to "develop" to skip authentication for development mode
......
...@@ -35,10 +35,6 @@ email: ...@@ -35,10 +35,6 @@ email:
secret: 'xxx' secret: 'xxx'
nickname: 'test' nickname: 'test'
# casbin configuration
casbin:
model-path: './resource/rbac_model.conf'
# system configuration # system configuration
system: system:
env: 'public' # Change to "develop" to skip authentication for development mode env: 'public' # Change to "develop" to skip authentication for development mode
......
package config
type Casbin struct {
ModelPath string `mapstructure:"model-path" json:"model-path" yaml:"model-path"` // 存放casbin模型的相对路径
}
...@@ -5,7 +5,6 @@ type Server struct { ...@@ -5,7 +5,6 @@ type Server struct {
Zap Zap `mapstructure:"zap" json:"zap" yaml:"zap"` Zap Zap `mapstructure:"zap" json:"zap" yaml:"zap"`
Redis Redis `mapstructure:"redis" json:"redis" yaml:"redis"` Redis Redis `mapstructure:"redis" json:"redis" yaml:"redis"`
Email Email `mapstructure:"email" json:"email" yaml:"email"` Email Email `mapstructure:"email" json:"email" yaml:"email"`
Casbin Casbin `mapstructure:"casbin" json:"casbin" yaml:"casbin"`
System System `mapstructure:"system" json:"system" yaml:"system"` System System `mapstructure:"system" json:"system" yaml:"system"`
Captcha Captcha `mapstructure:"captcha" json:"captcha" yaml:"captcha"` Captcha Captcha `mapstructure:"captcha" json:"captcha" yaml:"captcha"`
// auto // auto
......
[request_definition]
r = sub, obj, act
[policy_definition]
p = sub, obj, act
[role_definition]
g = _, _
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = r.sub == p.sub && keyMatch2(r.obj,p.obj) && r.act == p.act
...@@ -35,9 +35,17 @@ func (apiService *ApiService) CreateApi(api system.SysApi) (err error) { ...@@ -35,9 +35,17 @@ func (apiService *ApiService) CreateApi(api system.SysApi) (err error) {
//@return: err error //@return: err error
func (apiService *ApiService) DeleteApi(api system.SysApi) (err error) { func (apiService *ApiService) DeleteApi(api system.SysApi) (err error) {
err = global.GVA_DB.Delete(&api).Error var entity system.SysApi
CasbinServiceApp.ClearCasbin(1, api.Path, api.Method) err = global.GVA_DB.Where("id = ?", api.ID).First(&entity).Error // 根据id查询api记录
if errors.Is(err, gorm.ErrRecordNotFound) { // api记录不存在
return err return err
}
err = global.GVA_DB.Delete(&entity).Error
if err != nil {
return err
}
CasbinServiceApp.ClearCasbin(1, entity.Path, entity.Method)
return nil
} }
//@author: [piexlmax](https://github.com/piexlmax) //@author: [piexlmax](https://github.com/piexlmax)
......
...@@ -2,6 +2,8 @@ package system ...@@ -2,6 +2,8 @@ package system
import ( import (
"errors" "errors"
"github.com/casbin/casbin/v2/model"
"go.uber.org/zap"
"sync" "sync"
"github.com/casbin/casbin/v2" "github.com/casbin/casbin/v2"
...@@ -92,7 +94,28 @@ var ( ...@@ -92,7 +94,28 @@ var (
func (casbinService *CasbinService) Casbin() *casbin.SyncedEnforcer { func (casbinService *CasbinService) Casbin() *casbin.SyncedEnforcer {
once.Do(func() { once.Do(func() {
a, _ := gormadapter.NewAdapterByDB(global.GVA_DB) a, _ := gormadapter.NewAdapterByDB(global.GVA_DB)
syncedEnforcer, _ = casbin.NewSyncedEnforcer(global.GVA_CONFIG.Casbin.ModelPath, a) text := `
[request_definition]
r = sub, obj, act
[policy_definition]
p = sub, obj, act
[role_definition]
g = _, _
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = r.sub == p.sub && keyMatch2(r.obj,p.obj) && r.act == p.act
`
m, err := model.NewModelFromString(text)
if err != nil {
zap.L().Error("字符串加载模型失败!", zap.Error(err))
return
}
syncedEnforcer, _ = casbin.NewSyncedEnforcer(m, a)
}) })
_ = syncedEnforcer.LoadPolicy() _ = syncedEnforcer.LoadPolicy()
return syncedEnforcer return syncedEnforcer
......
...@@ -120,12 +120,6 @@ ...@@ -120,12 +120,6 @@
<el-button @click="email">测试邮件</el-button> <el-button @click="email">测试邮件</el-button>
</el-form-item> </el-form-item>
</el-collapse-item> </el-collapse-item>
<el-collapse-item title="casbin配置" name="6">
<el-form-item label="模型地址">
<el-input v-model="config.casbin['model-path']" />
</el-form-item>
</el-collapse-item>
<el-collapse-item title="验证码配置" name="7"> <el-collapse-item title="验证码配置" name="7">
<el-form-item label="字符长度"> <el-form-item label="字符长度">
<el-input v-model.number="config.captcha['key-long']" /> <el-input v-model.number="config.captcha['key-long']" />
...@@ -370,7 +364,6 @@ const config = ref({ ...@@ -370,7 +364,6 @@ const config = ref({
'iplimit-time': 0 'iplimit-time': 0
}, },
jwt: {}, jwt: {},
casbin: {},
mysql: {}, mysql: {},
pgsql: {}, pgsql: {},
excel: {}, excel: {},
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册