Changelog - 2026-03-02
Model Authorize Settings
In one line. A model can now declare its own authorization principal, so you stop repeating resource strings like 'Article' in route configs.
What changed
authorizein@modelsettings. Setsettings: { authorize: { principal: 'article' } }. The type isIModelAuthorizeSettings: a requiredprincipalplus any extra fields you want.AUTHORIZATION_SUBJECTis filled in for you. The@modeldecorator setsBaseEntity.AUTHORIZATION_SUBJECTfromauthorize.principal. A value you set on the class yourself wins.- Three
MetadataRegistrymethods.getModelAuthorizeSettings({ name })returns one model's settings.getAuthorizeModelPrincipals({ format })returns all principals as an'array'or a'record'.getAuthorizeModelSettings({ format })returns the full settings with each model's registry entry.
Who is affected
- Everyone. Additive only;
@modeldecorators withoutauthorizekeep working unchanged.
Details
typescript
@model({
type: 'entity',
settings: { authorize: { principal: 'article' } },
})
export class Article extends BaseEntity<typeof Article.schema> { /* ... */ }
Article.AUTHORIZATION_SUBJECT; // 'article'
authorize: {
action: AuthorizationActions.READ,
resource: Article.AUTHORIZATION_SUBJECT,
}
const registry = MetadataRegistry.getInstance();
registry.getModelAuthorizeSettings({ name: 'Article' }); // { principal: 'article' } | undefined
registry.getAuthorizeModelPrincipals({ format: 'array' }); // ['article', 'user', ...]
registry.getAuthorizeModelPrincipals({ format: 'record' }); // { Article: 'article', User: 'user', ... }