1 Star 12 Fork 11

NilOrganization / naas

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
adapter.go 2.75 KB
一键复制 编辑 原始数据 按行查看 历史
Keng 提交于 2020-11-29 13:23 . 添加资源服务器GRPC
package adapter
// 说明:参考 https://github.com/casbin/casbin/blob/master/persist/file-adapter/adapter.go
import (
"context"
"errors"
"strings"
"github.com/casbin/casbin/v2/model"
"github.com/nilorg/naas/pkg/proto"
"google.golang.org/grpc"
)
// Adapter is the file adapter for Casbin.
// It can load policy from naas grpc.
type Adapter struct {
ctx context.Context
client proto.CasbinAdapterClient
ResourceID string
ResourceSecret string
}
// NewAdapter is the constructor for Adapter.
func NewAdapter(ctx context.Context, clientConn grpc.ClientConnInterface) *Adapter {
if ctx == nil {
ctx = context.Background()
}
return &Adapter{
ctx: ctx,
client: proto.NewCasbinAdapterClient(clientConn),
}
}
// loadPolicyLine loads a text line as a policy rule to model.
func loadPolicyLine(line string, model model.Model) {
if line == "" || strings.HasPrefix(line, "#") {
return
}
tokens := strings.Split(line, ",")
for i := 0; i < len(tokens); i++ {
tokens[i] = strings.TrimSpace(tokens[i])
}
key := tokens[0]
sec := key[:1]
model[sec][key].Policy = append(model[sec][key].Policy, tokens[1:])
}
// LoadPolicy loads all policy rules from the storage.
func (a *Adapter) LoadPolicy(model model.Model) error {
return a.loadPolicyGrpc(model, loadPolicyLine)
}
func (a *Adapter) loadPolicyGrpc(model model.Model, handler func(string, model.Model)) (err error) {
var resp *proto.LoadPolicyResponse
ctx := proto.SetResourceAuth(a.ctx, a.ResourceID, a.ResourceSecret)
resp, err = a.client.LoadPolicy(ctx, &proto.LoadPolicyRequest{})
if err != nil {
return
}
for _, policy := range resp.Policys {
line := strings.TrimSpace(policy)
handler(line, model)
}
return
}
// SavePolicy saves all policy rules to the storage.
func (a *Adapter) SavePolicy(model model.Model) error {
return errors.New("not implemented")
}
// AddPolicy adds a policy rule to the storage.
func (a *Adapter) AddPolicy(sec string, ptype string, rule []string) error {
return errors.New("not implemented")
}
// AddPolicies adds policy rules to the storage.
func (a *Adapter) AddPolicies(sec string, ptype string, rules [][]string) error {
return errors.New("not implemented")
}
// RemovePolicy removes a policy rule from the storage.
func (a *Adapter) RemovePolicy(sec string, ptype string, rule []string) error {
return errors.New("not implemented")
}
// RemovePolicies removes policy rules from the storage.
func (a *Adapter) RemovePolicies(sec string, ptype string, rules [][]string) error {
return errors.New("not implemented")
}
// RemoveFilteredPolicy removes policy rules that match the filter from the storage.
func (a *Adapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error {
return errors.New("not implemented")
}
Go
1
https://gitee.com/nilorg/naas.git
git@gitee.com:nilorg/naas.git
nilorg
naas
naas
master

搜索帮助