This repository has been archived by the owner on Mar 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_test.go
74 lines (60 loc) · 1.62 KB
/
examples_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
70
71
72
73
74
package grbac
import "fmt"
func ExampleRole_simple() {
// Create a new role
roleU := NewRole("User")
// Add the permissions to the role
roleU.Permit("CreateMsg")
roleU.Permit("ReadMsg")
// Check the permission
if roleU.IsAllowed("ReadMsg") {
fmt.Println("ReadMsg permission is allowed in the User role")
}
// Output: ReadMsg permission is allowed in the User role
}
func ExampleRole() {
// Define new permissions
permA := "PermA"
permB := "PermB"
permC := "PermC"
permD := "PermD"
permD1 := "PermD1"
permE := "PermE"
// Create new roles and bind to them the permissions and parents
roleA := NewRole("RoleA")
roleA.Permit(permA)
roleB := NewRole("RoleB")
roleB.Permit(permB)
roleC := NewRole("RoleC")
roleC.Permit(permC)
roleC.SetParent(roleA)
roleC.SetParent(roleB)
roleD := NewRole("RoleD")
roleD.Permit(permD)
roleD.Permit(permD1)
roleD.SetParent(roleA)
roleD.SetParent(roleC)
roleE := NewRole("RoleE")
roleE.Permit(permE)
roleE.SetParent(roleA)
roleE.SetParent(roleC)
roleE.SetParent(roleD)
// Check the permission
if roleE.IsAllowed(permA, permB, permC, permD, permD1, permE) {
fmt.Println("All permissions are allowed in the RoleE role")
}
// Output: All permissions are allowed in the RoleE role
}
func ExampleRole_SetParent() {
roleU := NewRole("User")
roleU.Permit("CreateMsg")
roleU.Permit("ReadMsg")
roleA := NewRole("Admin")
roleA.SetParent(roleU)
roleA.Permit("EditMsg")
roleA.Permit("DelMsg")
if roleA.IsAllowed("CreateMsg", "ReadMsg", "EditMsg", "DelMsg") {
fmt.Println("All permissions are allowed in the Admin role")
}
// Output: All permissions are allowed in the Admin role
}