Skip to content

Multi-tenant with hooks for all operations? #2585

Answered by a8m
frederikhors asked this question in Q&A
Discussion options

You must be logged in to vote

I implemented with Ent a few different approaches for multi-tenacy. Named-database per tenant, tenant edge, and tenant_id field. The field/edge-based approaches were implemented with FilterRule as follows:

func FilterTenantRule() privacy.QueryRule {
	type TenantFilter interface {
		WhereTenantID(int)
	}
	return privacy.FilterFunc(func(ctx context.Context, f privacy.Filter) error {
		t := viewer.TenantFromContext(ctx)
		tf, ok := f.(FilterTenantRule)
		if !ok {
			return privacy.Denyf("unexpected filter type %T. All schemas must include the Tenant mixin", f)
		}
		tf.WhereTenantID(t.ID)
		// Skip to the next privacy rule (equivalent to return nil).
		return privacy.Skip
	})
}

Let me know i…

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@frederikhors
Comment options

@frederikhors
Comment options

@frederikhors
Comment options

Answer selected by frederikhors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants