This repository was archived by the owner on Dec 20, 2025. It is now read-only.
forked from Blank-Xu/sql-adapter
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
121 lines (98 loc) · 2.4 KB
/
Copy pathmain.go
File metadata and controls
121 lines (98 loc) · 2.4 KB
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
package main
import (
"bufio"
"database/sql"
"fmt"
"log"
"os"
"strings"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/casbin/casbin/v2"
sqladapter "github.com/nucleuscloud/sql-adapter"
)
const (
driverName = "mysql"
tableName = "casbin_rule_example"
envFile = "../../test/.env"
rbacModelFile = "../../test/testdata/rbac_model.conf"
)
var dataSource = func() string {
envMap := loadEnvfile(envFile)
return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8",
envMap["TEST_DATABASE_USER"],
envMap["TEST_DATABASE_PASSWORD"],
envMap["TEST_DATABASE_HOST"],
envMap["TEST_DATABASE_PORT_MYSQL"],
envMap["TEST_DATABASE_NAME"])
}()
func main() {
// connect to the database first.
db, err := sql.Open(driverName, dataSource)
if err != nil {
panic(err)
}
if err = db.Ping(); err != nil {
panic(err)
}
defer db.Close()
db.SetMaxOpenConns(20)
db.SetMaxIdleConns(10)
db.SetConnMaxLifetime(time.Minute * 10)
// Initialize an adapter and use it in a Casbin enforcer:
// The adapter will use the MySQL table name "casbin_rule_example",
// the default table name is "casbin_rule".
// If it doesn't exist, the adapter will create it automatically.
a, err := sqladapter.NewAdapter(db, driverName, tableName)
if err != nil {
panic(err)
}
e, err := casbin.NewEnforcer(rbacModelFile, a)
if err != nil {
panic(err)
}
// Load the policies from DB.
if err = e.LoadPolicy(); err != nil {
log.Println("LoadPolicy failed, err: ", err)
}
// Check the permission.
has, err := e.Enforce("alice", "data1", "read")
if err != nil {
log.Println("Enforce failed, err: ", err)
}
if !has {
log.Println("do not have permission")
}
// Modify the policy.
// e.AddPolicy(...)
// e.RemovePolicy(...)
// Save the policy back to DB.
if err = e.SavePolicy(); err != nil {
log.Println("SavePolicy failed, err: ", err)
}
}
func loadEnvfile(envFile string) map[string]string {
f, err := os.Open(envFile)
if err != nil {
panic(err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanLines)
m := make(map[string]string)
for scanner.Scan() {
text := strings.TrimSpace(scanner.Text())
if text == "" || text[0] == '#' {
continue
}
s := strings.Split(text, "=")
if len(s) < 2 {
panic("invalid env file: " + envFile)
}
m[s[0]] = strings.Join(s[1:], "")
}
if err = scanner.Err(); err != nil {
panic("load env file failed, err: " + err.Error())
}
return m
}