-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrule.go
47 lines (39 loc) · 876 Bytes
/
rule.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
package User
import (
_ "embed"
"github.com/casbin/casbin"
"github.com/casbin/gorm-adapter"
_ "github.com/go-sql-driver/mysql"
)
var (
//go:embed rule.casbin.txt
CasbinModel string
AuthEnforcer *casbin.Enforcer
)
func init() {
// 加载 casbin 鉴权规则
if enforcer, err := casbin.NewEnforcerSafe(
casbin.NewModel(CasbinModel),
gormadapter.NewAdapter(
"mysql",
"root:123456@tcp(127.0.0.1:3306)/",
),
); err != nil {
panic(err)
} else {
AuthEnforcer = enforcer
}
if err := AuthEnforcer.LoadPolicy(); err != nil {
panic(err)
}
insertPolicy()
}
func insertPolicy() {
AuthEnforcer.AddPolicy("admin", "/*", "*")
AuthEnforcer.AddPolicy("anonymous", "/login", "*")
AuthEnforcer.AddPolicy("member", "/logout", "*")
AuthEnforcer.AddPolicy("member", "/member/*", "*")
if err := AuthEnforcer.SavePolicy(); err != nil {
panic(err)
}
}