Skip to content

Commit f66c60e

Browse files
committed
Providing a more illustrative example with the JSON stored in an example file.
1 parent 8594beb commit f66c60e

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ func main() {
3838
}
3939
```
4040

41+
## Policy JSON
42+
43+
The following illustrates the expected JSON format for a policy. The [rbac_policy.json](examples/rbac_policy.json) has the same policy found in [rbac_policy.csv](examples/rbac_policy.csv).
44+
45+
```json
46+
[
47+
{"PType":"p","V0":"alice","V1":"data1","V2":"read"},
48+
{"PType":"p","V0":"bob","V1":"data2","V2":"write"},
49+
{"PType":"p","V0":"data2_admin","V1":"data2","V2":"read"},
50+
{"PType":"p","V0":"data2_admin","V1":"data2","V2":"write"},
51+
{"PType":"g","V0":"alice","V1":"data2_admin"}
52+
]
53+
```
54+
4155
## Getting Help
4256

4357
- [Casbin](https://github.com/casbin/casbin)

adapter_test.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
package jsonadapter
1616

1717
import (
18+
"io/ioutil"
1819
"log"
20+
"path/filepath"
1921
"testing"
2022

2123
"github.com/casbin/casbin/v2"
@@ -32,27 +34,9 @@ func testGetPolicy(t *testing.T, e *casbin.Enforcer, res [][]string) {
3234
}
3335

3436
func TestAdapter(t *testing.T) {
35-
// Because the JSON Buffer is empty at first,
36-
// so we need to load the policy from the file adapter (.CSV) first.
37-
e, _ := casbin.NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
38-
39-
b := []byte{}
37+
b, _ := ioutil.ReadFile(filepath.Join("examples", "rbac_policy.json"))
4038
a := NewAdapter(&b)
41-
// This is a trick to save the current policy to the JSON Buffer.
42-
// We can't call e.SavePolicy() because the adapter in the enforcer is still the file adapter.
43-
// The current policy means the policy in the Casbin enforcer (aka in memory).
44-
a.SavePolicy(e.GetModel())
45-
46-
// Clear the current policy.
47-
e.ClearPolicy()
48-
testGetPolicy(t, e, [][]string{})
49-
50-
// Load the policy from JSON Buffer.
51-
a.LoadPolicy(e.GetModel())
52-
testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}})
53-
54-
// Note: you don't need to look at the above code
55-
// if you already have a working JSON Buffer with policy inside.
39+
e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)
5640

5741
// Now the JSON Buffer has policy, so we can provide a normal use case.
5842
// Create an adapter and an enforcer.

examples/rbac_policy.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{"PType":"p","V0":"alice","V1":"data1","V2":"read"},
3+
{"PType":"p","V0":"bob","V1":"data2","V2":"write"},
4+
{"PType":"p","V0":"data2_admin","V1":"data2","V2":"read"},
5+
{"PType":"p","V0":"data2_admin","V1":"data2","V2":"write"},
6+
{"PType":"g","V0":"alice","V1":"data2_admin"}
7+
]

0 commit comments

Comments
 (0)