@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.
- package/CHANGELOG.md +115 -0
- package/README.md +279 -0
- package/dist/authz.guard.d.ts +48 -0
- package/dist/authz.guard.d.ts.map +1 -0
- package/dist/authz.guard.js +144 -0
- package/dist/authz.guard.js.map +1 -0
- package/dist/can-endpoint.controller.d.ts +0 -19
- package/dist/can-endpoint.controller.d.ts.map +1 -1
- package/dist/can-endpoint.controller.js +18 -9
- package/dist/can-endpoint.controller.js.map +1 -1
- package/dist/decision-provider.d.ts +25 -16
- package/dist/decision-provider.d.ts.map +1 -1
- package/dist/decision-provider.js +11 -1
- package/dist/decision-provider.js.map +1 -1
- package/dist/decisions.d.ts +24 -0
- package/dist/decisions.d.ts.map +1 -0
- package/dist/decisions.js +87 -0
- package/dist/decisions.js.map +1 -0
- package/dist/decorator/roles.decorator.d.ts +6 -12
- package/dist/decorator/roles.decorator.d.ts.map +1 -1
- package/dist/decorator/roles.decorator.js +13 -15
- package/dist/decorator/roles.decorator.js.map +1 -1
- package/dist/diagnostics.d.ts +15 -5
- package/dist/diagnostics.d.ts.map +1 -1
- package/dist/diagnostics.js +3 -1
- package/dist/diagnostics.js.map +1 -1
- package/dist/errors/exceptions.d.ts +0 -8
- package/dist/errors/exceptions.d.ts.map +1 -1
- package/dist/errors/exceptions.js +0 -14
- package/dist/errors/exceptions.js.map +1 -1
- package/dist/gate.d.ts +83 -209
- package/dist/gate.d.ts.map +1 -1
- package/dist/gate.js +209 -577
- package/dist/gate.js.map +1 -1
- package/dist/helpers.d.ts +46 -0
- package/dist/helpers.d.ts.map +1 -0
- package/dist/helpers.js +89 -0
- package/dist/helpers.js.map +1 -0
- package/dist/index.d.ts +28 -21
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +30 -13
- package/dist/index.js.map +1 -1
- package/dist/links.d.ts +39 -0
- package/dist/links.d.ts.map +1 -0
- package/dist/links.js +57 -0
- package/dist/links.js.map +1 -0
- package/dist/module.d.ts +23 -22
- package/dist/module.d.ts.map +1 -1
- package/dist/module.js +129 -158
- package/dist/module.js.map +1 -1
- package/dist/official.d.ts +39 -0
- package/dist/official.d.ts.map +1 -0
- package/dist/official.js +44 -0
- package/dist/official.js.map +1 -0
- package/dist/permission-cache.d.ts +18 -9
- package/dist/permission-cache.d.ts.map +1 -1
- package/dist/permission-cache.js +87 -24
- package/dist/permission-cache.js.map +1 -1
- package/dist/permission-matcher.d.ts +13 -0
- package/dist/permission-matcher.d.ts.map +1 -1
- package/dist/permission-matcher.js +38 -2
- package/dist/permission-matcher.js.map +1 -1
- package/dist/permission-provider.d.ts +11 -0
- package/dist/permission-provider.d.ts.map +1 -1
- package/dist/policies/authz-policy.d.ts +42 -0
- package/dist/policies/authz-policy.d.ts.map +1 -0
- package/dist/policies/authz-policy.js +80 -0
- package/dist/policies/authz-policy.js.map +1 -0
- package/dist/policies/gate.policy.d.ts +18 -0
- package/dist/policies/gate.policy.d.ts.map +1 -0
- package/dist/policies/gate.policy.js +37 -0
- package/dist/policies/gate.policy.js.map +1 -0
- package/dist/policies/index.d.ts +5 -0
- package/dist/policies/index.d.ts.map +1 -0
- package/dist/policies/index.js +5 -0
- package/dist/policies/index.js.map +1 -0
- package/dist/policies/permission.policy.d.ts +27 -0
- package/dist/policies/permission.policy.d.ts.map +1 -0
- package/dist/policies/permission.policy.js +61 -0
- package/dist/policies/permission.policy.js.map +1 -0
- package/dist/policies/role.policy.d.ts +27 -0
- package/dist/policies/role.policy.d.ts.map +1 -0
- package/dist/policies/role.policy.js +51 -0
- package/dist/policies/role.policy.js.map +1 -0
- package/dist/policy-index.d.ts +51 -0
- package/dist/policy-index.d.ts.map +1 -0
- package/dist/policy-index.js +135 -0
- package/dist/policy-index.js.map +1 -0
- package/dist/rbac.d.ts +92 -0
- package/dist/rbac.d.ts.map +1 -0
- package/dist/rbac.js +293 -0
- package/dist/rbac.js.map +1 -0
- package/dist/resource-resolver.d.ts +15 -17
- package/dist/resource-resolver.d.ts.map +1 -1
- package/dist/resource-resolver.js +8 -8
- package/dist/resource-resolver.js.map +1 -1
- package/dist/runtime.d.ts +31 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +40 -0
- package/dist/runtime.js.map +1 -0
- package/dist/tokens.d.ts +4 -8
- package/dist/tokens.d.ts.map +1 -1
- package/dist/tokens.js +4 -8
- package/dist/tokens.js.map +1 -1
- package/dist/types.d.ts +52 -169
- package/dist/types.d.ts.map +1 -1
- package/package.json +19 -10
- package/skills/authz-enforcement/SKILL.md +67 -131
- package/skills/authz-policies/SKILL.md +43 -117
- package/skills/authz-query-scopes/SKILL.md +16 -9
- package/skills/authz-rbac-seams/SKILL.md +39 -12
- package/skills/authz-setup/SKILL.md +45 -106
- package/dist/decorator/can.decorator.d.ts +0 -29
- package/dist/decorator/can.decorator.d.ts.map +0 -1
- package/dist/decorator/can.decorator.js +0 -23
- package/dist/decorator/can.decorator.js.map +0 -1
- package/dist/decorator/policy.decorator.d.ts +0 -18
- package/dist/decorator/policy.decorator.d.ts.map +0 -1
- package/dist/decorator/policy.decorator.js +0 -28
- package/dist/decorator/policy.decorator.js.map +0 -1
- package/dist/guard/can.guard.d.ts +0 -31
- package/dist/guard/can.guard.d.ts.map +0 -1
- package/dist/guard/can.guard.js +0 -92
- package/dist/guard/can.guard.js.map +0 -1
- package/dist/guard/roles.guard.d.ts +0 -21
- package/dist/guard/roles.guard.d.ts.map +0 -1
- package/dist/guard/roles.guard.js +0 -50
- package/dist/guard/roles.guard.js.map +0 -1
- package/dist/policy-registry.d.ts +0 -46
- package/dist/policy-registry.d.ts.map +0 -1
- package/dist/policy-registry.js +0 -101
- package/dist/policy-registry.js.map +0 -1
|
@@ -1,153 +1,92 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: authz-setup
|
|
3
3
|
description: >
|
|
4
|
-
Set up @dudousxd/nestjs-authz
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
Set up @dudousxd/nestjs-authz 0.9, the extras on top of NestJS's first-party
|
|
5
|
+
@nestjs/authorization. Import AuthorizationModule.forRoot() (official: global
|
|
6
|
+
deny-by-default AuthorizationGuard, @Can, AuthorizationService, getUser) next to
|
|
7
|
+
AuthzModule.forRoot / forRootAsync (ours: Gate, RolePolicy / PermissionPolicy /
|
|
8
|
+
GatePolicy, the ResourceResolver byId() uses, superAdmin, the allow+deny decision event,
|
|
9
|
+
the opt-in POST /authz/can endpoint). Covers the optional peers: @nestjs/authentication
|
|
10
|
+
(AuthenticationContext.user is the ambient Gate user) and @dudousxd/nestjs-context
|
|
11
|
+
(fallback ambient user via resolveUser, per-request permission cache, tenant).
|
|
11
12
|
metadata:
|
|
12
13
|
type: core
|
|
13
14
|
library: "@dudousxd/nestjs-authz"
|
|
14
|
-
library_version: "0.
|
|
15
|
+
library_version: "0.9.0"
|
|
15
16
|
framework: nestjs
|
|
16
17
|
---
|
|
17
18
|
|
|
18
19
|
# Setting up @dudousxd/nestjs-authz
|
|
19
20
|
|
|
20
|
-
`@
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
Authorization decisions belong to [`@nestjs/authorization`](https://docs.nestjs.com/security/authorization).
|
|
22
|
+
This package adds RBAC, `@Can` helpers, a service-layer `Gate`, super-admin, query scopes and
|
|
23
|
+
frontend glue. It never authenticates.
|
|
23
24
|
|
|
24
25
|
## Setup
|
|
25
26
|
|
|
26
27
|
```sh
|
|
27
|
-
pnpm add @dudousxd/nestjs-authz
|
|
28
|
+
pnpm add @nestjs/authorization @dudousxd/nestjs-authz
|
|
28
29
|
```
|
|
29
30
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
current user for free; when absent you pass the user explicitly via `gate.forUser(...)`.
|
|
33
|
-
|
|
34
|
-
Register the module once (it is `global: true`, so every module sees the `Gate` and the
|
|
35
|
-
guards):
|
|
31
|
+
NestJS 12, Node 22+. Peers: `@nestjs/common`, `@nestjs/core`, `@nestjs/authorization`,
|
|
32
|
+
`reflect-metadata`, `rxjs`; optional `@nestjs/authentication` and `@dudousxd/nestjs-context`.
|
|
36
33
|
|
|
37
34
|
```ts
|
|
38
|
-
import {
|
|
35
|
+
import { AuthorizationModule } from '@nestjs/authorization';
|
|
39
36
|
import { AuthzModule } from '@dudousxd/nestjs-authz';
|
|
40
|
-
import { PostPolicy } from './post.policy';
|
|
41
37
|
|
|
42
38
|
@Module({
|
|
43
39
|
imports: [
|
|
40
|
+
AuthorizationModule.forRoot({ getUser: (ctx) => ctx.switchToHttp().getRequest().user }),
|
|
44
41
|
AuthzModule.forRoot({
|
|
45
|
-
|
|
46
|
-
|
|
42
|
+
superAdmin: (user) => user?.isRoot === true,
|
|
43
|
+
resourceResolver: new OrmResourceResolver(), // for byId(); default builds { id } from :id
|
|
47
44
|
}),
|
|
48
45
|
],
|
|
46
|
+
providers: [PostPolicy], // official policies are discovered wherever they are providers
|
|
49
47
|
})
|
|
50
48
|
export class AppModule {}
|
|
51
49
|
```
|
|
52
50
|
|
|
53
|
-
`
|
|
54
|
-
`
|
|
55
|
-
|
|
56
|
-
init from `policies: []` **and** auto-discovered `@Policy` providers.
|
|
51
|
+
`forRootAsync({ imports, inject, useFactory | useClass | useExisting, canEndpoint, decorators, unannotated })` takes the
|
|
52
|
+
same options. `canEndpoint`, `decorators` and `unannotated` are static (read from the top
|
|
53
|
+
level next to `useFactory`, never from the factory's return — `decorators` and `unannotated` in the factory's return throw).
|
|
57
54
|
|
|
58
55
|
## Core patterns
|
|
59
56
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
imports: [ConfigModule],
|
|
68
|
-
inject: [ConfigService],
|
|
69
|
-
useFactory: (config: ConfigService) => ({
|
|
70
|
-
policies: [PostPolicy],
|
|
71
|
-
superAdmin: (user) => (user as { role?: string }).role === config.get('SUPER_ROLE'),
|
|
72
|
-
}),
|
|
73
|
-
});
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
`canEndpoint` and `resourceLoaders` may be declared **statically** on the async options
|
|
77
|
-
too (controllers are registered at module-definition time, before the factory resolves).
|
|
78
|
-
|
|
79
|
-
### Wiring the current user via nestjs-context
|
|
80
|
-
|
|
81
|
-
The Gate resolves the current user structurally through the well-known capability token
|
|
82
|
-
`CONTEXT_ACCESSOR` (`capability('context','accessor')` ===
|
|
83
|
-
`Symbol.for('@dudousxd/nestjs-context:accessor')`). Install and configure
|
|
84
|
-
`@dudousxd/nestjs-context` so requests carry a user; the Gate then needs no wiring. With
|
|
85
|
-
**no** accessor and no explicit user, checks treat the request as anonymous and **deny**.
|
|
86
|
-
|
|
87
|
-
### resolveUser — hydrate the real entity (the critical hook)
|
|
88
|
-
|
|
89
|
-
On the **context path**, the value handed to `superAdmin`, `after`, `resolveRoles`, and
|
|
90
|
-
your policy methods is the raw **`UserRef`** (`{ type, id }`) from nestjs-context — **not**
|
|
91
|
-
your hydrated user row. Provide `resolveUser` to load the entity before those run:
|
|
92
|
-
|
|
93
|
-
```ts
|
|
94
|
-
AuthzModule.forRoot({
|
|
95
|
-
resolveUser: (ref) => userRepo.findOneBy({ id: Number(ref.id) }), // ref: { type, id }
|
|
96
|
-
superAdmin: (user) => (user as User).isAdmin, // now sees a real column
|
|
97
|
-
});
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
`resolveUser` returning nullish is treated as "no user" (deny). It is **only** applied on
|
|
101
|
-
the context path — `gate.forUser(entity)` always uses the value you pass verbatim.
|
|
57
|
+
- The official module must stay global (its default): the `Gate` injects `AuthorizationService`.
|
|
58
|
+
- `getUser` is the only user on routes. The `Gate`'s ambient user (calls without `forUser`) is
|
|
59
|
+
`@nestjs/authentication`'s `AuthenticationContext.user` when that package is installed and its
|
|
60
|
+
scope is active (its answer is final, so an anonymous call there is a guest), else nestjs-context's
|
|
61
|
+
`{ type, id }` ref, hydrated by `resolveUser` once per request.
|
|
62
|
+
- Every route needs a declaration: the official guard denies a handler with no `@Can()`,
|
|
63
|
+
`@Can.Anyone()` or `@Public()`.
|
|
102
64
|
|
|
103
65
|
## Common mistakes
|
|
104
66
|
|
|
105
|
-
### 1.
|
|
67
|
+
### 1. Importing only AuthzModule
|
|
106
68
|
|
|
107
69
|
```ts
|
|
108
|
-
// Wrong —
|
|
109
|
-
|
|
110
|
-
AuthzModule.forRoot({ superAdmin: (u) => (u as User).isAdmin });
|
|
111
|
-
|
|
112
|
-
// Correct — hydrate the entity first.
|
|
113
|
-
AuthzModule.forRoot({
|
|
114
|
-
resolveUser: (ref) => userRepo.findOneBy({ id: Number(ref.id) }),
|
|
115
|
-
superAdmin: (u) => (u as User).isAdmin,
|
|
116
|
-
});
|
|
117
|
-
```
|
|
70
|
+
// Wrong — no AuthorizationService for the Gate, no guard on routes.
|
|
71
|
+
imports: [AuthzModule.forRoot()]
|
|
118
72
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
`packages/core/src/gate.ts` (`currentUser`).
|
|
73
|
+
// Correct
|
|
74
|
+
imports: [AuthorizationModule.forRoot(), AuthzModule.forRoot()]
|
|
75
|
+
```
|
|
123
76
|
|
|
124
|
-
### 2.
|
|
77
|
+
### 2. Passing policies to AuthzModule
|
|
125
78
|
|
|
126
79
|
```ts
|
|
127
|
-
// Wrong —
|
|
128
|
-
|
|
129
|
-
export class PostsModule {}
|
|
80
|
+
// Wrong (0.x) — `policies` is gone from AuthzModuleOptions.
|
|
81
|
+
AuthzModule.forRoot({ policies: [PostPolicy] })
|
|
130
82
|
|
|
131
|
-
// Correct — register
|
|
132
|
-
|
|
133
|
-
export class AppModule {}
|
|
83
|
+
// Correct — register them as providers, or with the official module.
|
|
84
|
+
AuthorizationModule.forRoot({ policies: [PostPolicy] })
|
|
134
85
|
```
|
|
135
86
|
|
|
136
|
-
|
|
137
|
-
registration exports `Gate`, the guards, and `RESOURCE_RESOLVER` to the whole app.
|
|
138
|
-
Source: `packages/core/src/module.ts` (`forRoot` returns `global: true`).
|
|
139
|
-
|
|
140
|
-
### 3. Reaching for the `canEndpoint` before it is enabled
|
|
87
|
+
### 3. Leaving routes undeclared after upgrading from 0.x
|
|
141
88
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
AuthzModule.forRoot({ policies: [PostPolicy] });
|
|
145
|
-
|
|
146
|
-
// Correct — opt in (true = default path 'authz/can', or pass a custom string).
|
|
147
|
-
AuthzModule.forRoot({ policies: [PostPolicy], canEndpoint: true });
|
|
148
|
-
```
|
|
89
|
+
0.x let un-annotated routes through. The official guard denies them (401 for guests, 403
|
|
90
|
+
otherwise). Add `@Can.Anyone()` to routes anyone may call.
|
|
149
91
|
|
|
150
|
-
|
|
151
|
-
truthy; it is the last-resort path for abilities not already hydrated on the client.
|
|
152
|
-
Source: `packages/core/src/module.ts` (`canControllers`), `packages/core/src/types.ts`
|
|
153
|
-
(`canEndpoint`).
|
|
92
|
+
Source: `packages/core/src/module.ts`, `packages/core/src/runtime.ts`.
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import 'reflect-metadata';
|
|
2
|
-
import { type Type } from '@nestjs/common';
|
|
3
|
-
export interface CanOptions {
|
|
4
|
-
/**
|
|
5
|
-
* Class-level ability (e.g. `create`): the policy method is keyed to the
|
|
6
|
-
* resource class but receives NO instance, so the guard skips resource loading.
|
|
7
|
-
*/
|
|
8
|
-
classLevel?: boolean;
|
|
9
|
-
}
|
|
10
|
-
export interface CanMetadata {
|
|
11
|
-
ability: string;
|
|
12
|
-
/** Resource class — to load an instance, or (for class-level) to locate the policy. */
|
|
13
|
-
resource?: Type<unknown>;
|
|
14
|
-
/** When true, skip resource loading and dispatch against the resource class. */
|
|
15
|
-
classLevel?: boolean;
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Guard a route with an authorization check.
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* `@Can('update', Post)` // resolve Post by :id, run PostPolicy.update(user, post)
|
|
22
|
-
* `@Can('create', Post, { classLevel: true })` // run PostPolicy.create(user) — no instance loaded
|
|
23
|
-
* `@Can('access-admin')` // ad-hoc gate, no resource
|
|
24
|
-
*
|
|
25
|
-
* When a resource class is provided (and not `classLevel`), the guard loads an
|
|
26
|
-
* instance via the registered `ResourceResolver` (default: by route `:id`).
|
|
27
|
-
*/
|
|
28
|
-
export declare function Can(ability: string, resource?: Type<unknown>, options?: CanOptions): MethodDecorator & ClassDecorator;
|
|
29
|
-
//# sourceMappingURL=can.decorator.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"can.decorator.d.ts","sourceRoot":"","sources":["../../src/decorator/can.decorator.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAC1B,OAAO,EAAe,KAAK,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAGxD,MAAM,WAAW,UAAU;IACzB;;;OAGG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAED,MAAM,WAAW,WAAW;IAC1B,OAAO,EAAE,MAAM,CAAC;IAChB,uFAAuF;IACvF,QAAQ,CAAC,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;IACzB,gFAAgF;IAChF,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,GAAG,CACjB,OAAO,EAAE,MAAM,EACf,QAAQ,CAAC,EAAE,IAAI,CAAC,OAAO,CAAC,EACxB,OAAO,CAAC,EAAE,UAAU,GACnB,eAAe,GAAG,cAAc,CAKlC"}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import 'reflect-metadata';
|
|
2
|
-
import { SetMetadata } from '@nestjs/common';
|
|
3
|
-
import { CAN_METADATA } from '../tokens.js';
|
|
4
|
-
/**
|
|
5
|
-
* Guard a route with an authorization check.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* `@Can('update', Post)` // resolve Post by :id, run PostPolicy.update(user, post)
|
|
9
|
-
* `@Can('create', Post, { classLevel: true })` // run PostPolicy.create(user) — no instance loaded
|
|
10
|
-
* `@Can('access-admin')` // ad-hoc gate, no resource
|
|
11
|
-
*
|
|
12
|
-
* When a resource class is provided (and not `classLevel`), the guard loads an
|
|
13
|
-
* instance via the registered `ResourceResolver` (default: by route `:id`).
|
|
14
|
-
*/
|
|
15
|
-
export function Can(ability, resource, options) {
|
|
16
|
-
const meta = { ability };
|
|
17
|
-
if (resource !== undefined)
|
|
18
|
-
meta.resource = resource;
|
|
19
|
-
if (options?.classLevel)
|
|
20
|
-
meta.classLevel = true;
|
|
21
|
-
return SetMetadata(CAN_METADATA, meta);
|
|
22
|
-
}
|
|
23
|
-
//# sourceMappingURL=can.decorator.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"can.decorator.js","sourceRoot":"","sources":["../../src/decorator/can.decorator.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAC1B,OAAO,EAAE,WAAW,EAAa,MAAM,gBAAgB,CAAC;AACxD,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAkB5C;;;;;;;;;;GAUG;AACH,MAAM,UAAU,GAAG,CACjB,OAAe,EACf,QAAwB,EACxB,OAAoB;IAEpB,MAAM,IAAI,GAAgB,EAAE,OAAO,EAAE,CAAC;IACtC,IAAI,QAAQ,KAAK,SAAS;QAAE,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IACrD,IAAI,OAAO,EAAE,UAAU;QAAE,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC;IAChD,OAAO,WAAW,CAAC,YAAY,EAAE,IAAI,CAAC,CAAC;AACzC,CAAC"}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import 'reflect-metadata';
|
|
2
|
-
import { type Type } from '@nestjs/common';
|
|
3
|
-
/**
|
|
4
|
-
* Marks a class as the policy for `resource`. The class methods become abilities
|
|
5
|
-
* dispatched by name (e.g. `update(user, post)` answers `gate.authorize('update', post)`).
|
|
6
|
-
*
|
|
7
|
-
* Applying `@Policy` also makes the class an `@Injectable()` provider so it can
|
|
8
|
-
* be auto-discovered and resolved from the Nest container.
|
|
9
|
-
*/
|
|
10
|
-
export declare function Policy(resource: Type<unknown>): ClassDecorator;
|
|
11
|
-
/**
|
|
12
|
-
* Returns the resource class a policy was declared for, or `undefined`.
|
|
13
|
-
*
|
|
14
|
-
* Uses `getMetadata` (not `getOwnMetadata`) so a subclass of a `@Policy`-decorated
|
|
15
|
-
* class still resolves the inherited resource via the prototype chain.
|
|
16
|
-
*/
|
|
17
|
-
export declare function getPolicyResource(policy: Type<unknown> | object): Type<unknown> | undefined;
|
|
18
|
-
//# sourceMappingURL=policy.decorator.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"policy.decorator.d.ts","sourceRoot":"","sources":["../../src/decorator/policy.decorator.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAC1B,OAAO,EAAc,KAAK,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAIvD;;;;;;GAMG;AACH,wBAAgB,MAAM,CAAC,QAAQ,EAAE,IAAI,CAAC,OAAO,CAAC,GAAG,cAAc,CAM9D;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,GAAG,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,SAAS,CAG3F"}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import 'reflect-metadata';
|
|
2
|
-
import { Injectable } from '@nestjs/common';
|
|
3
|
-
import { POLICY_RESOURCE_METADATA } from '../tokens.js';
|
|
4
|
-
/**
|
|
5
|
-
* Marks a class as the policy for `resource`. The class methods become abilities
|
|
6
|
-
* dispatched by name (e.g. `update(user, post)` answers `gate.authorize('update', post)`).
|
|
7
|
-
*
|
|
8
|
-
* Applying `@Policy` also makes the class an `@Injectable()` provider so it can
|
|
9
|
-
* be auto-discovered and resolved from the Nest container.
|
|
10
|
-
*/
|
|
11
|
-
export function Policy(resource) {
|
|
12
|
-
return (target) => {
|
|
13
|
-
Reflect.defineMetadata(POLICY_RESOURCE_METADATA, resource, target);
|
|
14
|
-
// Make the policy a provider without forcing the app to add @Injectable().
|
|
15
|
-
Injectable()(target);
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Returns the resource class a policy was declared for, or `undefined`.
|
|
20
|
-
*
|
|
21
|
-
* Uses `getMetadata` (not `getOwnMetadata`) so a subclass of a `@Policy`-decorated
|
|
22
|
-
* class still resolves the inherited resource via the prototype chain.
|
|
23
|
-
*/
|
|
24
|
-
export function getPolicyResource(policy) {
|
|
25
|
-
const ctor = typeof policy === 'function' ? policy : policy.constructor;
|
|
26
|
-
return Reflect.getMetadata(POLICY_RESOURCE_METADATA, ctor);
|
|
27
|
-
}
|
|
28
|
-
//# sourceMappingURL=policy.decorator.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"policy.decorator.js","sourceRoot":"","sources":["../../src/decorator/policy.decorator.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAC1B,OAAO,EAAE,UAAU,EAAa,MAAM,gBAAgB,CAAC;AACvD,OAAO,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAC;AAGxD;;;;;;GAMG;AACH,MAAM,UAAU,MAAM,CAAC,QAAuB;IAC5C,OAAO,CAAC,MAAM,EAAE,EAAE;QAChB,OAAO,CAAC,cAAc,CAAC,wBAAwB,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;QACnE,2EAA2E;QAC3E,UAAU,EAAE,CAAC,MAAM,CAAC,CAAC;IACvB,CAAC,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,MAA8B;IAC9D,MAAM,IAAI,GAAG,OAAO,MAAM,KAAK,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAE,MAAyB,CAAC,WAAW,CAAC;IAC5F,OAAO,OAAO,CAAC,WAAW,CAAC,wBAAwB,EAAE,IAAI,CAA8B,CAAC;AAC1F,CAAC"}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { type CanActivate, type ExecutionContext } from '@nestjs/common';
|
|
2
|
-
import { ModuleRef, Reflector } from '@nestjs/core';
|
|
3
|
-
import { Gate } from '../gate.js';
|
|
4
|
-
import type { ResourceResolver } from '../resource-resolver.js';
|
|
5
|
-
/**
|
|
6
|
-
* Enforces `@Can(ability, Resource?)` on routes.
|
|
7
|
-
*
|
|
8
|
-
* - No `@Can` metadata → allow (the guard is inert on un-annotated routes).
|
|
9
|
-
* - Resource class + not class-level → load instance via {@link ResourceResolver}
|
|
10
|
-
* (a missing/undefined instance is a denial).
|
|
11
|
-
* - Class-level or no resource → dispatch against the class / ad-hoc gate.
|
|
12
|
-
*
|
|
13
|
-
* The decision is delegated to {@link Gate.authorize}, which throws
|
|
14
|
-
* `ForbiddenException` on denial.
|
|
15
|
-
*/
|
|
16
|
-
export declare class CanGuard implements CanActivate {
|
|
17
|
-
private readonly reflector;
|
|
18
|
-
private readonly gate;
|
|
19
|
-
private readonly moduleRef;
|
|
20
|
-
private readonly resolver?;
|
|
21
|
-
constructor(reflector: Reflector, gate: Gate, moduleRef: ModuleRef, resolver?: ResourceResolver | undefined);
|
|
22
|
-
/**
|
|
23
|
-
* Locate the resource resolver. Prefers the value injected into this module;
|
|
24
|
-
* falls back to a non-strict {@link ModuleRef} lookup so a resolver provided by
|
|
25
|
-
* ANY module (e.g. the app root) is found even though the guard lives in the
|
|
26
|
-
* global AuthzModule.
|
|
27
|
-
*/
|
|
28
|
-
private resolveResolver;
|
|
29
|
-
canActivate(ctx: ExecutionContext): Promise<boolean>;
|
|
30
|
-
}
|
|
31
|
-
//# sourceMappingURL=can.guard.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"can.guard.d.ts","sourceRoot":"","sources":["../../src/guard/can.guard.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,WAAW,EAChB,KAAK,gBAAgB,EAKtB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAGpD,OAAO,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAClC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAIhE;;;;;;;;;;GAUG;AACH,qBACa,QAAS,YAAW,WAAW;IAExC,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,IAAI;IACrB,OAAO,CAAC,QAAQ,CAAC,SAAS;IAG1B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC;gBALT,SAAS,EAAE,SAAS,EACpB,IAAI,EAAE,IAAI,EACV,SAAS,EAAE,SAAS,EAGpB,QAAQ,CAAC,EAAE,gBAAgB,YAAA;IAG9C;;;;;OAKG;IACH,OAAO,CAAC,eAAe;IASjB,WAAW,CAAC,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC;CA2B3D"}
|
package/dist/guard/can.guard.js
DELETED
|
@@ -1,92 +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
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
-
};
|
|
10
|
-
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
11
|
-
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
|
-
};
|
|
13
|
-
import { ForbiddenException, Inject, Injectable, Optional, } from '@nestjs/common';
|
|
14
|
-
import { ModuleRef, Reflector } from '@nestjs/core';
|
|
15
|
-
import { ResourceResolverMissingException } from '../errors/exceptions.js';
|
|
16
|
-
import { Gate } from '../gate.js';
|
|
17
|
-
import { CAN_METADATA, RESOURCE_RESOLVER } from '../tokens.js';
|
|
18
|
-
/**
|
|
19
|
-
* Enforces `@Can(ability, Resource?)` on routes.
|
|
20
|
-
*
|
|
21
|
-
* - No `@Can` metadata → allow (the guard is inert on un-annotated routes).
|
|
22
|
-
* - Resource class + not class-level → load instance via {@link ResourceResolver}
|
|
23
|
-
* (a missing/undefined instance is a denial).
|
|
24
|
-
* - Class-level or no resource → dispatch against the class / ad-hoc gate.
|
|
25
|
-
*
|
|
26
|
-
* The decision is delegated to {@link Gate.authorize}, which throws
|
|
27
|
-
* `ForbiddenException` on denial.
|
|
28
|
-
*/
|
|
29
|
-
let CanGuard = class CanGuard {
|
|
30
|
-
reflector;
|
|
31
|
-
gate;
|
|
32
|
-
moduleRef;
|
|
33
|
-
resolver;
|
|
34
|
-
constructor(reflector, gate, moduleRef, resolver) {
|
|
35
|
-
this.reflector = reflector;
|
|
36
|
-
this.gate = gate;
|
|
37
|
-
this.moduleRef = moduleRef;
|
|
38
|
-
this.resolver = resolver;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Locate the resource resolver. Prefers the value injected into this module;
|
|
42
|
-
* falls back to a non-strict {@link ModuleRef} lookup so a resolver provided by
|
|
43
|
-
* ANY module (e.g. the app root) is found even though the guard lives in the
|
|
44
|
-
* global AuthzModule.
|
|
45
|
-
*/
|
|
46
|
-
resolveResolver() {
|
|
47
|
-
if (this.resolver)
|
|
48
|
-
return this.resolver;
|
|
49
|
-
try {
|
|
50
|
-
return this.moduleRef.get(RESOURCE_RESOLVER, { strict: false });
|
|
51
|
-
}
|
|
52
|
-
catch {
|
|
53
|
-
return undefined;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
async canActivate(ctx) {
|
|
57
|
-
const meta = this.reflector.getAllAndOverride(CAN_METADATA, [
|
|
58
|
-
ctx.getHandler(),
|
|
59
|
-
ctx.getClass(),
|
|
60
|
-
]);
|
|
61
|
-
if (!meta)
|
|
62
|
-
return true;
|
|
63
|
-
// Ad-hoc gate or class-level → no instance to load.
|
|
64
|
-
if (!meta.resource || meta.classLevel) {
|
|
65
|
-
const resource = meta.classLevel ? meta.resource : undefined;
|
|
66
|
-
await this.gate.authorize(meta.ability, resource);
|
|
67
|
-
return true;
|
|
68
|
-
}
|
|
69
|
-
// Instance ability → resolve the resource by route param / app resolver.
|
|
70
|
-
const resolver = this.resolveResolver();
|
|
71
|
-
if (!resolver) {
|
|
72
|
-
throw new ResourceResolverMissingException(meta.ability);
|
|
73
|
-
}
|
|
74
|
-
const instance = await resolver.resolve(meta.resource, ctx);
|
|
75
|
-
if (instance === undefined) {
|
|
76
|
-
// Not found → deny rather than dispatch a policy with a bogus resource.
|
|
77
|
-
throw new ForbiddenException(`Unauthorized: ${meta.ability}`);
|
|
78
|
-
}
|
|
79
|
-
await this.gate.authorize(meta.ability, instance);
|
|
80
|
-
return true;
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
CanGuard = __decorate([
|
|
84
|
-
Injectable(),
|
|
85
|
-
__param(3, Optional()),
|
|
86
|
-
__param(3, Inject(RESOURCE_RESOLVER)),
|
|
87
|
-
__metadata("design:paramtypes", [Reflector,
|
|
88
|
-
Gate,
|
|
89
|
-
ModuleRef, Object])
|
|
90
|
-
], CanGuard);
|
|
91
|
-
export { CanGuard };
|
|
92
|
-
//# sourceMappingURL=can.guard.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"can.guard.js","sourceRoot":"","sources":["../../src/guard/can.guard.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAGL,kBAAkB,EAClB,MAAM,EACN,UAAU,EACV,QAAQ,GACT,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEpD,OAAO,EAAE,gCAAgC,EAAE,MAAM,yBAAyB,CAAC;AAC3E,OAAO,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAElC,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAG/D;;;;;;;;;;GAUG;AAEI,IAAM,QAAQ,GAAd,MAAM,QAAQ;IAEA;IACA;IACA;IAGA;IANnB,YACmB,SAAoB,EACpB,IAAU,EACV,SAAoB,EAGpB,QAA2B;QAL3B,cAAS,GAAT,SAAS,CAAW;QACpB,SAAI,GAAJ,IAAI,CAAM;QACV,cAAS,GAAT,SAAS,CAAW;QAGpB,aAAQ,GAAR,QAAQ,CAAmB;IAC3C,CAAC;IAEJ;;;;;OAKG;IACK,eAAe;QACrB,IAAI,IAAI,CAAC,QAAQ;YAAE,OAAO,IAAI,CAAC,QAAQ,CAAC;QACxC,IAAI,CAAC;YACH,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAmB,iBAAiB,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;QACpF,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,SAAS,CAAC;QACnB,CAAC;IACH,CAAC;IAED,KAAK,CAAC,WAAW,CAAC,GAAqB;QACrC,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,iBAAiB,CAA0B,YAAY,EAAE;YACnF,GAAG,CAAC,UAAU,EAAE;YAChB,GAAG,CAAC,QAAQ,EAAE;SACf,CAAC,CAAC;QACH,IAAI,CAAC,IAAI;YAAE,OAAO,IAAI,CAAC;QAEvB,oDAAoD;QACpD,IAAI,CAAC,IAAI,CAAC,QAAQ,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;YACtC,MAAM,QAAQ,GAAG,IAAI,CAAC,UAAU,CAAC,CAAC,CAAE,IAAI,CAAC,QAAgC,CAAC,CAAC,CAAC,SAAS,CAAC;YACtF,MAAM,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;YAClD,OAAO,IAAI,CAAC;QACd,CAAC;QAED,yEAAyE;QACzE,MAAM,QAAQ,GAAG,IAAI,CAAC,eAAe,EAAE,CAAC;QACxC,IAAI,CAAC,QAAQ,EAAE,CAAC;YACd,MAAM,IAAI,gCAAgC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAC3D,CAAC;QACD,MAAM,QAAQ,GAAG,MAAM,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC;QAC5D,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;YAC3B,wEAAwE;YACxE,MAAM,IAAI,kBAAkB,CAAC,iBAAiB,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC;QAChE,CAAC;QACD,MAAM,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;QAClD,OAAO,IAAI,CAAC;IACd,CAAC;CACF,CAAA;AApDY,QAAQ;IADpB,UAAU,EAAE;IAMR,WAAA,QAAQ,EAAE,CAAA;IACV,WAAA,MAAM,CAAC,iBAAiB,CAAC,CAAA;qCAJE,SAAS;QACd,IAAI;QACC,SAAS;GAJ5B,QAAQ,CAoDpB"}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { type CanActivate, type ExecutionContext } from '@nestjs/common';
|
|
2
|
-
import { Reflector } from '@nestjs/core';
|
|
3
|
-
import { Gate } from '../gate.js';
|
|
4
|
-
/**
|
|
5
|
-
* Enforces `@Roles(...roles)` on routes.
|
|
6
|
-
*
|
|
7
|
-
* - No `@Roles` metadata → allow (the guard is inert on un-annotated routes).
|
|
8
|
-
* - Otherwise allow when the current user holds ANY of the listed roles.
|
|
9
|
-
*
|
|
10
|
-
* The current user is resolved EXACTLY as the {@link Gate} does — from the optional
|
|
11
|
-
* context accessor (nestjs-context) — and an unauthenticated request is denied by
|
|
12
|
-
* default (`gate.hasAnyRole` returns `false` for a NO_USER caller). The verdict is
|
|
13
|
-
* delegated to {@link Gate.hasAnyRole}; a denial throws `ForbiddenException`.
|
|
14
|
-
*/
|
|
15
|
-
export declare class RolesGuard implements CanActivate {
|
|
16
|
-
private readonly reflector;
|
|
17
|
-
private readonly gate;
|
|
18
|
-
constructor(reflector: Reflector, gate: Gate);
|
|
19
|
-
canActivate(ctx: ExecutionContext): Promise<boolean>;
|
|
20
|
-
}
|
|
21
|
-
//# sourceMappingURL=roles.guard.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"roles.guard.d.ts","sourceRoot":"","sources":["../../src/guard/roles.guard.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,WAAW,EAChB,KAAK,gBAAgB,EAGtB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAGlC;;;;;;;;;;GAUG;AACH,qBACa,UAAW,YAAW,WAAW;IAE1C,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,IAAI;gBADJ,SAAS,EAAE,SAAS,EACpB,IAAI,EAAE,IAAI;IAGvB,WAAW,CAAC,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC;CAS3D"}
|
|
@@ -1,50 +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
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
-
};
|
|
10
|
-
import { ForbiddenException, Injectable, } from '@nestjs/common';
|
|
11
|
-
import { Reflector } from '@nestjs/core';
|
|
12
|
-
import { Gate } from '../gate.js';
|
|
13
|
-
import { ROLES_METADATA } from '../tokens.js';
|
|
14
|
-
/**
|
|
15
|
-
* Enforces `@Roles(...roles)` on routes.
|
|
16
|
-
*
|
|
17
|
-
* - No `@Roles` metadata → allow (the guard is inert on un-annotated routes).
|
|
18
|
-
* - Otherwise allow when the current user holds ANY of the listed roles.
|
|
19
|
-
*
|
|
20
|
-
* The current user is resolved EXACTLY as the {@link Gate} does — from the optional
|
|
21
|
-
* context accessor (nestjs-context) — and an unauthenticated request is denied by
|
|
22
|
-
* default (`gate.hasAnyRole` returns `false` for a NO_USER caller). The verdict is
|
|
23
|
-
* delegated to {@link Gate.hasAnyRole}; a denial throws `ForbiddenException`.
|
|
24
|
-
*/
|
|
25
|
-
let RolesGuard = class RolesGuard {
|
|
26
|
-
reflector;
|
|
27
|
-
gate;
|
|
28
|
-
constructor(reflector, gate) {
|
|
29
|
-
this.reflector = reflector;
|
|
30
|
-
this.gate = gate;
|
|
31
|
-
}
|
|
32
|
-
async canActivate(ctx) {
|
|
33
|
-
const roles = this.reflector.getAllAndOverride(ROLES_METADATA, [
|
|
34
|
-
ctx.getHandler(),
|
|
35
|
-
ctx.getClass(),
|
|
36
|
-
]);
|
|
37
|
-
if (!roles || roles.length === 0)
|
|
38
|
-
return true;
|
|
39
|
-
if (await this.gate.hasAnyRole(roles))
|
|
40
|
-
return true;
|
|
41
|
-
throw new ForbiddenException(`Unauthorized: requires one of [${roles.join(', ')}]`);
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
RolesGuard = __decorate([
|
|
45
|
-
Injectable(),
|
|
46
|
-
__metadata("design:paramtypes", [Reflector,
|
|
47
|
-
Gate])
|
|
48
|
-
], RolesGuard);
|
|
49
|
-
export { RolesGuard };
|
|
50
|
-
//# sourceMappingURL=roles.guard.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"roles.guard.js","sourceRoot":"","sources":["../../src/guard/roles.guard.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAGL,kBAAkB,EAClB,UAAU,GACX,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAClC,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAE9C;;;;;;;;;;GAUG;AAEI,IAAM,UAAU,GAAhB,MAAM,UAAU;IAEF;IACA;IAFnB,YACmB,SAAoB,EACpB,IAAU;QADV,cAAS,GAAT,SAAS,CAAW;QACpB,SAAI,GAAJ,IAAI,CAAM;IAC1B,CAAC;IAEJ,KAAK,CAAC,WAAW,CAAC,GAAqB;QACrC,MAAM,KAAK,GAAG,IAAI,CAAC,SAAS,CAAC,iBAAiB,CAAuB,cAAc,EAAE;YACnF,GAAG,CAAC,UAAU,EAAE;YAChB,GAAG,CAAC,QAAQ,EAAE;SACf,CAAC,CAAC;QACH,IAAI,CAAC,KAAK,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO,IAAI,CAAC;QAC9C,IAAI,MAAM,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC;YAAE,OAAO,IAAI,CAAC;QACnD,MAAM,IAAI,kBAAkB,CAAC,kCAAkC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACtF,CAAC;CACF,CAAA;AAfY,UAAU;IADtB,UAAU,EAAE;qCAGmB,SAAS;QACd,IAAI;GAHlB,UAAU,CAetB"}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { type Type } from '@nestjs/common';
|
|
2
|
-
import type { PolicyInstance } from './types.js';
|
|
3
|
-
/**
|
|
4
|
-
* Maps a resource class → its policy instance. Populated at module init from
|
|
5
|
-
* explicit `policies: []` and/or auto-discovered `@Policy` providers.
|
|
6
|
-
*/
|
|
7
|
-
export declare class PolicyRegistry {
|
|
8
|
-
private readonly byResource;
|
|
9
|
-
private classAbilitiesCache;
|
|
10
|
-
/**
|
|
11
|
-
* Register a policy instance. Throws if the instance's class was not decorated
|
|
12
|
-
* with `@Policy(Resource)`.
|
|
13
|
-
*/
|
|
14
|
-
register(policy: PolicyInstance): void;
|
|
15
|
-
/** Resolve the policy instance for a resource class, or `undefined`. */
|
|
16
|
-
forResource(resource: Type<unknown>): PolicyInstance | undefined;
|
|
17
|
-
/** Resolve the policy for the class of a resource instance, or `undefined`. */
|
|
18
|
-
forInstance(instance: object): PolicyInstance | undefined;
|
|
19
|
-
/** True when a policy is registered for the resource class. */
|
|
20
|
-
has(resource: Type<unknown>): boolean;
|
|
21
|
-
/** All registered policies (for introspection/testing). */
|
|
22
|
-
all(): PolicyInstance[];
|
|
23
|
-
/** All registered resource classes (insertion order). */
|
|
24
|
-
resources(): Type<unknown>[];
|
|
25
|
-
/**
|
|
26
|
-
* Enumerate the CLASS-LEVEL ability method names declared on each registered
|
|
27
|
-
* policy, keyed by resource class. Used by integrations that pre-resolve a
|
|
28
|
-
* user's class-level abilities (e.g. to share them as Inertia props).
|
|
29
|
-
*
|
|
30
|
-
* Walks the policy prototype chain and collects own function-valued members,
|
|
31
|
-
* excluding `constructor` and the reserved `before` hook. Inherited Object
|
|
32
|
-
* members are skipped.
|
|
33
|
-
*
|
|
34
|
-
* Only methods that take NO resource instance are included — heuristically,
|
|
35
|
-
* arity `<= 1` (just `user`, e.g. `create(user)` / `viewAny(user)`). An
|
|
36
|
-
* instance method like `update(user, post)` is excluded: dispatching it
|
|
37
|
-
* against the resource CLASS would call it with the class constructor as
|
|
38
|
-
* `post` and write a bogus class-level verdict.
|
|
39
|
-
*/
|
|
40
|
-
classAbilities(): Array<{
|
|
41
|
-
resource: Type<unknown>;
|
|
42
|
-
abilities: string[];
|
|
43
|
-
}>;
|
|
44
|
-
private computeClassAbilities;
|
|
45
|
-
}
|
|
46
|
-
//# sourceMappingURL=policy-registry.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"policy-registry.d.ts","sourceRoot":"","sources":["../src/policy-registry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,KAAK,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAGvD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjD;;;GAGG;AACH,qBACa,cAAc;IACzB,OAAO,CAAC,QAAQ,CAAC,UAAU,CAA4C;IAIvE,OAAO,CAAC,mBAAmB,CAAsE;IAEjG;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,cAAc,GAAG,IAAI;IAUtC,wEAAwE;IACxE,WAAW,CAAC,QAAQ,EAAE,IAAI,CAAC,OAAO,CAAC,GAAG,cAAc,GAAG,SAAS;IAIhE,+EAA+E;IAC/E,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,cAAc,GAAG,SAAS;IAIzD,+DAA+D;IAC/D,GAAG,CAAC,QAAQ,EAAE,IAAI,CAAC,OAAO,CAAC,GAAG,OAAO;IAIrC,2DAA2D;IAC3D,GAAG,IAAI,cAAc,EAAE;IAIvB,yDAAyD;IACzD,SAAS,IAAI,IAAI,CAAC,OAAO,CAAC,EAAE;IAI5B;;;;;;;;;;;;;;OAcG;IACH,cAAc,IAAI,KAAK,CAAC;QAAE,QAAQ,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;QAAC,SAAS,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC;IAOzE,OAAO,CAAC,qBAAqB;CAsB9B"}
|