@anarchitects/auth-angular 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,17 +1,15 @@
1
- import * as _casl_ability from '@casl/ability';
2
- import { MongoAbility } from '@casl/ability';
3
1
  import { Action, Subject, PolicyRule } from '@anarchitects/auth-ts/models';
2
+ import { MongoAbility } from '@casl/ability';
4
3
 
5
- type AbilitySubject = Subject | (Record<string, unknown> & {
6
- __caslSubjectType__?: Subject;
7
- });
8
- type AppAbility = MongoAbility<[
9
- Action,
10
- AbilitySubject
11
- ], {
12
- conditions: Record<string, unknown>;
13
- }>;
14
- declare const createAppAbility: (rules: PolicyRule[]) => MongoAbility<_casl_ability.AbilityTuple, Record<string, unknown>>;
4
+ type AbilitySubject = Subject | object;
5
+ type AbilityResource = Record<string, unknown>;
6
+ type AppAbility = MongoAbility<[Action, AbilitySubject]>;
7
+ declare const createAppAbility: (rules: PolicyRule[]) => AppAbility;
8
+ declare const asAppAbilitySubject: <TResource extends AbilityResource>(subjectName: Subject, resource: TResource) => TResource & {
9
+ __caslSubjectType__: Subject;
10
+ };
11
+ declare const canAccessResource: <TResource extends AbilityResource>(ability: AppAbility | undefined, action: Action, subjectName: Subject, resource: TResource) => boolean;
12
+ declare const canAccessResourceField: <TResource extends AbilityResource>(ability: AppAbility | undefined, action: Action, subjectName: Subject, field: string, resource: TResource) => boolean;
15
13
 
16
- export { createAppAbility };
14
+ export { asAppAbilitySubject, canAccessResource, canAccessResourceField, createAppAbility };
17
15
  export type { AppAbility };
package/util/README.md CHANGED
@@ -5,12 +5,28 @@ Utility layer for Angular auth. Re-exported via `@anarchitects/auth-angular/util
5
5
  ## Exports
6
6
 
7
7
  - `createAppAbility(rules: PolicyRule[])`: wraps `createMongoAbility` and returns the typed `AppAbility` used throughout the auth domain.
8
+ - `canAccessResource(...)`: checks a concrete resource instance against the current ability.
9
+ - `canAccessResourceField(...)`: checks whether a specific field-level action is allowed for a concrete resource.
8
10
  - `AppAbility`: CASL ability type configured for `Action`/`Subject` pairs defined in `@anarchitects/auth-ts/models`.
9
11
 
12
+ ## When To Use These Helpers
13
+
14
+ Use this layer for concrete resource decisions, not coarse route metadata:
15
+
16
+ - `createAppAbility(rules)` builds the frontend CASL ability from validated RBAC rules
17
+ - `canAccessResource(...)` answers instance-level questions such as "may this user edit this post?"
18
+ - `canAccessResourceField(...)` answers field-sensitive UI questions such as inline title editing
19
+
20
+ If you only need coarse route-attempt semantics, use `policyGuard` and `RoutePolicy` instead of calling CASL directly here.
21
+
10
22
  ## Usage
11
23
 
12
24
  ```ts
13
- import { createAppAbility } from '@anarchitects/auth-angular/util';
25
+ import {
26
+ canAccessResource,
27
+ canAccessResourceField,
28
+ createAppAbility,
29
+ } from '@anarchitects/auth-angular/util';
14
30
  import type { PolicyRule } from '@anarchitects/auth-ts/models';
15
31
 
16
32
  const rules: PolicyRule[] = [
@@ -23,6 +39,16 @@ const ability = createAppAbility(rules);
23
39
  if (ability.can('manage', 'Project')) {
24
40
  // guarded feature logic
25
41
  }
42
+
43
+ const post = { id: 'post-1', authorId: 'user-1', title: 'Draft' };
44
+
45
+ if (canAccessResource(ability, 'update', 'Post', post)) {
46
+ // show edit button
47
+ }
48
+
49
+ if (canAccessResourceField(ability, 'update', 'Post', 'title', post)) {
50
+ // allow inline title editing
51
+ }
26
52
  ```
27
53
 
28
- For stateful orchestration examples, see `auth.store` in the state layer where the ability factory is integrated with the auth API responses.
54
+ Use these helpers for frontend instance-level decisions such as edit buttons, row actions, and resolved edit routes. Coarse route gating still belongs to `policyGuard`, and the backend must still enforce the final instance-level decision.