-
Notifications
You must be signed in to change notification settings - Fork 4
/
model.go
61 lines (51 loc) · 1.69 KB
/
model.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
package firestoreadapter
import (
"context"
"io/ioutil"
"cloud.google.com/go/firestore"
"github.com/casbin/casbin/v2/model"
)
type CasbinModelConf struct {
Text string `firestore:"text"`
}
// SaveModel loads a casbin model definition from the specified file and store it to Firestore.
func SaveModel(db *firestore.Client, path string) error {
return SaveModelWithConfig(db, path, Config{Collection:defaultCollectionName})
}
// SaveModel loads a casbin model definition from the specified file and store it to Firestore.
func SaveModelWithConfig(client *firestore.Client, path string, config Config) error {
b, err := ioutil.ReadFile(path)
if err != nil {
return err
}
text := string(b)
// Validate the specified config.
if _, err = model.NewModelFromString(text); err != nil {
return err
}
ctx := context.Background()
err = client.RunTransaction(ctx, func(ctx context.Context, tx *firestore.Transaction) error {
ref := client.Collection(config.collectionName()).Doc("conf")
m := CasbinModelConf{text}
return tx.Set(ref, &m)
})
return err
}
// LoadModel loads a casbin model definition from Firestore.
func LoadModel(client *firestore.Client) (model.Model, error) {
return LoadModelWithConfig(client, Config{Collection:defaultCollectionName})
}
// LoadModel loads a casbin model definition from Firestore.
func LoadModelWithConfig(client *firestore.Client, config Config) (model.Model, error) {
ctx := context.Background()
ref := client.Collection(config.collectionName()).Doc("conf")
docsnap, err := ref.Get(ctx)
if err != nil {
return nil, err
}
var conf CasbinModelConf
if err = docsnap.DataTo(&conf); err != nil {
return nil, err
}
return model.NewModelFromString(conf.Text)
}