Skip to content

Commit

Permalink
feat: upgrade casbin to v2.55.1 and add preview feature (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
huangkuan123 authored Oct 6, 2022
1 parent e1dae28 commit 5ffa22b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
60 changes: 51 additions & 9 deletions adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func finalizer(a *Adapter) {
}
}

//Select conn according to table name(use map store name-index)
// Select conn according to table name(use map store name-index)
type specificPolicy int

func (p *specificPolicy) Resolve(connPools []gorm.ConnPool) gorm.ConnPool {
Expand All @@ -115,8 +115,10 @@ func (dbPool *DbPool) switchDb(dbName string) *gorm.DB {

// NewAdapter is the constructor for Adapter.
// Params : databaseName,tableName,dbSpecified
// databaseName,{tableName/dbSpecified}
// {database/dbSpecified}
//
// databaseName,{tableName/dbSpecified}
// {database/dbSpecified}
//
// databaseName and tableName are user defined.
// Their default value are "casbin" and "casbin_rule"
//
Expand Down Expand Up @@ -424,7 +426,7 @@ func (a *Adapter) truncateTable() error {
return a.db.Exec(fmt.Sprintf("truncate table %s", a.getFullTableName())).Error
}

func loadPolicyLine(line CasbinRule, model model.Model) {
func loadPolicyLine(line CasbinRule, model model.Model) error {
var p = []string{line.Ptype,
line.V0, line.V1, line.V2,
line.V3, line.V4, line.V5}
Expand All @@ -435,8 +437,11 @@ func loadPolicyLine(line CasbinRule, model model.Model) {
}
index += 1
p = p[:index]

persist.LoadPolicyArray(p, model)
err := persist.LoadPolicyArray(p, model)
if err != nil {
return nil
}
return err
}

// LoadPolicy loads policy from database.
Expand All @@ -445,9 +450,15 @@ func (a *Adapter) LoadPolicy(model model.Model) error {
if err := a.db.Order("ID").Find(&lines).Error; err != nil {
return err
}

err := a.Preview(&lines, model)
if err != nil {
return err
}
for _, line := range lines {
loadPolicyLine(line, model)
err := loadPolicyLine(line, model)
if err != nil {
return err
}
}

return nil
Expand Down Expand Up @@ -481,7 +492,10 @@ func (a *Adapter) LoadFilteredPolicy(model model.Model, filter interface{}) erro
}

for _, line := range lines {
loadPolicyLine(line, model)
err := loadPolicyLine(line, model)
if err != nil {
return err
}
}
}
a.isFiltered = true
Expand Down Expand Up @@ -847,6 +861,34 @@ func (a *Adapter) UpdateFilteredPolicies(sec string, ptype string, newPolicies [
return oldPolicies, tx.Commit().Error
}

// Preview Pre-checking to avoid causing partial load success and partial failure deep
func (a *Adapter) Preview(rules *[]CasbinRule, model model.Model) error {
j := 0
for i, rule := range *rules {
r := []string{rule.Ptype,
rule.V0, rule.V1, rule.V2,
rule.V3, rule.V4, rule.V5}
index := len(r) - 1
for r[index] == "" {
index--
}
index += 1
p := r[:index]
key := p[0]
sec := key[:1]
ok, err := model.HasPolicyEx(sec, key, p[1:])
if err != nil {
return err
}
if ok {
(*rules)[j], (*rules)[i] = rule, (*rules)[j]
j++
}
}
(*rules) = (*rules)[j:]
return nil
}

func (c *CasbinRule) queryString() (interface{}, []interface{}) {
queryArgs := []interface{}{c.Ptype}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/casbin/gorm-adapter/v3
go 1.14

require (
github.com/casbin/casbin/v2 v2.37.4
github.com/casbin/casbin/v2 v2.55.1
github.com/glebarez/sqlite v1.4.3
github.com/go-sql-driver/mysql v1.6.0
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030I
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/casbin/casbin/v2 v2.37.4 h1:RWSKPjaZ8JlOBlcW1bI/FTII8OPxvQ9jVy9JwyNL6DQ=
github.com/casbin/casbin/v2 v2.37.4/go.mod h1:vByNa/Fchek0KZUgG5wEsl7iFsiviAYKRtgrQfcJqHg=
github.com/casbin/casbin/v2 v2.55.1 h1:vaTAHSLkQfielg9UiHdIdvIVK/NAmMjBkDkrOM9iDqI=
github.com/casbin/casbin/v2 v2.55.1/go.mod h1:vByNa/Fchek0KZUgG5wEsl7iFsiviAYKRtgrQfcJqHg=
github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
Expand Down

0 comments on commit 5ffa22b

Please sign in to comment.