Files
upstream-ent/examples/privacytenant/ent/template/privacy.tmpl
upstream-mirror 4b3ad0b611
Some checks failed
Atlas CI Public / lint (push) Failing after 13s
Atlas CI Public / sync (push) Has been skipped
snapshot of https://github.com/ent/ent.git (master, 2026-09-15; blobs > 131072B omitted)
2026-09-15 22:12:36 +05:00

29 lines
1.1 KiB
Cheetah

{{/* The line below tells Intellij/GoLand to enable the autocompletion based on the *gen.Graph type. */}}
{{/* gotype: entgo.io/ent/entc/gen.Graph */}}
{{/* An example privacy rule that is appended to the generated privacy package. For more info: https://entgo.io/docs/privacy#template-based */}}
{{ define "privacy/additional/privacy" }}
// FilterTenantRule is a query/mutation rule that filters out entities that are not in the tenant.
func FilterTenantRule() QueryMutationRule {
// TenantsFilter is an interface to wrap WhereTenantID()
// predicate that is used by both `Group` and `User` schemas.
type TenantsFilter interface {
WhereTenantID(entql.IntP)
}
return FilterFunc(func(ctx context.Context, f Filter) error {
view := viewer.FromContext(ctx)
tid, ok := view.Tenant()
if !ok {
return Denyf("missing tenant information in viewer")
}
tf, ok := f.(TenantsFilter)
if !ok {
return Denyf("unexpected filter type %T", f)
}
// Make sure that a tenant reads only entities that have an edge to it.
tf.WhereTenantID(entql.IntEQ(tid))
// Skip to the next privacy rule (equivalent to return nil).
return Skip
})
}
{{ end }}