Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: revert "fix: add column v6, v7" and restore columns up to v5 #181

Merged
merged 1 commit into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 2 additions & 61 deletions adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ type CasbinRule struct {
V3 string `gorm:"size:100"`
V4 string `gorm:"size:100"`
V5 string `gorm:"size:100"`
V6 string `gorm:"size:25"`
V7 string `gorm:"size:25"`
}

func (CasbinRule) TableName() string {
Expand All @@ -65,8 +63,6 @@ type Filter struct {
V3 []string
V4 []string
V5 []string
V6 []string
V7 []string
}

type BatchFilter struct {
Expand Down Expand Up @@ -403,7 +399,7 @@ func (a *Adapter) createTable() error {
index := strings.ReplaceAll("idx_"+tableName, ".", "_")
hasIndex := a.db.Migrator().HasIndex(t, index)
if !hasIndex {
if err := a.db.Exec(fmt.Sprintf("CREATE UNIQUE INDEX %s ON %s (ptype,v0,v1,v2,v3,v4,v5,v6,v7)", index, tableName)).Error; err != nil {
if err := a.db.Exec(fmt.Sprintf("CREATE UNIQUE INDEX %s ON %s (ptype,v0,v1,v2,v3,v4,v5)", index, tableName)).Error; err != nil {
return err
}
}
Expand All @@ -429,8 +425,7 @@ func (a *Adapter) truncateTable() error {
func loadPolicyLine(line CasbinRule, model model.Model) {
var p = []string{line.Ptype,
line.V0, line.V1, line.V2,
line.V3, line.V4, line.V5,
line.V6, line.V7}
line.V3, line.V4, line.V5}

index := len(p) - 1
for p[index] == "" {
Expand Down Expand Up @@ -521,12 +516,6 @@ func (a *Adapter) filterQuery(db *gorm.DB, filter Filter) func(db *gorm.DB) *gor
if len(filter.V5) > 0 {
db = db.Where("v5 in (?)", filter.V5)
}
if len(filter.V6) > 0 {
db = db.Where("v6 in (?)", filter.V6)
}
if len(filter.V7) > 0 {
db = db.Where("v7 in (?)", filter.V7)
}
return db
}
}
Expand All @@ -553,12 +542,6 @@ func (a *Adapter) savePolicyLine(ptype string, rule []string) CasbinRule {
if len(rule) > 5 {
line.V5 = rule[5]
}
if len(rule) > 6 {
line.V6 = rule[6]
}
if len(rule) > 7 {
line.V7 = rule[7]
}

return *line
}
Expand Down Expand Up @@ -674,12 +657,6 @@ func (a *Adapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int,
if fieldIndex <= 5 && 5 < fieldIndex+len(fieldValues) {
line.V5 = fieldValues[5-fieldIndex]
}
if fieldIndex <= 6 && 6 < fieldIndex+len(fieldValues) {
line.V6 = fieldValues[6-fieldIndex]
}
if fieldIndex <= 7 && 7 < fieldIndex+len(fieldValues) {
line.V7 = fieldValues[7-fieldIndex]
}
err = a.rawDelete(a.db, *line)
return err
}
Expand Down Expand Up @@ -722,14 +699,6 @@ func (a *Adapter) rawDelete(db *gorm.DB, line CasbinRule) error {
queryStr += " and v5 = ?"
queryArgs = append(queryArgs, line.V5)
}
if line.V6 != "" {
queryStr += " and v6 = ?"
queryArgs = append(queryArgs, line.V6)
}
if line.V7 != "" {
queryStr += " and v7 = ?"
queryArgs = append(queryArgs, line.V7)
}
args := append([]interface{}{queryStr}, queryArgs...)
err := db.Delete(a.getTableInstance(), args...).Error
return err
Expand Down Expand Up @@ -763,14 +732,6 @@ func appendWhere(line CasbinRule) (string, []interface{}) {
queryStr += " and v5 = ?"
queryArgs = append(queryArgs, line.V5)
}
if line.V6 != "" {
queryStr += " and v6 = ?"
queryArgs = append(queryArgs, line.V6)
}
if line.V7 != "" {
queryStr += " and v7 = ?"
queryArgs = append(queryArgs, line.V7)
}
return queryStr, queryArgs
}

Expand Down Expand Up @@ -823,12 +784,6 @@ func (a *Adapter) UpdateFilteredPolicies(sec string, ptype string, newPolicies [
if fieldIndex <= 5 && 5 < fieldIndex+len(fieldValues) {
line.V5 = fieldValues[5-fieldIndex]
}
if fieldIndex <= 6 && 6 < fieldIndex+len(fieldValues) {
line.V6 = fieldValues[6-fieldIndex]
}
if fieldIndex <= 7 && 7 < fieldIndex+len(fieldValues) {
line.V7 = fieldValues[7-fieldIndex]
}

newP := make([]CasbinRule, 0, len(newPolicies))
oldP := make([]CasbinRule, 0)
Expand Down Expand Up @@ -891,14 +846,6 @@ func (c *CasbinRule) queryString() (interface{}, []interface{}) {
queryStr += " and v5 = ?"
queryArgs = append(queryArgs, c.V5)
}
if c.V6 != "" {
queryStr += " and v6 = ?"
queryArgs = append(queryArgs, c.V6)
}
if c.V7 != "" {
queryStr += " and v7 = ?"
queryArgs = append(queryArgs, c.V7)
}

return queryStr, queryArgs
}
Expand Down Expand Up @@ -926,11 +873,5 @@ func (c *CasbinRule) toStringPolicy() []string {
if c.V5 != "" {
policy = append(policy, c.V5)
}
if c.V6 != "" {
policy = append(policy, c.V6)
}
if c.V7 != "" {
policy = append(policy, c.V7)
}
return policy
}
11 changes: 0 additions & 11 deletions adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,6 @@ func initAdapterWithGormInstanceAndCustomTable(t *testing.T, db *gorm.DB) *Adapt
V3 string `gorm:"size:128;uniqueIndex:unique_index"`
V4 string `gorm:"size:128;uniqueIndex:unique_index"`
V5 string `gorm:"size:128;uniqueIndex:unique_index"`
V6 string `gorm:"size:128;uniqueIndex:unique_index"`
V7 string `gorm:"size:128;uniqueIndex:unique_index"`
}

// Create an adapter
Expand Down Expand Up @@ -630,12 +628,3 @@ func TestAddPolicies(t *testing.T) {

testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}, {"jack", "data1", "read"}, {"jack2", "data1", "read"}})
}

func TestAddPoliciesFullColumn(t *testing.T) {
a := initAdapter(t, "mysql", "root:@tcp(127.0.0.1:3306)/", "casbin", "casbin_rule")
e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)
e.AddPolicies([][]string{{"jack", "data1", "read", "col3", "col4", "col5", "col6", "col7"}, {"jack2", "data1", "read", "col3", "col4", "col5", "col6", "col7"}})
e.LoadPolicy()

testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}, {"jack", "data1", "read", "col3", "col4", "col5", "col6", "col7"}, {"jack2", "data1", "read", "col3", "col4", "col5", "col6", "col7"}})
}