@nextage/era-lib 1.0.193 → 1.0.194
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/build/utils/tenant.d.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* *
|
|
10
10
|
*********************************************************************************************/
|
|
11
11
|
import { BaseUser, NxContext, NxObject } from '@nextage/nx-frame-be';
|
|
12
|
-
import { ScopedReadModel } from './tenant.interfaces';
|
|
12
|
+
import { ExistenceCheckModel, ScopedReadModel } from './tenant.interfaces';
|
|
13
13
|
/**
|
|
14
14
|
* Resolves the organization a request operates on - ALWAYS exactly one.
|
|
15
15
|
*
|
|
@@ -95,4 +95,73 @@ export declare function getTenant(user?: BaseUser): string;
|
|
|
95
95
|
* @throws `AppError('messages.notFound')` when the document is out of scope
|
|
96
96
|
*/
|
|
97
97
|
export declare function assertTenantScope(model: ScopedReadModel, id: string, context?: NxContext): Promise<string>;
|
|
98
|
+
/**
|
|
99
|
+
* Answers whether the organization NAMED by a request actually exists (T49).
|
|
100
|
+
*
|
|
101
|
+
* {@link resolveTenant} answers one question - "may this user REACH the
|
|
102
|
+
* selection" - and for a cross-tenant role the answer is "any of them", so it
|
|
103
|
+
* hands the value back untouched. EXISTENCE was nobody's job: an `orgId` naming
|
|
104
|
+
* no organization, never created or since deleted, travelled through unchecked.
|
|
105
|
+
*
|
|
106
|
+
* On reads that costs nothing: the filter matches no document and the caller gets
|
|
107
|
+
* an empty list. It bites on WRITES, because {@link setTenant} also runs inside
|
|
108
|
+
* `beforeCreate`: the document is stored with a `tenantId` pointing nowhere,
|
|
109
|
+
* unreachable by every later query and invisible to any tenant-wide cascade,
|
|
110
|
+
* which keys off an `OrganizationRemoved` event that will never arrive for an
|
|
111
|
+
* organization that never existed.
|
|
112
|
+
*
|
|
113
|
+
* ### Where to call it
|
|
114
|
+
*
|
|
115
|
+
* In the REQUEST CONTEXT of the service - the one place `orgId` enters, crossed
|
|
116
|
+
* ONCE per request - and NOT inside `resolveTenant`. That helper is synchronous
|
|
117
|
+
* and `setTenant` calls it from `beforeCreate`/`beforeGet`/`updateFilters`, hooks
|
|
118
|
+
* that run on every single query: making it asynchronous would cost a database
|
|
119
|
+
* round trip per query, and a signature change rippling through every caller, to
|
|
120
|
+
* answer a question that only changes when the request does.
|
|
121
|
+
*
|
|
122
|
+
* ⚠️ A service whose `orgId` reaches it only through checked paths does NOT need
|
|
123
|
+
* this: auth-srv, for one, takes the organization from mutation ARGUMENTS and
|
|
124
|
+
* passes it to `assertOrgScope`, which re-reads the document and fails when it is
|
|
125
|
+
* missing - the check there would be dead code. Wire it where the selection
|
|
126
|
+
* enters raw, from a header or from connection params.
|
|
127
|
+
*
|
|
128
|
+
* ### Fail-closed, and mute
|
|
129
|
+
*
|
|
130
|
+
* A non-existent selection is refused with the very same `messages.forbidden` as
|
|
131
|
+
* an out-of-reach one: telling them apart would answer a question the caller is
|
|
132
|
+
* not entitled to ask - whether someone else's organization exists, or has been
|
|
133
|
+
* deleted.
|
|
134
|
+
*
|
|
135
|
+
* A malformed value is refused likewise and deliberately BEFORE the query: the
|
|
136
|
+
* selection is client-supplied, and handing an arbitrary string to mongoose would
|
|
137
|
+
* raise a CastError - a 500 where a denial belongs. Note that `toObjectId` does
|
|
138
|
+
* NOT throw on a bad value, it returns the string unchanged, so the guard cannot
|
|
139
|
+
* be left to it.
|
|
140
|
+
*
|
|
141
|
+
* NO selection means "the caller's own organization" (rule 1) and there is
|
|
142
|
+
* nothing to verify: the check is a no-op, so requests that simply do not select
|
|
143
|
+
* are unaffected.
|
|
144
|
+
*
|
|
145
|
+
* @param model the organization model, or any local projection standing in for it
|
|
146
|
+
* @param orgId the organization named by the request, when any
|
|
147
|
+
* @returns `true` when the selection is usable, `false` when it names nothing
|
|
148
|
+
*/
|
|
149
|
+
export declare function selectedOrgExists(model: ExistenceCheckModel, orgId?: string): Promise<boolean>;
|
|
150
|
+
/**
|
|
151
|
+
* {@link selectedOrgExists} in throwing form, for GraphQL context builders.
|
|
152
|
+
*
|
|
153
|
+
* A rejected context aborts the whole operation, which is exactly what a refusal
|
|
154
|
+
* means here.
|
|
155
|
+
*
|
|
156
|
+
* ⚠️ An Express middleware may NOT be able to use this shape: if the service
|
|
157
|
+
* registers its error handler BEFORE the routes, the error never reaches it, and
|
|
158
|
+
* `AppError` carries its code in `extensions.code` rather than in `.status`, so
|
|
159
|
+
* the handler would answer 500 on an already-written response. There, call
|
|
160
|
+
* {@link selectedOrgExists} and write the refusal directly.
|
|
161
|
+
*
|
|
162
|
+
* @param model the organization model, or any local projection standing in for it
|
|
163
|
+
* @param orgId the organization named by the request, when any
|
|
164
|
+
* @throws `AppError('messages.forbidden')` when the selection names no organization
|
|
165
|
+
*/
|
|
166
|
+
export declare function assertSelectedOrgExists(model: ExistenceCheckModel, orgId?: string): Promise<void>;
|
|
98
167
|
//# sourceMappingURL=tenant.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tenant.d.ts","sourceRoot":"","sources":["../../src/utils/tenant.ts"],"names":[],"mappings":"AAAA;;;;;;;;;+FAS+F;AAE/F,OAAO,EAAY,QAAQ,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"tenant.d.ts","sourceRoot":"","sources":["../../src/utils/tenant.ts"],"names":[],"mappings":"AAAA;;;;;;;;;+FAS+F;AAE/F,OAAO,EAAY,QAAQ,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAI/E,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAS,qBAAqB,CAAC;AAE9E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,wBAAgB,aAAa,CAAC,IAAI,CAAC,EAAE,QAAQ,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAerE;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,QAAQ,EAAE,OAAO,GAAE,SAAc,GAAG,IAAI,CAGvE;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,SAAS,CAAC,IAAI,CAAC,EAAE,QAAQ,GAAG,MAAM,CAMjD;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAsB,iBAAiB,CAAC,KAAK,EAAE,eAAe,EAAE,EAAE,EAAE,MAAM,EAAE,OAAO,GAAE,SAAc,GAAG,OAAO,CAAC,MAAM,CAAC,CAMpH;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AACH,wBAAsB,iBAAiB,CAAC,KAAK,EAAE,mBAAmB,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CASpG;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAsB,uBAAuB,CAAC,KAAK,EAAE,mBAAmB,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAIvG"}
|
|
@@ -16,4 +16,19 @@ import { NxContext } from '@nextage/nx-frame-be';
|
|
|
16
16
|
export interface ScopedReadModel {
|
|
17
17
|
get(id: string, context: NxContext): Promise<unknown>;
|
|
18
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* A model of which only "does a document with this `_id` exist" is needed.
|
|
21
|
+
*
|
|
22
|
+
* Structural for the same reason as {@link ScopedReadModel}, and deliberately
|
|
23
|
+
* built on `mgModel.exists` rather than on `get`: the question is whether the
|
|
24
|
+
* document EXISTS AT ALL, so it must NOT go through `beforeGet` and the tenant
|
|
25
|
+
* scoping it applies - a scoped read would answer "no" for an organization that
|
|
26
|
+
* exists but the caller cannot reach, which is a different question with a
|
|
27
|
+
* different answer.
|
|
28
|
+
*/
|
|
29
|
+
export interface ExistenceCheckModel {
|
|
30
|
+
mgModel: {
|
|
31
|
+
exists(filter: Record<string, unknown>): Promise<unknown>;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
19
34
|
//# sourceMappingURL=tenant.interfaces.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tenant.interfaces.d.ts","sourceRoot":"","sources":["../../src/utils/tenant.interfaces.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAEjD;;;;;;;GAOG;AACH,MAAM,WAAW,eAAe;IAC9B,GAAG,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CACvD"}
|
|
1
|
+
{"version":3,"file":"tenant.interfaces.d.ts","sourceRoot":"","sources":["../../src/utils/tenant.interfaces.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAEjD;;;;;;;GAOG;AACH,MAAM,WAAW,eAAe;IAC9B,GAAG,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CACvD;AAED;;;;;;;;;GASG;AACH,MAAM,WAAW,mBAAmB;IAClC,OAAO,EAAE;QACP,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;KAC3D,CAAC;CACH"}
|
package/build/utils/tenant.js
CHANGED
|
@@ -14,7 +14,10 @@ exports.resolveTenant = resolveTenant;
|
|
|
14
14
|
exports.setTenant = setTenant;
|
|
15
15
|
exports.getTenant = getTenant;
|
|
16
16
|
exports.assertTenantScope = assertTenantScope;
|
|
17
|
+
exports.selectedOrgExists = selectedOrgExists;
|
|
18
|
+
exports.assertSelectedOrgExists = assertSelectedOrgExists;
|
|
17
19
|
const nx_frame_be_1 = require("@nextage/nx-frame-be");
|
|
20
|
+
const mongo_utils_1 = require("@nextage/nx-frame-be/build/common/base/mongo-utils");
|
|
18
21
|
const scope_1 = require("./scope");
|
|
19
22
|
/**
|
|
20
23
|
* Resolves the organization a request operates on - ALWAYS exactly one.
|
|
@@ -121,4 +124,82 @@ async function assertTenantScope(model, id, context = {}) {
|
|
|
121
124
|
throw new nx_frame_be_1.AppError('messages.notFound');
|
|
122
125
|
return id;
|
|
123
126
|
}
|
|
127
|
+
/**
|
|
128
|
+
* Answers whether the organization NAMED by a request actually exists (T49).
|
|
129
|
+
*
|
|
130
|
+
* {@link resolveTenant} answers one question - "may this user REACH the
|
|
131
|
+
* selection" - and for a cross-tenant role the answer is "any of them", so it
|
|
132
|
+
* hands the value back untouched. EXISTENCE was nobody's job: an `orgId` naming
|
|
133
|
+
* no organization, never created or since deleted, travelled through unchecked.
|
|
134
|
+
*
|
|
135
|
+
* On reads that costs nothing: the filter matches no document and the caller gets
|
|
136
|
+
* an empty list. It bites on WRITES, because {@link setTenant} also runs inside
|
|
137
|
+
* `beforeCreate`: the document is stored with a `tenantId` pointing nowhere,
|
|
138
|
+
* unreachable by every later query and invisible to any tenant-wide cascade,
|
|
139
|
+
* which keys off an `OrganizationRemoved` event that will never arrive for an
|
|
140
|
+
* organization that never existed.
|
|
141
|
+
*
|
|
142
|
+
* ### Where to call it
|
|
143
|
+
*
|
|
144
|
+
* In the REQUEST CONTEXT of the service - the one place `orgId` enters, crossed
|
|
145
|
+
* ONCE per request - and NOT inside `resolveTenant`. That helper is synchronous
|
|
146
|
+
* and `setTenant` calls it from `beforeCreate`/`beforeGet`/`updateFilters`, hooks
|
|
147
|
+
* that run on every single query: making it asynchronous would cost a database
|
|
148
|
+
* round trip per query, and a signature change rippling through every caller, to
|
|
149
|
+
* answer a question that only changes when the request does.
|
|
150
|
+
*
|
|
151
|
+
* ⚠️ A service whose `orgId` reaches it only through checked paths does NOT need
|
|
152
|
+
* this: auth-srv, for one, takes the organization from mutation ARGUMENTS and
|
|
153
|
+
* passes it to `assertOrgScope`, which re-reads the document and fails when it is
|
|
154
|
+
* missing - the check there would be dead code. Wire it where the selection
|
|
155
|
+
* enters raw, from a header or from connection params.
|
|
156
|
+
*
|
|
157
|
+
* ### Fail-closed, and mute
|
|
158
|
+
*
|
|
159
|
+
* A non-existent selection is refused with the very same `messages.forbidden` as
|
|
160
|
+
* an out-of-reach one: telling them apart would answer a question the caller is
|
|
161
|
+
* not entitled to ask - whether someone else's organization exists, or has been
|
|
162
|
+
* deleted.
|
|
163
|
+
*
|
|
164
|
+
* A malformed value is refused likewise and deliberately BEFORE the query: the
|
|
165
|
+
* selection is client-supplied, and handing an arbitrary string to mongoose would
|
|
166
|
+
* raise a CastError - a 500 where a denial belongs. Note that `toObjectId` does
|
|
167
|
+
* NOT throw on a bad value, it returns the string unchanged, so the guard cannot
|
|
168
|
+
* be left to it.
|
|
169
|
+
*
|
|
170
|
+
* NO selection means "the caller's own organization" (rule 1) and there is
|
|
171
|
+
* nothing to verify: the check is a no-op, so requests that simply do not select
|
|
172
|
+
* are unaffected.
|
|
173
|
+
*
|
|
174
|
+
* @param model the organization model, or any local projection standing in for it
|
|
175
|
+
* @param orgId the organization named by the request, when any
|
|
176
|
+
* @returns `true` when the selection is usable, `false` when it names nothing
|
|
177
|
+
*/
|
|
178
|
+
async function selectedOrgExists(model, orgId) {
|
|
179
|
+
if (!orgId)
|
|
180
|
+
return true;
|
|
181
|
+
if (!(0, mongo_utils_1.isValidObjectId)(orgId))
|
|
182
|
+
return false;
|
|
183
|
+
return !!await model.mgModel.exists({ _id: (0, mongo_utils_1.toObjectId)(orgId) });
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* {@link selectedOrgExists} in throwing form, for GraphQL context builders.
|
|
187
|
+
*
|
|
188
|
+
* A rejected context aborts the whole operation, which is exactly what a refusal
|
|
189
|
+
* means here.
|
|
190
|
+
*
|
|
191
|
+
* ⚠️ An Express middleware may NOT be able to use this shape: if the service
|
|
192
|
+
* registers its error handler BEFORE the routes, the error never reaches it, and
|
|
193
|
+
* `AppError` carries its code in `extensions.code` rather than in `.status`, so
|
|
194
|
+
* the handler would answer 500 on an already-written response. There, call
|
|
195
|
+
* {@link selectedOrgExists} and write the refusal directly.
|
|
196
|
+
*
|
|
197
|
+
* @param model the organization model, or any local projection standing in for it
|
|
198
|
+
* @param orgId the organization named by the request, when any
|
|
199
|
+
* @throws `AppError('messages.forbidden')` when the selection names no organization
|
|
200
|
+
*/
|
|
201
|
+
async function assertSelectedOrgExists(model, orgId) {
|
|
202
|
+
if (!await selectedOrgExists(model, orgId))
|
|
203
|
+
throw new nx_frame_be_1.AppError('messages.forbidden');
|
|
204
|
+
}
|
|
124
205
|
//# sourceMappingURL=tenant.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tenant.js","sourceRoot":"","sources":["../../src/utils/tenant.ts"],"names":[],"mappings":";AAAA;;;;;;;;;+FAS+F;;
|
|
1
|
+
{"version":3,"file":"tenant.js","sourceRoot":"","sources":["../../src/utils/tenant.ts"],"names":[],"mappings":";AAAA;;;;;;;;;+FAS+F;;AA0C/F,sCAeC;AAcD,8BAGC;AAaD,8BAMC;AA0BD,8CAMC;AAqDD,8CASC;AAkBD,0DAIC;AA/MD,sDAA+E;AAC/E,oFAA6G;AAE7G,mCAAkE;AAGlE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,SAAgB,aAAa,CAAC,IAAe,EAAE,KAAc;IAE3D,IAAI,CAAC,KAAK;QACR,OAAO,SAAS,CAAC,IAAI,CAAC,CAAC;IAEzB,MAAM,SAAS,GAAG,IAAA,uBAAe,EAAC,IAAI,CAAC,CAAC;IAExC,sEAAsE;IACtE,IAAI,CAAC,SAAS;QACZ,OAAO,KAAK,CAAC;IAEf,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,KAAK,CAAC;QAC5B,MAAM,IAAI,sBAAQ,CAAC,oBAAoB,CAAC,CAAC;IAE3C,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;;;;;;;;;GAWG;AACH,SAAgB,SAAS,CAA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