Skip to content

Commit

Permalink
feat: ent use interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
goxiaoy committed Dec 24, 2022
1 parent 867ff6b commit 0b157bc
Show file tree
Hide file tree
Showing 76 changed files with 2,706 additions and 6,167 deletions.
File renamed without changes.
32 changes: 0 additions & 32 deletions ent/ent.go

This file was deleted.

86 changes: 54 additions & 32 deletions ent/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
- Enable [EntQL Filtering](https://entgo.io/docs/feature-flags/#entql-filtering) and [Privacy Layer](https://entgo.io/docs/feature-flags/#privacy-layer) features
Modify your `ent/generate.go`
```
go generate ... --feature privacy --feature entql ...
go generate ... --feature intercept,schema/snapshot ...
```
- Add global runtime hook to your client
```go
import sent "github.com/go-saas/saas/ent"
client.use(sent.Saas)
```
- Copy mixin into your schema


- Copy following codes to your schema folder
```go
type HasTenant struct {
mixin.Schema
Expand All @@ -22,38 +19,63 @@
}
}

func (HasTenant) Policy() ent.Policy {
return privacy.Policy{
Query: privacy.QueryPolicy{
FilterTenantRule(),
},
func (h HasTenant) Interceptors() []ent.Interceptor {
return []ent.Interceptor{
intercept.TraverseFunc(func(ctx context.Context, q intercept.Query) error {
e := data.FromMultiTenancyDataFilter(ctx)
if !e {
// Skip tenant filter
return nil
}
ct, _ := saas.FromCurrentTenant(ctx)
h.P(ct, q)
return nil
}),
}
}

func FilterTenantRule() privacy.QueryMutationRule {
type hasTenant interface {
Where(p entql.P)
WhereTenantID(p entql.StringP)
func (h HasTenant) P(t saas.TenantInfo, w interface{ WhereP(...func(*sql.Selector)) }) {
if len(t.GetId()) == 0 {
w.WhereP(
sql.FieldIsNull(h.Fields()[0].Descriptor().Name))
return
}
return privacy.FilterFunc(func(ctx context.Context, f privacy.Filter) error {
ct, _ := common.FromCurrentTenant(ctx)
e := data.FromMultiTenancyDataFilter(ctx)
hf, ok := f.(hasTenant)
if e && ok {
//apply data filter
if ct.GetId() == "" {
//host side
hf.Where(entql.FieldNil("tenant_id"))
} else {
hf.WhereTenantID(entql.StringEQ(ct.GetId()))
}
}
w.WhereP(
sql.FieldEQ(h.Fields()[0].Descriptor().Name, t.GetId()))
}

return privacy.Skip
})
func (h HasTenant) Hooks() []ent.Hook {
return []ent.Hook{
hook.On(
func(next ent.Mutator) ent.Mutator {
type hasTenant interface {
SetOp(ent.Op)
SetTenantID(ss *sql.NullString)
WhereP(...func(*sql.Selector))
}
return ent.MutateFunc(func(ctx context.Context, mutation ent.Mutation) (ent.Value, error) {
if hf, ok := mutation.(hasTenant); ok {
ct, _ := saas.FromCurrentTenant(ctx)
at := data.FromAutoSetTenantId(ctx)
if ok && at {
if ct.GetId() != "" {
//normalize tenant side only
hf.SetTenantID(&sql.NullString{
String: ct.GetId(),
Valid: true,
})
}
}
}
return next.Mutate(ctx, mutation)
})
},
ent.OpCreate|ent.OpUpdate|ent.OpUpdateOne,
),
}
}

```

- Embed mixin into your schema
```go
// Post holds the schema definition for the Post entity.
Expand Down
28 changes: 15 additions & 13 deletions examples/ent/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,39 @@ go 1.18
replace github.com/go-saas/saas => ../../

require (
entgo.io/ent v0.10.1
github.com/gin-gonic/gin v1.7.7
entgo.io/ent v0.11.5-0.20221222102126-038cc0cace07
github.com/gin-gonic/gin v1.8.1
github.com/go-saas/saas v0.0.0-00010101000000-000000000000
github.com/mattn/go-sqlite3 v1.14.10
github.com/mattn/go-sqlite3 v1.14.16
)

require (
ariga.io/atlas v0.3.7-0.20220303204946-787354f533c3 // indirect
ariga.io/atlas v0.8.4-0.20221212165942-e53dd27a603d // indirect
github.com/agext/levenshtein v1.2.1 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-openapi/inflect v0.19.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.10.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/go-playground/validator/v10 v10.11.1 // indirect
github.com/goccy/go-json v0.9.11 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/hcl/v2 v2.10.0 // indirect
github.com/hashicorp/hcl/v2 v2.13.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/ugorji/go/codec v1.2.6 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
github.com/zclconf/go-cty v1.8.0 // indirect
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
golang.org/x/mod v0.5.1 // indirect
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
golang.org/x/crypto v0.0.0-20220926161630-eccd6366d1be // indirect
golang.org/x/mod v0.7.0 // indirect
golang.org/x/net v0.0.0-20221002022538-bcab6841153b // indirect
golang.org/x/sys v0.2.0 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading

0 comments on commit 0b157bc

Please sign in to comment.