-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
189 lines (157 loc) · 4.22 KB
/
main.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"fmt"
"log"
"github.com/gogf/gf/v2/database/gdb"
"github.com/dobyte/gf-casbin"
)
var enforcer *casbin.Enforcer
func init() {
gdb.SetConfig(gdb.Config{
"default": gdb.ConfigGroup{
gdb.ConfigNode{
Host: "127.0.0.1",
Port: "3306",
User: "root",
Pass: "123456",
Name: "topic1",
Type: "mysql",
Role: "master",
Weight: 100,
},
},
})
db, err := gdb.Instance()
if err != nil {
log.Fatalf("Database init failure:%s \n", err.Error())
}
e, err := casbin.NewEnforcer(&casbin.Options{
Model: "./example/model.conf",
Debug: true,
Enable: true,
AutoLoad: true,
Table: "casbin_policy_test",
DB: db,
// Link: "mysql:root:123456@tcp(127.0.0.1:3306)/topic1",
// Link: "pgsql:user=root password=123456 host=127.0.0.1 port=5432 dbname=topic1",
})
if err != nil {
log.Fatalf("Casbin init failure:%s \n", err.Error())
}
enforcer = e
}
func main() {
var (
ok bool
policies [][]string
)
// add a permission node for role
_, _ = enforcer.AddPolicy("role_1", "node_1")
// batch add permission nodes for roles
_, _ = enforcer.AddPolicies([][]string{
{"role_2", "node_2"},
{"role_3", "node_3"},
})
// add a role for user
_, _ = enforcer.AddGroupingPolicy("user_1", "role_1")
// batch add roles for users
_, _ = enforcer.AddGroupingPolicies([][]string{
{"user_2", "role_2"},
{"user_3", "role_3"},
})
// check role_1 policy
if ok = enforcer.HasPolicy("role_1", "node_1"); ok {
fmt.Println("role_1 is allowed access node_1")
} else {
fmt.Println("role_1 is not allowed access node_1")
}
// check role_1 policy
if ok = enforcer.HasPolicy("role_1", "node_2"); ok {
fmt.Println("role_1 is allowed access node_2")
} else {
fmt.Println("role_1 is not allowed access node_2")
}
// check user_1 policy
if ok = enforcer.HasGroupingPolicy("user_1", "role_1"); ok {
fmt.Println("user_1 has role_1")
} else {
fmt.Println("user_1 has not role_1")
}
// check user_1 policy
if ok = enforcer.HasGroupingPolicy("user_1", "role_2"); ok {
fmt.Println("user_1 has role_2")
} else {
fmt.Println("user_1 has not role_2")
}
// check access permission of user_1
if ok, _ = enforcer.Enforce("user_1", "node_1"); ok {
fmt.Println("user_1 is allowed access node_1")
} else {
fmt.Println("user_1 is not allowed access node_1")
}
// check access permission of user_1
if ok, _ = enforcer.Enforce("user_1", "node_2"); ok {
fmt.Println("user_1 is allowed access node_2")
} else {
fmt.Println("user_1 is not allowed access node_2")
}
// remove a policy
_, _ = enforcer.RemovePolicy("role_1", "node_1")
// get all policies
policies = enforcer.GetPolicy()
fmt.Println()
fmt.Println("all policies:")
fmt.Println(policies)
// batch remove policies
_, _ = enforcer.RemovePolicies([][]string{
{"role_2", "node_2"},
{"role_3", "node_3"},
})
// get all policies
policies = enforcer.GetPolicy()
fmt.Println()
fmt.Println("all policies:")
fmt.Println(policies)
// remove a grouping policy
_, _ = enforcer.RemoveGroupingPolicy("user_1", "role_1")
// get all grouping policies
policies = enforcer.GetGroupingPolicy()
fmt.Println()
fmt.Println("all grouping policies:")
fmt.Println(policies)
// batch remove grouping policies
_, _ = enforcer.RemoveGroupingPolicies([][]string{
{"user_2", "role_2"},
{"user_3", "role_3"},
})
// get all grouping policies
policies = enforcer.GetGroupingPolicy()
fmt.Println()
fmt.Println("all grouping policies:")
fmt.Println(policies)
fmt.Println()
// check role_1 policy
if ok = enforcer.HasPolicy("role_1", "node_1"); ok {
fmt.Println("role_1 is allowed access node_1")
} else {
fmt.Println("role_1 is not allowed access node_1")
}
// check role_1 policy
if ok = enforcer.HasPolicy("role_1", "node_2"); ok {
fmt.Println("role_1 is allowed access node_2")
} else {
fmt.Println("role_1 is not allowed access node_2")
}
// check user_1 policy
if ok = enforcer.HasGroupingPolicy("user_1", "role_1"); ok {
fmt.Println("user_1 has role_1")
} else {
fmt.Println("user_1 has not role_1")
}
// check user_1 policy
if ok = enforcer.HasGroupingPolicy("user_1", "role_2"); ok {
fmt.Println("user_1 has role_2")
} else {
fmt.Println("user_1 has not role_2")
}
}