Skip to content

Commit 97b017e

Browse files
authored
Merge pull request #2 from mousedownmike/feature/json_example
JSON Policy Example
2 parents 8594beb + 8d2df58 commit 97b017e

File tree

3 files changed

+64
-20
lines changed

3 files changed

+64
-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: 43 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"
@@ -31,33 +33,54 @@ func testGetPolicy(t *testing.T, e *casbin.Enforcer, res [][]string) {
3133
}
3234
}
3335

34-
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")
36+
func errorExpected(t *testing.T, err error) {
37+
if err == nil {
38+
t.Error("expected error")
39+
}
40+
}
3841

39-
b := []byte{}
42+
func TestAdapter(t *testing.T) {
43+
b, _ := ioutil.ReadFile(filepath.Join("examples", "rbac_policy.json"))
4044
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.
45+
e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)
46+
e.GetPolicy()
5647

5748
// Now the JSON Buffer has policy, so we can provide a normal use case.
5849
// Create an adapter and an enforcer.
5950
// NewEnforcer() will load the policy automatically.
6051
a = NewAdapter(&b)
6152
e, _ = casbin.NewEnforcer("examples/rbac_model.conf", a)
6253
testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}})
54+
55+
//Test Clear Policy
56+
e.ClearPolicy()
57+
testGetPolicy(t, e, [][]string{})
58+
59+
// Test Add Policy
60+
_, _ = e.AddPolicy("alice", "data1", "read")
61+
testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}})
62+
63+
// Add policies with up to 6 rule elements
64+
_, _ = e.AddPolicy("alice", "data1", "read", "indeterminate")
65+
_, _ = e.AddPolicy("alice", "domain1", "data1", "write", "indeterminate")
66+
_, _ = e.AddPolicy("alice", "domain1", "data1", "write", "indeterminate", "foo")
67+
_, _ = e.AddPolicy("alice", "domain1", "data1", "write", "indeterminate", "foo", "bar")
68+
69+
// Add grouping policy
70+
_, _ = e.AddGroupingPolicy("alice", "data2_admin")
71+
72+
// Test Save Policy
73+
expectedPolicies := len(e.GetPolicy()) + len(e.GetGroupingPolicy())
74+
_ = e.SavePolicy()
75+
if len(a.policy) != expectedPolicies {
76+
t.Errorf("expected %d policies, got %d", expectedPolicies, len(a.policy))
77+
}
78+
79+
// Not implemented methods
80+
err := a.AddPolicy("", "", []string{""})
81+
errorExpected(t, err)
82+
err = a.RemovePolicy("", "", []string{""})
83+
errorExpected(t, err)
84+
err = a.RemoveFilteredPolicy("", "", 0, "")
85+
errorExpected(t, err)
6386
}

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)