Skip to content

HierarchicalAccessStrategy

katanakit-js / HierarchicalAccessStrategy

Defined in: core/services/access.service.ts:113

Estrategia por defecto: control de acceso jerárquico basado en roles registrados. Es la estrategia estándar del servicio.

const strategy = new HierarchicalAccessStrategy();
strategy.useCan({ id: 1, roles: ["editor"] }, "content:create"); // true
new HierarchicalAccessStrategy(): HierarchicalAccessStrategy;

Defined in: core/services/access.service.ts:117

HierarchicalAccessStrategy

useCan(subject, capability): boolean;

Defined in: core/services/access.service.ts:196

Whether the subject may perform a capability. Any of the subject’s roles granting the capability (directly or by inheritance) is enough.

Parameter Type
subject { [key: string]: unknown; id: string | number; roles: string[]; }
subject.id? string | number
subject.roles? string[]
capability string

boolean

useCan({ id: 1, roles: ["author"] }, "content:publish"); // false
useCan({ id: 1, roles: ["author"] }, "content:create"); // true
useCan({ id: 2, roles: ["editor"] }, "content:create"); // true (inherits author)
useCan({ id: 3, roles: [] }, "conversations:use"); // false

IAccessStrategy.useCan


useCapabilitiesFor(role): string[];

Defined in: core/services/access.service.ts:140

Effective capabilities of a role: its own plus every less-privileged role’s.

Parameter Type
role string

string[]

useCapabilitiesFor("author"); // content:create, content:edit:own, ..., conversations:use
useCapabilitiesFor("guest"); // []

IAccessStrategy.useCapabilitiesFor


useHasRole(subject, role): boolean;

Defined in: core/services/access.service.ts:180

Exact role membership - no hierarchy involved.

Parameter Type
subject { [key: string]: unknown; id: string | number; roles: string[]; }
subject.id? string | number
subject.roles? string[]
role string

boolean

IAccessStrategy.useHasRole


useRegisterRole(definition): void;

Defined in: core/services/access.service.ts:161

Registers (or replaces) a role definition. Use it to extend a built-in role’s capabilities, e.g. grant content:create to member, or to add a deployment-specific role that inherits from the built-ins by rank. The definition is copied, so mutating the caller’s object afterwards has no effect.

Parameter Type
definition { capabilities: string[]; label: string; rank: number; slug: string; }
definition.capabilities? string[]
definition.label? string
definition.rank? number
definition.slug? string

void

IAccessStrategy.useRegisterRole


useResetRoles(): void;

Defined in: core/services/access.service.ts:167

Restores the built-in role registry (mainly for tests).

void

IAccessStrategy.useResetRoles


useRoles(): {
capabilities: string[];
label: string;
rank: number;
slug: string;
}[];

Defined in: core/services/access.service.ts:175

Lists every registered role definition. Returns copies, not live state.

{ capabilities: string[]; label: string; rank: number; slug: string; }[]

IAccessStrategy.useRoles