Skip to content

Commit

Permalink
Merge pull request #20 from wumoxi/master
Browse files Browse the repository at this point in the history
fix:fixed a bug with the specified table prefix
  • Loading branch information
hsluoyz authored Aug 26, 2019
2 parents 8fcaf92 + e6fe0b3 commit 4bda006
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"github.com/lib/pq"
)

var tablePrefix string

type CasbinRule struct {
TablePrefix string `gorm:"-"`
PType string `gorm:"size:100"`
Expand Down Expand Up @@ -105,6 +107,8 @@ func NewAdapterByDBUsePrefix(db *gorm.DB, prefix string) (*Adapter, error) {
db: db,
}

tablePrefix = prefix

err := a.createTable()
if err != nil {
return nil, err
Expand Down Expand Up @@ -308,7 +312,7 @@ func (a *Adapter) filterQuery(db *gorm.DB, filter Filter) func(db *gorm.DB) *gor
}

func savePolicyLine(ptype string, rule []string) CasbinRule {
line := CasbinRule{}
line := CasbinRule{TablePrefix: tablePrefix}

line.PType = ptype
if len(rule) > 0 {
Expand Down Expand Up @@ -383,7 +387,7 @@ func (a *Adapter) RemovePolicy(sec string, ptype string, rule []string) error {

// RemoveFilteredPolicy removes policy rules that match the filter from the storage.
func (a *Adapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error {
line := CasbinRule{}
line := CasbinRule{TablePrefix: tablePrefix}

line.PType = ptype
if fieldIndex <= 0 && 0 < fieldIndex+len(fieldValues) {
Expand Down Expand Up @@ -437,6 +441,6 @@ func rawDelete(db *gorm.DB, line CasbinRule) error {
queryArgs = append(queryArgs, line.V5)
}
args := append([]interface{}{queryStr}, queryArgs...)
err := db.Delete(CasbinRule{}, args...).Error
err := db.Delete(CasbinRule{TablePrefix: tablePrefix}, args...).Error
return err
}

0 comments on commit 4bda006

Please sign in to comment.