@dudousxd/nestjs-authz 0.8.0 → 0.9.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.
Files changed (132) hide show
  1. package/CHANGELOG.md +115 -0
  2. package/README.md +279 -0
  3. package/dist/authz.guard.d.ts +48 -0
  4. package/dist/authz.guard.d.ts.map +1 -0
  5. package/dist/authz.guard.js +144 -0
  6. package/dist/authz.guard.js.map +1 -0
  7. package/dist/can-endpoint.controller.d.ts +0 -19
  8. package/dist/can-endpoint.controller.d.ts.map +1 -1
  9. package/dist/can-endpoint.controller.js +18 -9
  10. package/dist/can-endpoint.controller.js.map +1 -1
  11. package/dist/decision-provider.d.ts +25 -16
  12. package/dist/decision-provider.d.ts.map +1 -1
  13. package/dist/decision-provider.js +11 -1
  14. package/dist/decision-provider.js.map +1 -1
  15. package/dist/decisions.d.ts +24 -0
  16. package/dist/decisions.d.ts.map +1 -0
  17. package/dist/decisions.js +87 -0
  18. package/dist/decisions.js.map +1 -0
  19. package/dist/decorator/roles.decorator.d.ts +6 -12
  20. package/dist/decorator/roles.decorator.d.ts.map +1 -1
  21. package/dist/decorator/roles.decorator.js +13 -15
  22. package/dist/decorator/roles.decorator.js.map +1 -1
  23. package/dist/diagnostics.d.ts +15 -5
  24. package/dist/diagnostics.d.ts.map +1 -1
  25. package/dist/diagnostics.js +3 -1
  26. package/dist/diagnostics.js.map +1 -1
  27. package/dist/errors/exceptions.d.ts +0 -8
  28. package/dist/errors/exceptions.d.ts.map +1 -1
  29. package/dist/errors/exceptions.js +0 -14
  30. package/dist/errors/exceptions.js.map +1 -1
  31. package/dist/gate.d.ts +83 -209
  32. package/dist/gate.d.ts.map +1 -1
  33. package/dist/gate.js +209 -577
  34. package/dist/gate.js.map +1 -1
  35. package/dist/helpers.d.ts +46 -0
  36. package/dist/helpers.d.ts.map +1 -0
  37. package/dist/helpers.js +89 -0
  38. package/dist/helpers.js.map +1 -0
  39. package/dist/index.d.ts +28 -21
  40. package/dist/index.d.ts.map +1 -1
  41. package/dist/index.js +30 -13
  42. package/dist/index.js.map +1 -1
  43. package/dist/links.d.ts +39 -0
  44. package/dist/links.d.ts.map +1 -0
  45. package/dist/links.js +57 -0
  46. package/dist/links.js.map +1 -0
  47. package/dist/module.d.ts +23 -22
  48. package/dist/module.d.ts.map +1 -1
  49. package/dist/module.js +129 -158
  50. package/dist/module.js.map +1 -1
  51. package/dist/official.d.ts +39 -0
  52. package/dist/official.d.ts.map +1 -0
  53. package/dist/official.js +44 -0
  54. package/dist/official.js.map +1 -0
  55. package/dist/permission-cache.d.ts +18 -9
  56. package/dist/permission-cache.d.ts.map +1 -1
  57. package/dist/permission-cache.js +87 -24
  58. package/dist/permission-cache.js.map +1 -1
  59. package/dist/permission-matcher.d.ts +13 -0
  60. package/dist/permission-matcher.d.ts.map +1 -1
  61. package/dist/permission-matcher.js +38 -2
  62. package/dist/permission-matcher.js.map +1 -1
  63. package/dist/permission-provider.d.ts +11 -0
  64. package/dist/permission-provider.d.ts.map +1 -1
  65. package/dist/policies/authz-policy.d.ts +42 -0
  66. package/dist/policies/authz-policy.d.ts.map +1 -0
  67. package/dist/policies/authz-policy.js +80 -0
  68. package/dist/policies/authz-policy.js.map +1 -0
  69. package/dist/policies/gate.policy.d.ts +18 -0
  70. package/dist/policies/gate.policy.d.ts.map +1 -0
  71. package/dist/policies/gate.policy.js +37 -0
  72. package/dist/policies/gate.policy.js.map +1 -0
  73. package/dist/policies/index.d.ts +5 -0
  74. package/dist/policies/index.d.ts.map +1 -0
  75. package/dist/policies/index.js +5 -0
  76. package/dist/policies/index.js.map +1 -0
  77. package/dist/policies/permission.policy.d.ts +27 -0
  78. package/dist/policies/permission.policy.d.ts.map +1 -0
  79. package/dist/policies/permission.policy.js +61 -0
  80. package/dist/policies/permission.policy.js.map +1 -0
  81. package/dist/policies/role.policy.d.ts +27 -0
  82. package/dist/policies/role.policy.d.ts.map +1 -0
  83. package/dist/policies/role.policy.js +51 -0
  84. package/dist/policies/role.policy.js.map +1 -0
  85. package/dist/policy-index.d.ts +51 -0
  86. package/dist/policy-index.d.ts.map +1 -0
  87. package/dist/policy-index.js +135 -0
  88. package/dist/policy-index.js.map +1 -0
  89. package/dist/rbac.d.ts +92 -0
  90. package/dist/rbac.d.ts.map +1 -0
  91. package/dist/rbac.js +293 -0
  92. package/dist/rbac.js.map +1 -0
  93. package/dist/resource-resolver.d.ts +15 -17
  94. package/dist/resource-resolver.d.ts.map +1 -1
  95. package/dist/resource-resolver.js +8 -8
  96. package/dist/resource-resolver.js.map +1 -1
  97. package/dist/runtime.d.ts +31 -0
  98. package/dist/runtime.d.ts.map +1 -0
  99. package/dist/runtime.js +40 -0
  100. package/dist/runtime.js.map +1 -0
  101. package/dist/tokens.d.ts +4 -8
  102. package/dist/tokens.d.ts.map +1 -1
  103. package/dist/tokens.js +4 -8
  104. package/dist/tokens.js.map +1 -1
  105. package/dist/types.d.ts +52 -169
  106. package/dist/types.d.ts.map +1 -1
  107. package/package.json +19 -10
  108. package/skills/authz-enforcement/SKILL.md +67 -131
  109. package/skills/authz-policies/SKILL.md +43 -117
  110. package/skills/authz-query-scopes/SKILL.md +16 -9
  111. package/skills/authz-rbac-seams/SKILL.md +39 -12
  112. package/skills/authz-setup/SKILL.md +45 -106
  113. package/dist/decorator/can.decorator.d.ts +0 -29
  114. package/dist/decorator/can.decorator.d.ts.map +0 -1
  115. package/dist/decorator/can.decorator.js +0 -23
  116. package/dist/decorator/can.decorator.js.map +0 -1
  117. package/dist/decorator/policy.decorator.d.ts +0 -18
  118. package/dist/decorator/policy.decorator.d.ts.map +0 -1
  119. package/dist/decorator/policy.decorator.js +0 -28
  120. package/dist/decorator/policy.decorator.js.map +0 -1
  121. package/dist/guard/can.guard.d.ts +0 -31
  122. package/dist/guard/can.guard.d.ts.map +0 -1
  123. package/dist/guard/can.guard.js +0 -92
  124. package/dist/guard/can.guard.js.map +0 -1
  125. package/dist/guard/roles.guard.d.ts +0 -21
  126. package/dist/guard/roles.guard.d.ts.map +0 -1
  127. package/dist/guard/roles.guard.js +0 -50
  128. package/dist/guard/roles.guard.js.map +0 -1
  129. package/dist/policy-registry.d.ts +0 -46
  130. package/dist/policy-registry.d.ts.map +0 -1
  131. package/dist/policy-registry.js +0 -101
  132. package/dist/policy-registry.js.map +0 -1
@@ -1,101 +0,0 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- import { Injectable } from '@nestjs/common';
8
- import { getPolicyResource } from './decorator/policy.decorator.js';
9
- import { PolicyNotDecoratedException } from './errors/exceptions.js';
10
- /**
11
- * Maps a resource class → its policy instance. Populated at module init from
12
- * explicit `policies: []` and/or auto-discovered `@Policy` providers.
13
- */
14
- let PolicyRegistry = class PolicyRegistry {
15
- byResource = new Map();
16
- // Cache for classAbilities(): the result is static after registration, so it
17
- // is computed lazily once and invalidated whenever a policy is registered.
18
- classAbilitiesCache;
19
- /**
20
- * Register a policy instance. Throws if the instance's class was not decorated
21
- * with `@Policy(Resource)`.
22
- */
23
- register(policy) {
24
- const resource = getPolicyResource(policy);
25
- if (!resource) {
26
- const name = policy.constructor?.name ?? 'Policy';
27
- throw new PolicyNotDecoratedException(name);
28
- }
29
- this.byResource.set(resource, policy);
30
- this.classAbilitiesCache = undefined;
31
- }
32
- /** Resolve the policy instance for a resource class, or `undefined`. */
33
- forResource(resource) {
34
- return this.byResource.get(resource);
35
- }
36
- /** Resolve the policy for the class of a resource instance, or `undefined`. */
37
- forInstance(instance) {
38
- return this.byResource.get(instance.constructor);
39
- }
40
- /** True when a policy is registered for the resource class. */
41
- has(resource) {
42
- return this.byResource.has(resource);
43
- }
44
- /** All registered policies (for introspection/testing). */
45
- all() {
46
- return [...this.byResource.values()];
47
- }
48
- /** All registered resource classes (insertion order). */
49
- resources() {
50
- return [...this.byResource.keys()];
51
- }
52
- /**
53
- * Enumerate the CLASS-LEVEL ability method names declared on each registered
54
- * policy, keyed by resource class. Used by integrations that pre-resolve a
55
- * user's class-level abilities (e.g. to share them as Inertia props).
56
- *
57
- * Walks the policy prototype chain and collects own function-valued members,
58
- * excluding `constructor` and the reserved `before` hook. Inherited Object
59
- * members are skipped.
60
- *
61
- * Only methods that take NO resource instance are included — heuristically,
62
- * arity `<= 1` (just `user`, e.g. `create(user)` / `viewAny(user)`). An
63
- * instance method like `update(user, post)` is excluded: dispatching it
64
- * against the resource CLASS would call it with the class constructor as
65
- * `post` and write a bogus class-level verdict.
66
- */
67
- classAbilities() {
68
- if (this.classAbilitiesCache === undefined) {
69
- this.classAbilitiesCache = this.computeClassAbilities();
70
- }
71
- return this.classAbilitiesCache;
72
- }
73
- computeClassAbilities() {
74
- const out = [];
75
- for (const [resource, policy] of this.byResource) {
76
- const abilities = new Set();
77
- let proto = Object.getPrototypeOf(policy);
78
- while (proto && proto !== Object.prototype) {
79
- for (const name of Object.getOwnPropertyNames(proto)) {
80
- if (name === 'constructor' || name === 'before')
81
- continue;
82
- const member = policy[name];
83
- // Class-level abilities take only `user` (arity <= 1). A method that
84
- // also declares a resource param (arity >= 2) is instance-scoped and
85
- // must not be dispatched against the class.
86
- if (typeof member === 'function' && member.length <= 1) {
87
- abilities.add(name);
88
- }
89
- }
90
- proto = Object.getPrototypeOf(proto);
91
- }
92
- out.push({ resource, abilities: [...abilities] });
93
- }
94
- return out;
95
- }
96
- };
97
- PolicyRegistry = __decorate([
98
- Injectable()
99
- ], PolicyRegistry);
100
- export { PolicyRegistry };
101
- //# sourceMappingURL=policy-registry.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"policy-registry.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