forked from mikespook/gorbac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
69 lines (61 loc) · 1.78 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package gorbac_test
import (
"fmt"
"github.com/mikespook/gorbac"
)
/*
Suppose:
The role-a is inheriting from role-b.
The role-b is inheriting from role-c, role-d.
The role-c is individual.
The role-d is individual.
The role-e is inheriting from role-d.
Every roles have thire own permissions.
*/
func ExampleRbac() {
rbac := gorbac.New()
rA := gorbac.NewStdRole("role-a")
rB := gorbac.NewStdRole("role-b")
rC := gorbac.NewStdRole("role-c")
rD := gorbac.NewStdRole("role-d")
rE := gorbac.NewStdRole("role-e")
pA := gorbac.NewStdPermission("permission-a")
pB := gorbac.NewStdPermission("permission-b")
pC := gorbac.NewStdPermission("permission-c")
pD := gorbac.NewStdPermission("permission-d")
pE := gorbac.NewStdPermission("permission-e")
rA.Assign(pA)
rB.Assign(pB)
rC.Assign(pC)
rD.Assign(pD)
rE.Assign(pE)
rbac.Add(rA)
rbac.Add(rB)
rbac.Add(rC)
rbac.Add(rD)
rbac.Add(rE)
rbac.SetParent("role-a", "role-b")
rbac.SetParents("role-b", []string{"role-c", "role-d"})
rbac.SetParent("role-e", "role-d")
if rbac.IsGranted("role-a", pA, nil) &&
rbac.IsGranted("role-a", pB, nil) &&
rbac.IsGranted("role-a", pC, nil) &&
rbac.IsGranted("role-a", pD, nil) {
fmt.Println("The role-a has been granted permis-a, b, c and d.")
}
if rbac.IsGranted("role-b", pB, nil) &&
rbac.IsGranted("role-b", pC, nil) &&
rbac.IsGranted("role-b", pD, nil) {
fmt.Println("The role-b has been granted permis-b, c and d.")
}
// When a circle inheratance occurred,
rbac.SetParent("role-c", "role-a")
// it could be detected as following code:
if err := gorbac.InherCircle(rbac); err != nil {
fmt.Println("A circle inheratance occurred.")
}
// Output:
// The role-a has been granted permis-a, b, c and d.
// The role-b has been granted permis-b, c and d.
// A circle inheratance occurred.
}