@nest-omni/core 1.0.29 → 1.0.31
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/decorators/omni-auth.decorator.d.ts +7 -3
- package/decorators/omni-auth.decorator.js +16 -13
- package/package.json +1 -1
- package/providers/context.provider.d.ts +2 -0
- package/providers/context.provider.js +9 -0
- package/setup/bootstrap.setup.js +1 -2
- package/tsconfig.tsbuildinfo +1 -1
- package/interceptors/auth-user.interceptor.d.ts +0 -4
- package/interceptors/auth-user.interceptor.js +0 -24
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
export interface IOmniAuth {
|
|
2
|
-
uid:
|
|
2
|
+
uid: string;
|
|
3
3
|
username: string;
|
|
4
4
|
tenantId: string;
|
|
5
5
|
appId: string;
|
|
6
|
+
admin: boolean;
|
|
7
|
+
role: string;
|
|
6
8
|
}
|
|
7
|
-
export declare const OmniAuth: (...dataOrPipes: (
|
|
8
|
-
export declare function getOmniAuthData
|
|
9
|
+
export declare const OmniAuth: (...dataOrPipes: (import("@nestjs/common").PipeTransform<any, any> | import("@nestjs/common").Type<import("@nestjs/common").PipeTransform<any, any>> | keyof IOmniAuth)[]) => ParameterDecorator;
|
|
10
|
+
export declare function getOmniAuthData<T extends keyof IOmniAuth>(request: {
|
|
11
|
+
headers: Record<string, string>;
|
|
12
|
+
}, key?: T): IOmniAuth[T] | IOmniAuth;
|
|
@@ -2,34 +2,37 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getOmniAuthData = exports.OmniAuth = void 0;
|
|
4
4
|
const common_1 = require("@nestjs/common");
|
|
5
|
-
const
|
|
5
|
+
const lodash_1 = require("lodash");
|
|
6
6
|
const config_1 = require("@nestjs/config");
|
|
7
|
-
exports.OmniAuth = (0, common_1.createParamDecorator)((
|
|
8
|
-
return getOmniAuthData(ctx.switchToHttp().getRequest(),
|
|
7
|
+
exports.OmniAuth = (0, common_1.createParamDecorator)((key, ctx) => {
|
|
8
|
+
return getOmniAuthData(ctx.switchToHttp().getRequest(), key);
|
|
9
9
|
});
|
|
10
|
-
function getOmniAuthData(
|
|
11
|
-
const headers =
|
|
10
|
+
function getOmniAuthData(request, key) {
|
|
11
|
+
const headers = request.headers;
|
|
12
12
|
let user;
|
|
13
|
-
const configService = new config_1.ConfigService(
|
|
14
|
-
if (
|
|
15
|
-
lodash.isEmpty(headers['x-username'])) {
|
|
13
|
+
const configService = new config_1.ConfigService(request);
|
|
14
|
+
if ((0, lodash_1.isEmpty)(headers['x-userid']) || (0, lodash_1.isEmpty)(headers['x-username'])) {
|
|
16
15
|
if (process.env.NODE_ENV === 'dev' || process.env.NODE_ENV === 'uat') {
|
|
17
16
|
user = {
|
|
18
17
|
uid: configService.get('MOCK_UID'),
|
|
19
18
|
username: configService.get('MOCK_USERNAME'),
|
|
20
19
|
appId: configService.get('MOCK_APP_ID'),
|
|
21
20
|
tenantId: configService.get('MOCK_TENANT_ID'),
|
|
21
|
+
admin: configService.get('MOCK_ADMIN', false),
|
|
22
|
+
role: configService.get('MOCK_ROLE', ''),
|
|
22
23
|
};
|
|
23
24
|
}
|
|
24
25
|
}
|
|
25
26
|
else {
|
|
26
27
|
user = {
|
|
27
|
-
uid: headers['x-userid'],
|
|
28
|
-
username: headers['x-username'],
|
|
29
|
-
appId: headers['x-appid'],
|
|
30
|
-
tenantId: headers['x-tenantid'],
|
|
28
|
+
uid: headers['x-userid'] || '',
|
|
29
|
+
username: headers['x-username'] || '',
|
|
30
|
+
appId: headers['x-appid'] || '',
|
|
31
|
+
tenantId: headers['x-tenantid'] || '',
|
|
32
|
+
admin: headers['x-admin'] === '1',
|
|
33
|
+
role: headers['x-role'] || '',
|
|
31
34
|
};
|
|
32
35
|
}
|
|
33
|
-
return
|
|
36
|
+
return key ? user[key] : user;
|
|
34
37
|
}
|
|
35
38
|
exports.getOmniAuthData = getOmniAuthData;
|
package/package.json
CHANGED
|
@@ -28,5 +28,7 @@ export declare class ContextProvider {
|
|
|
28
28
|
static getAuthUserField<T extends keyof IOmniAuth>(field: T): IOmniAuth[T];
|
|
29
29
|
static getAuthUser(): IOmniAuth;
|
|
30
30
|
static getAppId(): string;
|
|
31
|
+
static getRole(): string;
|
|
32
|
+
static getAdmin(): boolean;
|
|
31
33
|
static getTenantId(): string;
|
|
32
34
|
}
|
|
@@ -51,6 +51,9 @@ class ContextProvider {
|
|
|
51
51
|
static getAuthUserField(field) {
|
|
52
52
|
var _a;
|
|
53
53
|
const auth = (_a = ContextProvider.get(ContextProvider.authUserKey)) !== null && _a !== void 0 ? _a : {};
|
|
54
|
+
if (!auth) {
|
|
55
|
+
throw new Error('User information not found.');
|
|
56
|
+
}
|
|
54
57
|
return auth[field];
|
|
55
58
|
}
|
|
56
59
|
static getAuthUser() {
|
|
@@ -59,6 +62,12 @@ class ContextProvider {
|
|
|
59
62
|
static getAppId() {
|
|
60
63
|
return ContextProvider.getAuthUserField('appId');
|
|
61
64
|
}
|
|
65
|
+
static getRole() {
|
|
66
|
+
return ContextProvider.getAuthUserField('role');
|
|
67
|
+
}
|
|
68
|
+
static getAdmin() {
|
|
69
|
+
return ContextProvider.getAuthUserField('admin');
|
|
70
|
+
}
|
|
62
71
|
static getTenantId() {
|
|
63
72
|
return ContextProvider.getAuthUserField('tenantId');
|
|
64
73
|
}
|
package/setup/bootstrap.setup.js
CHANGED
|
@@ -22,7 +22,6 @@ const common_1 = require("@nestjs/common");
|
|
|
22
22
|
const nestjs_sentry_1 = require("@ntegral/nestjs-sentry");
|
|
23
23
|
const nestjs_i18n_1 = require("nestjs-i18n");
|
|
24
24
|
const crud_1 = require("@dataui/crud");
|
|
25
|
-
const auth_user_interceptor_1 = require("../interceptors/auth-user.interceptor");
|
|
26
25
|
const nestjs_cls_1 = require("nestjs-cls");
|
|
27
26
|
crud_1.CrudConfigService.load({
|
|
28
27
|
auth: {
|
|
@@ -69,7 +68,7 @@ function bootstrapSetup(AppModule, SetupSwagger) {
|
|
|
69
68
|
limit: '50mb',
|
|
70
69
|
}), new nestjs_cls_1.ClsMiddleware({}).use, (0, __1.RequestIdMiddleware)(), (0, __1.PowerByMiddleware)(), (0, __1.OmniAuthMiddleware)());
|
|
71
70
|
app.useStaticAssets('public', { prefix: '/' });
|
|
72
|
-
app.useGlobalInterceptors(new __1.LanguageInterceptor(), new
|
|
71
|
+
app.useGlobalInterceptors(new __1.LanguageInterceptor(), new __1.TranslationInterceptor(), new nestjs_pino_1.LoggerErrorInterceptor(), new nestjs_sentry_1.SentryInterceptor({
|
|
73
72
|
filters: [
|
|
74
73
|
{
|
|
75
74
|
type: common_1.HttpException,
|