Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
C
casbin-pg-adapter
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Abby Cin
casbin-pg-adapter
Commits
d951292b
Unverified
Commit
d951292b
authored
May 11, 2020
by
Zixuan Liu
Committed by
GitHub
May 11, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #6 from DivyPatel9881/batch_op
fix: Add batch operations to pg adapter.
parents
13bf718c
d7fa4f3e
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
79 additions
and
1 deletion
+79
-1
adapter.go
adapter.go
+35
-0
adapter_test.go
adapter_test.go
+40
-0
go.mod
go.mod
+1
-1
go.sum
go.sum
+3
-0
No files found.
adapter.go
View file @
d951292b
...
@@ -233,6 +233,24 @@ func (a *Adapter) AddPolicy(sec string, ptype string, rule []string) error {
...
@@ -233,6 +233,24 @@ func (a *Adapter) AddPolicy(sec string, ptype string, rule []string) error {
return
err
return
err
}
}
// AddPolicies adds policy rules to the storage.
func
(
a
*
Adapter
)
AddPolicies
(
sec
string
,
ptype
string
,
rules
[][]
string
)
error
{
var
lines
[]
*
CasbinRule
for
_
,
rule
:=
range
rules
{
line
:=
savePolicyLine
(
ptype
,
rule
)
lines
=
append
(
lines
,
line
)
}
err
:=
a
.
db
.
RunInTransaction
(
func
(
tx
*
pg
.
Tx
)
error
{
_
,
err
:=
tx
.
Model
(
&
lines
)
.
OnConflict
(
"DO NOTHING"
)
.
Insert
()
return
err
})
return
err
}
// RemovePolicy removes a policy rule from the storage.
// RemovePolicy removes a policy rule from the storage.
func
(
a
*
Adapter
)
RemovePolicy
(
sec
string
,
ptype
string
,
rule
[]
string
)
error
{
func
(
a
*
Adapter
)
RemovePolicy
(
sec
string
,
ptype
string
,
rule
[]
string
)
error
{
line
:=
savePolicyLine
(
ptype
,
rule
)
line
:=
savePolicyLine
(
ptype
,
rule
)
...
@@ -240,6 +258,23 @@ func (a *Adapter) RemovePolicy(sec string, ptype string, rule []string) error {
...
@@ -240,6 +258,23 @@ func (a *Adapter) RemovePolicy(sec string, ptype string, rule []string) error {
return
err
return
err
}
}
// RemovePolicies removes policy rules from the storage.
func
(
a
*
Adapter
)
RemovePolicies
(
sec
string
,
ptype
string
,
rules
[][]
string
)
error
{
var
lines
[]
*
CasbinRule
for
_
,
rule
:=
range
rules
{
line
:=
savePolicyLine
(
ptype
,
rule
)
lines
=
append
(
lines
,
line
)
}
err
:=
a
.
db
.
RunInTransaction
(
func
(
tx
*
pg
.
Tx
)
error
{
_
,
err
:=
tx
.
Model
(
&
lines
)
.
Delete
()
return
err
})
return
err
}
// RemoveFilteredPolicy removes policy rules that match the filter from the storage.
// RemoveFilteredPolicy removes policy rules that match the filter from the storage.
func
(
a
*
Adapter
)
RemoveFilteredPolicy
(
sec
string
,
ptype
string
,
fieldIndex
int
,
fieldValues
...
string
)
error
{
func
(
a
*
Adapter
)
RemoveFilteredPolicy
(
sec
string
,
ptype
string
,
fieldIndex
int
,
fieldValues
...
string
)
error
{
query
:=
a
.
db
.
Model
((
*
CasbinRule
)(
nil
))
.
Where
(
"p_type = ?"
,
ptype
)
query
:=
a
.
db
.
Model
((
*
CasbinRule
)(
nil
))
.
Where
(
"p_type = ?"
,
ptype
)
...
...
adapter_test.go
View file @
d951292b
...
@@ -105,6 +105,35 @@ func (s *AdapterTestSuite) TestAutoSave() {
...
@@ -105,6 +105,35 @@ func (s *AdapterTestSuite) TestAutoSave() {
[][]
string
{{
"alice"
,
"data1"
,
"read"
},
{
"bob"
,
"data2"
,
"write"
},
{
"data2_admin"
,
"data2"
,
"read"
},
{
"data2_admin"
,
"data2"
,
"write"
},
{
"alice"
,
"data1"
,
"write"
}},
[][]
string
{{
"alice"
,
"data1"
,
"read"
},
{
"bob"
,
"data2"
,
"write"
},
{
"data2_admin"
,
"data2"
,
"read"
},
{
"data2_admin"
,
"data2"
,
"write"
},
{
"alice"
,
"data1"
,
"write"
}},
s
.
e
.
GetPolicy
(),
s
.
e
.
GetPolicy
(),
)
)
_
,
err
=
s
.
e
.
AddPolicies
([][]
string
{
{
"bob"
,
"data2"
,
"read"
},
{
"alice"
,
"data2"
,
"write"
},
{
"alice"
,
"data2"
,
"read"
},
{
"bob"
,
"data1"
,
"write"
},
{
"bob"
,
"data1"
,
"read"
},
})
s
.
Require
()
.
NoError
(
err
)
// Reload the policy from the storage to see the effect.
err
=
s
.
e
.
LoadPolicy
()
s
.
Require
()
.
NoError
(
err
)
// The policy has a new rule: {"alice", "data1", "write"}.
s
.
assertPolicy
(
[][]
string
{
{
"alice"
,
"data1"
,
"read"
},
{
"bob"
,
"data2"
,
"write"
},
{
"data2_admin"
,
"data2"
,
"read"
},
{
"data2_admin"
,
"data2"
,
"write"
},
{
"alice"
,
"data1"
,
"write"
},
{
"bob"
,
"data2"
,
"read"
},
{
"alice"
,
"data2"
,
"write"
},
{
"alice"
,
"data2"
,
"read"
},
{
"bob"
,
"data1"
,
"write"
},
{
"bob"
,
"data1"
,
"read"
},
},
s
.
e
.
GetPolicy
(),
)
s
.
Require
()
.
NoError
(
err
)
s
.
Require
()
.
NoError
(
err
)
}
}
...
@@ -141,6 +170,17 @@ func (s *AdapterTestSuite) TestRemovePolicy() {
...
@@ -141,6 +170,17 @@ func (s *AdapterTestSuite) TestRemovePolicy() {
[][]
string
{{
"bob"
,
"data2"
,
"write"
},
{
"data2_admin"
,
"data2"
,
"read"
},
{
"data2_admin"
,
"data2"
,
"write"
}},
[][]
string
{{
"bob"
,
"data2"
,
"write"
},
{
"data2_admin"
,
"data2"
,
"read"
},
{
"data2_admin"
,
"data2"
,
"write"
}},
s
.
e
.
GetPolicy
(),
s
.
e
.
GetPolicy
(),
)
)
_
,
err
=
s
.
e
.
RemovePolicies
([][]
string
{
{
"data2_admin"
,
"data2"
,
"read"
},
{
"data2_admin"
,
"data2"
,
"write"
},
})
s
.
Require
()
.
NoError
(
err
)
s
.
assertPolicy
(
[][]
string
{{
"bob"
,
"data2"
,
"write"
}},
s
.
e
.
GetPolicy
(),
)
}
}
func
(
s
*
AdapterTestSuite
)
TestRemoveFilteredPolicy
()
{
func
(
s
*
AdapterTestSuite
)
TestRemoveFilteredPolicy
()
{
...
...
go.mod
View file @
d951292b
...
@@ -3,7 +3,7 @@ module github.com/casbin/casbin-pg-adapter
...
@@ -3,7 +3,7 @@ module github.com/casbin/casbin-pg-adapter
go 1.12
go 1.12
require (
require (
github.com/casbin/casbin/v2 v2.
1.2
github.com/casbin/casbin/v2 v2.
4.1
github.com/go-pg/pg/v9 v9.1.0
github.com/go-pg/pg/v9 v9.1.0
github.com/mmcloughlin/meow v0.0.0-20181112033425-871e50784daf
github.com/mmcloughlin/meow v0.0.0-20181112033425-871e50784daf
github.com/stretchr/testify v1.4.0
github.com/stretchr/testify v1.4.0
...
...
go.sum
View file @
d951292b
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible h1:1G1pk05UrOh0NlF1oeaaix1x8XzrfjIDK47TY0Zehcw=
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible h1:1G1pk05UrOh0NlF1oeaaix1x8XzrfjIDK47TY0Zehcw=
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/casbin/casbin v1.9.1 h1:ucjbS5zTrmSLtH4XogqOG920Poe6QatdXtz1FEbApeM=
github.com/casbin/casbin/v2 v2.1.2 h1:bTwon/ECRx9dwBy2ewRVr5OiqjeXSGiTUY74sDPQi/g=
github.com/casbin/casbin/v2 v2.1.2 h1:bTwon/ECRx9dwBy2ewRVr5OiqjeXSGiTUY74sDPQi/g=
github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
github.com/casbin/casbin/v2 v2.4.1 h1:1dDJbnUoCTUu5ysFhaYND0yY8YReQ+GDQ7yDy5FNHyY=
github.com/casbin/casbin/v2 v2.4.1/go.mod h1:XXtYGrs/0zlOsJMeRteEdVi/FsB0ph7KgNfjoCoJUD8=
github.com/codemodus/kace v0.5.1 h1:4OCsBlE2c/rSJo375ggfnucv9eRzge/U5LrrOZd47HA=
github.com/codemodus/kace v0.5.1 h1:4OCsBlE2c/rSJo375ggfnucv9eRzge/U5LrrOZd47HA=
github.com/codemodus/kace v0.5.1/go.mod h1:coddaHoX1ku1YFSe4Ip0mL9kQjJvKkzb9CfIdG1YR04=
github.com/codemodus/kace v0.5.1/go.mod h1:coddaHoX1ku1YFSe4Ip0mL9kQjJvKkzb9CfIdG1YR04=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment