/
entfga.go
71 lines (57 loc) · 1.45 KB
/
entfga.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
package entfga
import (
"embed"
"entgo.io/ent/entc"
"entgo.io/ent/entc/gen"
)
var (
//go:embed templates/*
_templates embed.FS
)
type Config struct {
SoftDeletes bool
}
func (c Config) Name() string {
return "AuthzConfig"
}
// AuthzExtension implements entc.Extension.
type AuthzExtension struct {
entc.DefaultExtension
config *Config
}
type ExtensionOption = func(*AuthzExtension)
// NewFGAExtension creates a new fga extension
func NewFGAExtension(opts ...ExtensionOption) *AuthzExtension {
extension := &AuthzExtension{
// Set configuration defaults that can get overridden with ExtensionOption
config: &Config{
SoftDeletes: false,
},
}
for _, opt := range opts {
opt(extension)
}
return extension
}
// WithSoftDeletes ensure the delete hook is still used even when soft deletes
// change the Op to Update
func WithSoftDeletes() ExtensionOption {
return func(e *AuthzExtension) {
e.config.SoftDeletes = true
}
}
// Templates returns the generated templates which include the client and authz from mutation
func (e *AuthzExtension) Templates() []*gen.Template {
templates := []*gen.Template{
parseTemplate("authzFromMutation", "templates/authzFromMutation.tmpl"),
parseTemplate("authzChecks", "templates/authzChecks.tmpl"),
parseTemplate("client", "templates/client.tmpl"),
}
return templates
}
// Annotations of the AuthzExtension
func (e *AuthzExtension) Annotations() []entc.Annotation {
return []entc.Annotation{
e.config,
}
}