Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sapk committed Jul 6, 2023
1 parent 5113e7a commit 0a5a527
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 66 deletions.
72 changes: 19 additions & 53 deletions persist/string-adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,87 +15,53 @@
package stringadapter

import (
"bytes"
"errors"
"strings"

"github.com/casbin/casbin/v3/model"
"github.com/casbin/casbin/v3/persist"
"github.com/casbin/casbin/v3/util"
)

// Adapter is the string adapter for Casbin.
// It can load policy from string or save policy to string.
type Adapter struct {
lines string
}

// NewAdapter is the constructor for Adapter.
func NewAdapter(lines string) *Adapter {
return &Adapter{lines: lines}
}
// String is the string adapter for Casbin.
// It can load policy from string.
type String string

// LoadPolicy loads all policy rules from the storage.
func (a *Adapter) LoadPolicy(model model.Model) error {
if a.lines == "" {
return errors.New("invalid lines, lines cannot be empty")
}

rows := strings.Split(a.lines, "\n")
// LoadPolicy loads all policy rules from the string.
func (a String) LoadPolicy(model model.Model) error {
rows := strings.Split(string(a), "\n")
for _, line := range rows {
persist.LoadPolicyLine(strings.TrimSpace(line), model)
}

return nil
}

// SavePolicy saves all policy rules to the storage.
func (a *Adapter) SavePolicy(model model.Model) error {
var tmp bytes.Buffer
ptypes := model.GetPtypes("p")
for _, ptype := range ptypes {
for _, rule := range model.GetPolicy("p", ptype) {
tmp.WriteString(ptype + ", ")
tmp.WriteString(util.ArrayToString(rule))
tmp.WriteString("\n")
}
}

ptypes = model.GetPtypes("g")
for _, ptype := range ptypes {
for _, rule := range model.GetPolicy("g", ptype) {
tmp.WriteString(ptype + ", ")
tmp.WriteString(util.ArrayToString(rule))
tmp.WriteString("\n")
}
}

a.lines = strings.TrimRight(tmp.String(), "\n")

return nil
// SavePolicy is not supported by this adaptater.
func (a String) 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 {
// AddPolicy is not supported by this adaptater.
func (a String) 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 {
// AddPolicies is not supported by this adaptater.
func (a String) 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 {
// RemovePolicy is not supported by this adaptater.
func (a String) 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 {
// RemovePolicies is not supported by this adaptater.
func (a String) 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 {
// RemoveFilteredPolicy is not supported by this adaptater.
func (a String) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error {
return errors.New("not implemented")
}
22 changes: 11 additions & 11 deletions persist/string-adapter/adapter_filtered.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (
"github.com/casbin/casbin/v3/persist"
)

// FilteredAdapter is the filtered file adapter for Casbin. It can load policy
// from file or save policy to file and supports loading of filtered policies.
// FilteredAdapter is the filtered string adapter for Casbin. It can load policy
// from string and supports loading of filtered policies.
type FilteredAdapter struct {
*Adapter
String
filtered bool
}

Expand All @@ -37,17 +37,17 @@ type Filter struct {
}

// NewFilteredAdapter is the constructor for FilteredAdapter.
func NewFilteredAdapter(filePath string) *FilteredAdapter {
a := FilteredAdapter{}
a.filtered = true
a.Adapter = NewAdapter(filePath)
return &a
func NewFilteredAdapter(policy string) *FilteredAdapter {
return &FilteredAdapter{
filtered: false,
String: String(policy),
}
}

// LoadPolicy loads all policy rules from the storage.
func (a *FilteredAdapter) LoadPolicy(model model.Model) error {
a.filtered = false
return a.Adapter.LoadPolicy(model)
return a.String.LoadPolicy(model)
}

// LoadFilteredPolicy loads only policy rules that match the filter.
Expand All @@ -68,7 +68,7 @@ func (a *FilteredAdapter) LoadFilteredPolicy(model model.Model, filter interface
}

func (a *FilteredAdapter) loadFilteredPolicyFile(model model.Model, filter *Filter, handler func(string, model.Model)) error {
rows := strings.Split(a.lines, "\n")
rows := strings.Split(string(a.String), "\n")
for _, line := range rows {
line := strings.TrimSpace(line)
if filterLine(line, filter) {
Expand All @@ -91,7 +91,7 @@ func (a *FilteredAdapter) SavePolicy(model model.Model) error {
if a.filtered {
return errors.New("cannot save a filtered policy")
}
return a.Adapter.SavePolicy(model)
return errors.New("not implemented")
}

func filterLine(line string, filter *Filter) bool {
Expand Down
4 changes: 2 additions & 2 deletions persist/string-adapter/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ p, data_group_admin, /admin/*, POST
p, data_group_admin, /bob_data/*, POST
g, alice, data_group_admin
`
a := NewAdapter(line)
a := String(line)
m := model.NewModel()
err := m.LoadModelFromText(conf)
if err != nil {
Expand Down Expand Up @@ -84,7 +84,7 @@ p, data_group_admin, data3, read
p, data_group_admin, data3, write
g, alice, data_group_admin
`
a := NewAdapter(line)
a := String(line)
m := model.NewModel()
err := m.LoadModelFromText(conf)
if err != nil {
Expand Down

0 comments on commit 0a5a527

Please sign in to comment.