Changelog - 2026-05-27
Casbin Domain Matching Function Support
New FeatureIn one line. One policy line can now grant a role across every tenant, instead of one line per tenant.
What changed
- New
domainMatchingenforcer option. An opt-in{ roleDefinition, fn }registers a domain matching function (keyMatch,keyMatch2,keyMatch3,keyMatch4orregexMatch) on a role definition. A storedg, User_x, Role_y, *then matches any domain, whileg, User_x, Role_y, Merchant_Xstill matches onlyMerchant_X. - New
CasbinDomainMatchingFunctionsconstant class. It exposes the five functions plusSCHEME_SET,isValid()and theTCasbinDomainMatchingFunctiontype. - Fail-loud validation.
configure()throws if the configuredroleDefinitionis not declared in the Casbin model. - Survives reloads. The function persists across every
loadFilteredPolicyreload.
typescript
options: {
model: { driver: CasbinEnforcerModelDrivers.TEXT, definition: CASBIN_RBAC_MODEL },
adapter,
cached,
normalizePayloadFn,
domainMatching: { roleDefinition: 'g', fn: CasbinDomainMatchingFunctions.KEY_MATCH },
} satisfies ICasbinEnforcerOptions,Your Casbin model's matcher must call g with the request domain and apply the matching function:
ini
[matchers]
m = g(r.sub, p.sub, r.dom) && keyMatch(r.dom, p.dom) && r.obj == p.obj && r.act == p.actWho is affected
- Multi-tenant apps using Casbin RBAC with domains. No action needed. The option is off by default and domains compare as exact strings unless you opt in.
- Apps where a user holds a role across many tenants. Opting in cuts the user's policy line count from
permissions x domainstomemberships + permissions.
Details
- The function is applied as
fn(requestDomain, policyDomain), so the wildcard must be on the stored policy side:keyMatch("Merchant_X", "*")istrue. KEY_MATCHtreats*as the only wildcard.KEY_MATCH_2adds:paramsegments,KEY_MATCH_3adds{param}segments,KEY_MATCH_4adds repeated-name equality checks, andREGEX_MATCHtreats the policy value as a full regular expression.
WARNING
With a domain model (r = sub, dom, obj, act), normalizePayloadFn must always return a domain. If it returns undefined, the enforcer falls back to the 3-argument enforceSync form, which mis-evaluates against a 4-argument model.