@nocobase/server 1.5.0-beta.2 → 1.5.0-beta.20
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/lib/application.d.ts +15 -2
- package/lib/application.js +31 -3
- package/lib/audit-manager/index.d.ts +115 -0
- package/lib/audit-manager/index.js +330 -0
- package/lib/gateway/index.js +73 -22
- package/lib/gateway/ws-server.d.ts +16 -2
- package/lib/gateway/ws-server.js +45 -7
- package/lib/helper.js +1 -0
- package/lib/index.d.ts +2 -1
- package/lib/index.js +4 -2
- package/lib/locale/locale.d.ts +1 -0
- package/lib/locale/locale.js +12 -1
- package/lib/middlewares/data-template.js +5 -5
- package/lib/plugin-manager/deps.js +1 -1
- package/lib/plugin-manager/plugin-manager.d.ts +2 -2
- package/lib/plugin-manager/plugin-manager.js +51 -13
- package/lib/plugin.d.ts +3 -12
- package/lib/plugin.js +6 -11
- package/lib/pub-sub-manager/handler-manager.d.ts +34 -0
- package/lib/pub-sub-manager/handler-manager.js +149 -0
- package/lib/pub-sub-manager/index.d.ts +11 -0
- package/lib/pub-sub-manager/index.js +34 -0
- package/lib/pub-sub-manager/pub-sub-manager.d.ts +27 -0
- package/lib/pub-sub-manager/pub-sub-manager.js +116 -0
- package/lib/pub-sub-manager/types.d.ts +27 -0
- package/lib/pub-sub-manager/types.js +24 -0
- package/lib/service-container.d.ts +5 -0
- package/lib/service-container.js +50 -0
- package/lib/sync-message-manager.d.ts +25 -0
- package/lib/sync-message-manager.js +105 -0
- package/package.json +16 -14
- package/lib/sync-manager.d.ts +0 -46
- package/lib/sync-manager.js +0 -137
package/lib/application.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ import Database, { CollectionOptions, IDatabaseOptions } from '@nocobase/databas
|
|
|
15
15
|
import { Logger, LoggerOptions, RequestLoggerOptions, SystemLogger, SystemLoggerOptions } from '@nocobase/logger';
|
|
16
16
|
import { ResourceOptions, Resourcer } from '@nocobase/resourcer';
|
|
17
17
|
import { Telemetry, TelemetryOptions } from '@nocobase/telemetry';
|
|
18
|
+
import { LockManager, LockManagerOptions } from '@nocobase/lock-manager';
|
|
18
19
|
import { AsyncEmitter, ToposortOptions } from '@nocobase/utils';
|
|
19
20
|
import { Command, CommandOptions, ParseOptions } from 'commander';
|
|
20
21
|
import { IncomingMessage, ServerResponse } from 'http';
|
|
@@ -28,7 +29,10 @@ import { ApplicationVersion } from './helpers/application-version';
|
|
|
28
29
|
import { Locale } from './locale';
|
|
29
30
|
import { Plugin } from './plugin';
|
|
30
31
|
import { InstallOptions, PluginManager } from './plugin-manager';
|
|
31
|
-
import {
|
|
32
|
+
import { PubSubManager, PubSubManagerOptions } from './pub-sub-manager';
|
|
33
|
+
import { SyncMessageManager } from './sync-message-manager';
|
|
34
|
+
import { ServiceContainer } from './service-container';
|
|
35
|
+
import { AuditManager } from './audit-manager';
|
|
32
36
|
export type PluginType = string | typeof Plugin;
|
|
33
37
|
export type PluginConfiguration = PluginType | [PluginType, any];
|
|
34
38
|
export interface ResourceManagerOptions {
|
|
@@ -58,6 +62,8 @@ export interface ApplicationOptions {
|
|
|
58
62
|
*/
|
|
59
63
|
resourcer?: ResourceManagerOptions;
|
|
60
64
|
resourceManager?: ResourceManagerOptions;
|
|
65
|
+
pubSubManager?: PubSubManagerOptions;
|
|
66
|
+
syncMessageManager?: any;
|
|
61
67
|
bodyParser?: any;
|
|
62
68
|
cors?: any;
|
|
63
69
|
dataWrapping?: boolean;
|
|
@@ -72,6 +78,8 @@ export interface ApplicationOptions {
|
|
|
72
78
|
pmSock?: string;
|
|
73
79
|
name?: string;
|
|
74
80
|
authManager?: AuthManagerOptions;
|
|
81
|
+
auditManager?: AuditManager;
|
|
82
|
+
lockManager?: LockManagerOptions;
|
|
75
83
|
/**
|
|
76
84
|
* @internal
|
|
77
85
|
*/
|
|
@@ -152,7 +160,8 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
|
|
|
152
160
|
/**
|
|
153
161
|
* @internal
|
|
154
162
|
*/
|
|
155
|
-
|
|
163
|
+
pubSubManager: PubSubManager;
|
|
164
|
+
syncMessageManager: SyncMessageManager;
|
|
156
165
|
requestLogger: Logger;
|
|
157
166
|
protected plugins: Map<string, Plugin<any>>;
|
|
158
167
|
protected _appSupervisor: AppSupervisor;
|
|
@@ -161,6 +170,8 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
|
|
|
161
170
|
private _maintainingCommandStatus;
|
|
162
171
|
private _maintainingStatusBeforeCommand;
|
|
163
172
|
private _actionCommand;
|
|
173
|
+
container: ServiceContainer;
|
|
174
|
+
lockManager: LockManager;
|
|
164
175
|
constructor(options: ApplicationOptions);
|
|
165
176
|
private static staticCommands;
|
|
166
177
|
static addCommand(callback: (app: Application) => void): void;
|
|
@@ -212,6 +223,8 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
|
|
|
212
223
|
get acl(): import("@nocobase/acl").ACL;
|
|
213
224
|
protected _authManager: AuthManager;
|
|
214
225
|
get authManager(): AuthManager;
|
|
226
|
+
protected _auditManager: AuditManager;
|
|
227
|
+
get auditManager(): AuditManager;
|
|
215
228
|
protected _locales: Locale;
|
|
216
229
|
/**
|
|
217
230
|
* This method is deprecated and should not be used.
|
package/lib/application.js
CHANGED
|
@@ -49,6 +49,7 @@ var import_data_source_manager = require("@nocobase/data-source-manager");
|
|
|
49
49
|
var import_database = __toESM(require("@nocobase/database"));
|
|
50
50
|
var import_logger = require("@nocobase/logger");
|
|
51
51
|
var import_telemetry = require("@nocobase/telemetry");
|
|
52
|
+
var import_lock_manager = require("@nocobase/lock-manager");
|
|
52
53
|
var import_utils = require("@nocobase/utils");
|
|
53
54
|
var import_crypto = require("crypto");
|
|
54
55
|
var import_glob = __toESM(require("glob"));
|
|
@@ -72,8 +73,12 @@ var import_middlewares = require("./middlewares");
|
|
|
72
73
|
var import_data_template = require("./middlewares/data-template");
|
|
73
74
|
var import_validate_filter_params = __toESM(require("./middlewares/validate-filter-params"));
|
|
74
75
|
var import_plugin_manager = require("./plugin-manager");
|
|
75
|
-
var
|
|
76
|
+
var import_pub_sub_manager = require("./pub-sub-manager");
|
|
77
|
+
var import_sync_message_manager = require("./sync-message-manager");
|
|
76
78
|
var import_package = __toESM(require("../package.json"));
|
|
79
|
+
var import_service_container = require("./service-container");
|
|
80
|
+
var import_available_action = require("./acl/available-action");
|
|
81
|
+
var import_audit_manager = require("./audit-manager");
|
|
77
82
|
const _Application = class _Application extends import_koa.default {
|
|
78
83
|
constructor(options) {
|
|
79
84
|
super();
|
|
@@ -112,7 +117,8 @@ const _Application = class _Application extends import_koa.default {
|
|
|
112
117
|
/**
|
|
113
118
|
* @internal
|
|
114
119
|
*/
|
|
115
|
-
|
|
120
|
+
pubSubManager;
|
|
121
|
+
syncMessageManager;
|
|
116
122
|
requestLogger;
|
|
117
123
|
plugins = /* @__PURE__ */ new Map();
|
|
118
124
|
_appSupervisor = import_app_supervisor.AppSupervisor.getInstance();
|
|
@@ -121,6 +127,8 @@ const _Application = class _Application extends import_koa.default {
|
|
|
121
127
|
_maintainingCommandStatus;
|
|
122
128
|
_maintainingStatusBeforeCommand;
|
|
123
129
|
_actionCommand;
|
|
130
|
+
container = new import_service_container.ServiceContainer();
|
|
131
|
+
lockManager;
|
|
124
132
|
static addCommand(callback) {
|
|
125
133
|
this.staticCommands.push(callback);
|
|
126
134
|
}
|
|
@@ -214,6 +222,10 @@ const _Application = class _Application extends import_koa.default {
|
|
|
214
222
|
get authManager() {
|
|
215
223
|
return this._authManager;
|
|
216
224
|
}
|
|
225
|
+
_auditManager;
|
|
226
|
+
get auditManager() {
|
|
227
|
+
return this._auditManager;
|
|
228
|
+
}
|
|
217
229
|
_locales;
|
|
218
230
|
/**
|
|
219
231
|
* This method is deprecated and should not be used.
|
|
@@ -346,6 +358,9 @@ const _Application = class _Application extends import_koa.default {
|
|
|
346
358
|
if (this.cacheManager) {
|
|
347
359
|
await this.cacheManager.close();
|
|
348
360
|
}
|
|
361
|
+
if (this.pubSubManager) {
|
|
362
|
+
await this.pubSubManager.close();
|
|
363
|
+
}
|
|
349
364
|
if (this.telemetry.started) {
|
|
350
365
|
await this.telemetry.shutdown();
|
|
351
366
|
}
|
|
@@ -820,7 +835,12 @@ const _Application = class _Application extends import_koa.default {
|
|
|
820
835
|
this._cronJobManager = new import_cron_job_manager.CronJobManager(this);
|
|
821
836
|
this._cli = this.createCLI();
|
|
822
837
|
this._i18n = (0, import_helper.createI18n)(options);
|
|
823
|
-
this.
|
|
838
|
+
this.pubSubManager = (0, import_pub_sub_manager.createPubSubManager)(this, options.pubSubManager);
|
|
839
|
+
this.syncMessageManager = new import_sync_message_manager.SyncMessageManager(this, options.syncMessageManager);
|
|
840
|
+
this.lockManager = new import_lock_manager.LockManager({
|
|
841
|
+
defaultAdapter: process.env.LOCK_ADAPTER_DEFAULT,
|
|
842
|
+
...options.lockManager
|
|
843
|
+
});
|
|
824
844
|
this.context.db = this.db;
|
|
825
845
|
this.context.resourcer = this.resourceManager;
|
|
826
846
|
this.context.resourceManager = this.resourceManager;
|
|
@@ -841,10 +861,18 @@ const _Application = class _Application extends import_koa.default {
|
|
|
841
861
|
default: "basic",
|
|
842
862
|
...this.options.authManager || {}
|
|
843
863
|
});
|
|
864
|
+
this._auditManager = new import_audit_manager.AuditManager();
|
|
844
865
|
this.resourceManager.define({
|
|
845
866
|
name: "auth",
|
|
846
867
|
actions: import_auth.actions
|
|
847
868
|
});
|
|
869
|
+
this._dataSourceManager.afterAddDataSource((dataSource) => {
|
|
870
|
+
if (dataSource.collectionManager instanceof import_data_source_manager.SequelizeCollectionManager) {
|
|
871
|
+
for (const [actionName, actionParams] of Object.entries(import_available_action.availableActions)) {
|
|
872
|
+
dataSource.acl.setAvailableAction(actionName, actionParams);
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
});
|
|
848
876
|
this._dataSourceManager.use(this._authManager.middleware(), { tag: "auth" });
|
|
849
877
|
this._dataSourceManager.use(import_validate_filter_params.default, { tag: "validate-filter-params", before: ["auth"] });
|
|
850
878
|
this._dataSourceManager.use(import_middlewares.parseVariables, {
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
import { Context } from '@nocobase/actions';
|
|
10
|
+
export interface AuditLog {
|
|
11
|
+
uuid: string;
|
|
12
|
+
dataSource: string;
|
|
13
|
+
resource: string;
|
|
14
|
+
action: string;
|
|
15
|
+
sourceCollection?: string;
|
|
16
|
+
sourceRecordUK?: string;
|
|
17
|
+
targetCollection?: string;
|
|
18
|
+
targetRecordUK?: string;
|
|
19
|
+
userId: string;
|
|
20
|
+
roleName: string;
|
|
21
|
+
ip: string;
|
|
22
|
+
ua: string;
|
|
23
|
+
status: number;
|
|
24
|
+
metadata?: Record<string, any>;
|
|
25
|
+
}
|
|
26
|
+
export interface UserInfo {
|
|
27
|
+
userId?: string;
|
|
28
|
+
roleName?: string;
|
|
29
|
+
}
|
|
30
|
+
export interface SourceAndTarget {
|
|
31
|
+
sourceCollection?: string;
|
|
32
|
+
sourceRecordUK?: string;
|
|
33
|
+
targetCollection?: string;
|
|
34
|
+
targetRecordUK?: string;
|
|
35
|
+
}
|
|
36
|
+
export interface AuditLogger {
|
|
37
|
+
log(auditLog: AuditLog): Promise<void>;
|
|
38
|
+
}
|
|
39
|
+
type Action = string | {
|
|
40
|
+
name: string;
|
|
41
|
+
getMetaData?: (ctx: Context) => Promise<Record<string, any>>;
|
|
42
|
+
getUserInfo?: (ctx: Context) => Promise<UserInfo>;
|
|
43
|
+
getSourceAndTarget?: (ctx: Context) => Promise<SourceAndTarget>;
|
|
44
|
+
};
|
|
45
|
+
export declare class AuditManager {
|
|
46
|
+
logger: AuditLogger;
|
|
47
|
+
resources: Map<string, Map<string, Action>>;
|
|
48
|
+
constructor();
|
|
49
|
+
setLogger(logger: AuditLogger): void;
|
|
50
|
+
/**
|
|
51
|
+
* 注册需要参与审计的资源和操作,支持几种写法
|
|
52
|
+
*
|
|
53
|
+
* 对所有资源生效;
|
|
54
|
+
* registerActions(['create'])
|
|
55
|
+
*
|
|
56
|
+
* 对某个资源的所有操作生效 resource:*
|
|
57
|
+
* registerActions(['app:*'])
|
|
58
|
+
*
|
|
59
|
+
* 对某个资源的某个操作生效 resouce:action
|
|
60
|
+
* registerAction(['pm:update'])
|
|
61
|
+
*
|
|
62
|
+
* 支持传getMetaData方法
|
|
63
|
+
*
|
|
64
|
+
* registerActions([
|
|
65
|
+
* 'create',
|
|
66
|
+
* { name: 'auth:signIn', getMetaData}
|
|
67
|
+
* ])
|
|
68
|
+
*
|
|
69
|
+
* 支持传getUserInfo方法
|
|
70
|
+
*
|
|
71
|
+
* registerActions([
|
|
72
|
+
* 'create',
|
|
73
|
+
* { name: 'auth:signIn', getUserInfo }
|
|
74
|
+
* ])
|
|
75
|
+
*
|
|
76
|
+
* 当注册的接口有重叠时,颗粒度细的注册方法优先级更高
|
|
77
|
+
*
|
|
78
|
+
* Action1: registerActions(['create']);
|
|
79
|
+
*
|
|
80
|
+
* Action2: registerAction([{ name: 'user:*', getMetaData }]);
|
|
81
|
+
*
|
|
82
|
+
* Action3: registerAction([{ name: 'user:create', getMetaData }]);
|
|
83
|
+
*
|
|
84
|
+
* 对于user:create接口,以上优先级顺序是 Action3 > Action2 > Action1
|
|
85
|
+
*
|
|
86
|
+
* @param actions 操作列表
|
|
87
|
+
*/
|
|
88
|
+
registerActions(actions: Action[]): void;
|
|
89
|
+
/**
|
|
90
|
+
* 注册单个操作,支持的用法同registerActions
|
|
91
|
+
* @param action 操作
|
|
92
|
+
*/
|
|
93
|
+
registerAction(action: Action): void;
|
|
94
|
+
getAction(action: string, resource?: string): Action;
|
|
95
|
+
getDefaultMetaData(ctx: any): Promise<{
|
|
96
|
+
request: {
|
|
97
|
+
params: any;
|
|
98
|
+
query: any;
|
|
99
|
+
body: any;
|
|
100
|
+
path: any;
|
|
101
|
+
headers: {
|
|
102
|
+
'x-authenticator': any;
|
|
103
|
+
'x-locale': any;
|
|
104
|
+
'x-timezone': any;
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
response: {
|
|
108
|
+
body: any;
|
|
109
|
+
};
|
|
110
|
+
}>;
|
|
111
|
+
formatAuditData(ctx: Context): AuditLog;
|
|
112
|
+
output(ctx: any, reqId: any, metadata?: Record<string, any>): Promise<void>;
|
|
113
|
+
middleware(): (ctx: any, next: any) => Promise<void>;
|
|
114
|
+
}
|
|
115
|
+
export {};
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __create = Object.create;
|
|
11
|
+
var __defProp = Object.defineProperty;
|
|
12
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
13
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
14
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
15
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
16
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
17
|
+
var __export = (target, all) => {
|
|
18
|
+
for (var name in all)
|
|
19
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
20
|
+
};
|
|
21
|
+
var __copyProps = (to, from, except, desc) => {
|
|
22
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
23
|
+
for (let key of __getOwnPropNames(from))
|
|
24
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
25
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
26
|
+
}
|
|
27
|
+
return to;
|
|
28
|
+
};
|
|
29
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
30
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
31
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
32
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
33
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
34
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
35
|
+
mod
|
|
36
|
+
));
|
|
37
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
38
|
+
var audit_manager_exports = {};
|
|
39
|
+
__export(audit_manager_exports, {
|
|
40
|
+
AuditManager: () => AuditManager
|
|
41
|
+
});
|
|
42
|
+
module.exports = __toCommonJS(audit_manager_exports);
|
|
43
|
+
var import_stream = __toESM(require("stream"));
|
|
44
|
+
function isStream(obj) {
|
|
45
|
+
return obj instanceof import_stream.default.Readable || obj instanceof import_stream.default.Writable || obj instanceof import_stream.default.Duplex || obj instanceof import_stream.default.Transform;
|
|
46
|
+
}
|
|
47
|
+
__name(isStream, "isStream");
|
|
48
|
+
const _AuditManager = class _AuditManager {
|
|
49
|
+
logger;
|
|
50
|
+
resources;
|
|
51
|
+
constructor() {
|
|
52
|
+
this.resources = /* @__PURE__ */ new Map();
|
|
53
|
+
}
|
|
54
|
+
setLogger(logger) {
|
|
55
|
+
this.logger = logger;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* 注册需要参与审计的资源和操作,支持几种写法
|
|
59
|
+
*
|
|
60
|
+
* 对所有资源生效;
|
|
61
|
+
* registerActions(['create'])
|
|
62
|
+
*
|
|
63
|
+
* 对某个资源的所有操作生效 resource:*
|
|
64
|
+
* registerActions(['app:*'])
|
|
65
|
+
*
|
|
66
|
+
* 对某个资源的某个操作生效 resouce:action
|
|
67
|
+
* registerAction(['pm:update'])
|
|
68
|
+
*
|
|
69
|
+
* 支持传getMetaData方法
|
|
70
|
+
*
|
|
71
|
+
* registerActions([
|
|
72
|
+
* 'create',
|
|
73
|
+
* { name: 'auth:signIn', getMetaData}
|
|
74
|
+
* ])
|
|
75
|
+
*
|
|
76
|
+
* 支持传getUserInfo方法
|
|
77
|
+
*
|
|
78
|
+
* registerActions([
|
|
79
|
+
* 'create',
|
|
80
|
+
* { name: 'auth:signIn', getUserInfo }
|
|
81
|
+
* ])
|
|
82
|
+
*
|
|
83
|
+
* 当注册的接口有重叠时,颗粒度细的注册方法优先级更高
|
|
84
|
+
*
|
|
85
|
+
* Action1: registerActions(['create']);
|
|
86
|
+
*
|
|
87
|
+
* Action2: registerAction([{ name: 'user:*', getMetaData }]);
|
|
88
|
+
*
|
|
89
|
+
* Action3: registerAction([{ name: 'user:create', getMetaData }]);
|
|
90
|
+
*
|
|
91
|
+
* 对于user:create接口,以上优先级顺序是 Action3 > Action2 > Action1
|
|
92
|
+
*
|
|
93
|
+
* @param actions 操作列表
|
|
94
|
+
*/
|
|
95
|
+
registerActions(actions) {
|
|
96
|
+
actions.forEach((action) => {
|
|
97
|
+
this.registerAction(action);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* 注册单个操作,支持的用法同registerActions
|
|
102
|
+
* @param action 操作
|
|
103
|
+
*/
|
|
104
|
+
registerAction(action) {
|
|
105
|
+
let originAction = "";
|
|
106
|
+
let getMetaData = null;
|
|
107
|
+
let getUserInfo = null;
|
|
108
|
+
let getSourceAndTarget = null;
|
|
109
|
+
if (typeof action === "string") {
|
|
110
|
+
originAction = action;
|
|
111
|
+
} else {
|
|
112
|
+
originAction = action.name;
|
|
113
|
+
getMetaData = action.getMetaData;
|
|
114
|
+
getUserInfo = action.getUserInfo;
|
|
115
|
+
getSourceAndTarget = action.getSourceAndTarget;
|
|
116
|
+
}
|
|
117
|
+
const nameRegex = /^[a-zA-Z0-9_-]+$/;
|
|
118
|
+
const resourceWildcardRegex = /^([a-zA-Z0-9_-]+):\*$/;
|
|
119
|
+
const resourceAndActionRegex = /^([a-zA-Z0-9_-]+):([a-zA-Z0-9_-]+)$/;
|
|
120
|
+
let resourceName = "";
|
|
121
|
+
let actionName = "";
|
|
122
|
+
if (nameRegex.test(originAction)) {
|
|
123
|
+
actionName = originAction;
|
|
124
|
+
resourceName = "__default__";
|
|
125
|
+
}
|
|
126
|
+
if (resourceWildcardRegex.test(originAction)) {
|
|
127
|
+
const match = originAction.match(resourceWildcardRegex);
|
|
128
|
+
resourceName = match[1];
|
|
129
|
+
actionName = "__default__";
|
|
130
|
+
}
|
|
131
|
+
if (resourceAndActionRegex.test(originAction)) {
|
|
132
|
+
const match = originAction.match(resourceAndActionRegex);
|
|
133
|
+
resourceName = match[1];
|
|
134
|
+
actionName = match[2];
|
|
135
|
+
}
|
|
136
|
+
if (!resourceName && !actionName) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
let resource = this.resources.get(resourceName);
|
|
140
|
+
if (!resource) {
|
|
141
|
+
resource = /* @__PURE__ */ new Map();
|
|
142
|
+
this.resources.set(resourceName, resource);
|
|
143
|
+
}
|
|
144
|
+
const saveAction = {
|
|
145
|
+
name: originAction
|
|
146
|
+
};
|
|
147
|
+
if (getMetaData) {
|
|
148
|
+
saveAction.getMetaData = getMetaData;
|
|
149
|
+
}
|
|
150
|
+
if (getUserInfo) {
|
|
151
|
+
saveAction.getUserInfo = getUserInfo;
|
|
152
|
+
}
|
|
153
|
+
if (getSourceAndTarget) {
|
|
154
|
+
saveAction.getSourceAndTarget = getSourceAndTarget;
|
|
155
|
+
}
|
|
156
|
+
resource.set(actionName, saveAction);
|
|
157
|
+
}
|
|
158
|
+
getAction(action, resource) {
|
|
159
|
+
let resourceName = resource;
|
|
160
|
+
if (!resource) {
|
|
161
|
+
resourceName = "__default__";
|
|
162
|
+
}
|
|
163
|
+
if (resourceName === "__default__") {
|
|
164
|
+
const resourceActions = this.resources.get(resourceName);
|
|
165
|
+
if (resourceActions) {
|
|
166
|
+
const resourceAction = resourceActions.get(action);
|
|
167
|
+
if (resourceAction) {
|
|
168
|
+
return resourceAction;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
} else {
|
|
172
|
+
const resourceActions = this.resources.get(resourceName);
|
|
173
|
+
if (resourceActions) {
|
|
174
|
+
let resourceAction = resourceActions.get(action);
|
|
175
|
+
if (resourceAction) {
|
|
176
|
+
return resourceAction;
|
|
177
|
+
} else {
|
|
178
|
+
resourceAction = resourceActions.get("__default__");
|
|
179
|
+
if (resourceAction) {
|
|
180
|
+
return resourceAction;
|
|
181
|
+
} else {
|
|
182
|
+
const defaultResourceActions = this.resources.get("__default__");
|
|
183
|
+
if (defaultResourceActions) {
|
|
184
|
+
const defaultResourceAction = defaultResourceActions.get(action);
|
|
185
|
+
if (defaultResourceAction) {
|
|
186
|
+
return defaultResourceAction;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
} else {
|
|
192
|
+
const resourceActions2 = this.resources.get("__default__");
|
|
193
|
+
if (resourceActions2) {
|
|
194
|
+
const resourceAction = resourceActions2.get(action);
|
|
195
|
+
if (resourceAction) {
|
|
196
|
+
return resourceAction;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
async getDefaultMetaData(ctx) {
|
|
204
|
+
var _a, _b, _c;
|
|
205
|
+
let body = null;
|
|
206
|
+
if (ctx.body) {
|
|
207
|
+
if (!Buffer.isBuffer(ctx.body) && !isStream(ctx.body)) {
|
|
208
|
+
body = ctx.body;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return {
|
|
212
|
+
request: {
|
|
213
|
+
params: ctx.request.params,
|
|
214
|
+
query: ctx.request.query,
|
|
215
|
+
body: ctx.request.body,
|
|
216
|
+
path: ctx.request.path,
|
|
217
|
+
headers: {
|
|
218
|
+
"x-authenticator": (_a = ctx.request) == null ? void 0 : _a.headers["x-authenticator"],
|
|
219
|
+
"x-locale": (_b = ctx.request) == null ? void 0 : _b.headers["x-locale"],
|
|
220
|
+
"x-timezone": (_c = ctx.request) == null ? void 0 : _c.headers["x-timezone"]
|
|
221
|
+
}
|
|
222
|
+
},
|
|
223
|
+
response: {
|
|
224
|
+
body
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
formatAuditData(ctx) {
|
|
229
|
+
var _a, _b, _c;
|
|
230
|
+
const { resourceName } = ctx.action;
|
|
231
|
+
const ipvalues = ctx.request.header["x-forwarded-for"];
|
|
232
|
+
let ipvalue = "";
|
|
233
|
+
if (ipvalues instanceof Array) {
|
|
234
|
+
ipvalue = ipvalues[0];
|
|
235
|
+
} else {
|
|
236
|
+
ipvalue = ipvalues;
|
|
237
|
+
}
|
|
238
|
+
const ips = ipvalue ? ipvalue == null ? void 0 : ipvalue.split(/\s*,\s*/) : [];
|
|
239
|
+
const auditLog = {
|
|
240
|
+
uuid: ctx.reqId,
|
|
241
|
+
dataSource: ctx.request.header["x-data-source"] || "main",
|
|
242
|
+
resource: resourceName,
|
|
243
|
+
action: ctx.action.actionName,
|
|
244
|
+
userId: (_b = (_a = ctx.state) == null ? void 0 : _a.currentUser) == null ? void 0 : _b.id,
|
|
245
|
+
roleName: (_c = ctx.state) == null ? void 0 : _c.currentRole,
|
|
246
|
+
ip: ips.length > 0 ? ips[0] : ctx.request.ip,
|
|
247
|
+
ua: ctx.request.header["user-agent"],
|
|
248
|
+
status: ctx.response.status
|
|
249
|
+
};
|
|
250
|
+
return auditLog;
|
|
251
|
+
}
|
|
252
|
+
async output(ctx, reqId, metadata) {
|
|
253
|
+
var _a;
|
|
254
|
+
try {
|
|
255
|
+
const { resourceName, actionName } = ctx.action;
|
|
256
|
+
const action = this.getAction(actionName, resourceName);
|
|
257
|
+
if (!action) {
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
const auditLog = this.formatAuditData(ctx);
|
|
261
|
+
auditLog.uuid = reqId;
|
|
262
|
+
auditLog.status = ctx.status;
|
|
263
|
+
const defaultMetaData = await this.getDefaultMetaData(ctx);
|
|
264
|
+
auditLog.metadata = { ...metadata, ...defaultMetaData };
|
|
265
|
+
if (typeof action !== "string") {
|
|
266
|
+
if (action.getUserInfo) {
|
|
267
|
+
const userInfo = await action.getUserInfo(ctx);
|
|
268
|
+
if (userInfo) {
|
|
269
|
+
if (userInfo.userId) {
|
|
270
|
+
auditLog.userId = userInfo.userId;
|
|
271
|
+
}
|
|
272
|
+
if (userInfo.roleName) {
|
|
273
|
+
auditLog.roleName = userInfo.roleName;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
if (action.getMetaData) {
|
|
278
|
+
const extra = await action.getMetaData(ctx);
|
|
279
|
+
if (extra) {
|
|
280
|
+
if (extra.request) {
|
|
281
|
+
auditLog.metadata.request = { ...auditLog.metadata.request, ...extra.request };
|
|
282
|
+
}
|
|
283
|
+
if (extra.response) {
|
|
284
|
+
auditLog.metadata.response = { ...auditLog.metadata.response, ...extra.response };
|
|
285
|
+
}
|
|
286
|
+
auditLog.metadata = { ...extra, ...auditLog.metadata };
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
if (action.getSourceAndTarget) {
|
|
290
|
+
const sourceAndTarget = await action.getSourceAndTarget(ctx);
|
|
291
|
+
if (sourceAndTarget) {
|
|
292
|
+
auditLog.sourceCollection = sourceAndTarget.sourceCollection;
|
|
293
|
+
auditLog.sourceRecordUK = sourceAndTarget.sourceRecordUK;
|
|
294
|
+
auditLog.targetCollection = sourceAndTarget.targetCollection;
|
|
295
|
+
auditLog.targetRecordUK = sourceAndTarget.targetRecordUK;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
this.logger.log(auditLog);
|
|
300
|
+
} catch (err) {
|
|
301
|
+
(_a = ctx.log) == null ? void 0 : _a.error(err);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
// 中间件
|
|
305
|
+
middleware() {
|
|
306
|
+
return async (ctx, next) => {
|
|
307
|
+
const reqId = ctx.reqId;
|
|
308
|
+
let metadata = {};
|
|
309
|
+
try {
|
|
310
|
+
await next();
|
|
311
|
+
} catch (err) {
|
|
312
|
+
metadata = {
|
|
313
|
+
status: ctx.status,
|
|
314
|
+
errMsg: err.message
|
|
315
|
+
};
|
|
316
|
+
throw err;
|
|
317
|
+
} finally {
|
|
318
|
+
if (this.logger) {
|
|
319
|
+
this.output(ctx, reqId, metadata);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
__name(_AuditManager, "AuditManager");
|
|
326
|
+
let AuditManager = _AuditManager;
|
|
327
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
328
|
+
0 && (module.exports = {
|
|
329
|
+
AuditManager
|
|
330
|
+
});
|