Skip to content
0

katanakit-js / HierarchicalAccessStrategy

Class: HierarchicalAccessStrategy ​

Defined in: src/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.

Example ​

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

Implements ​

Constructors ​

Constructor ​

ts
new HierarchicalAccessStrategy(): HierarchicalAccessStrategy;

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

Returns ​

HierarchicalAccessStrategy

Methods ​

useCan() ​

ts
useCan(subject, capability): boolean;

Defined in: src/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.

Parameters ​

ParameterType
subject{ [key: string]: unknown; id: string | number; roles: string[]; }
subject.idstring | number
subject.rolesstring[]
capabilitystring

Returns ​

boolean

Example ​

ts
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

Implementation of ​

IAccessStrategy.useCan


useCapabilitiesFor() ​

ts
useCapabilitiesFor(role): string[];

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

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

Parameters ​

ParameterType
rolestring

Returns ​

string[]

Example ​

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

Implementation of ​

IAccessStrategy.useCapabilitiesFor


useHasRole() ​

ts
useHasRole(subject, role): boolean;

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

Exact role membership - no hierarchy involved.

Parameters ​

ParameterType
subject{ [key: string]: unknown; id: string | number; roles: string[]; }
subject.idstring | number
subject.rolesstring[]
rolestring

Returns ​

boolean

Implementation of ​

IAccessStrategy.useHasRole


useRegisterRole() ​

ts
useRegisterRole(definition): void;

Defined in: src/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.

Parameters ​

ParameterType
definition{ capabilities: string[]; label: string; rank: number; slug: string; }
definition.capabilitiesstring[]
definition.labelstring
definition.ranknumber
definition.slugstring

Returns ​

void

Implementation of ​

IAccessStrategy.useRegisterRole


useResetRoles() ​

ts
useResetRoles(): void;

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

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

Returns ​

void

Implementation of ​

IAccessStrategy.useResetRoles


useRoles() ​

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

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

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

Returns ​

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

Implementation of ​

IAccessStrategy.useRoles

Released under the MIT License.