@nocobase/server 1.5.0-beta.3 → 1.5.0-beta.4
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 +13 -2
- package/lib/application.js +29 -3
- package/lib/audit-manager/index.d.ts +115 -0
- package/lib/audit-manager/index.js +326 -0
- 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/plugin-manager/deps.js +1 -1
- package/lib/plugin-manager/plugin-manager.js +31 -0
- 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/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,9 @@ 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 { AuditManager } from './audit-manager';
|
|
32
35
|
export type PluginType = string | typeof Plugin;
|
|
33
36
|
export type PluginConfiguration = PluginType | [PluginType, any];
|
|
34
37
|
export interface ResourceManagerOptions {
|
|
@@ -58,6 +61,8 @@ export interface ApplicationOptions {
|
|
|
58
61
|
*/
|
|
59
62
|
resourcer?: ResourceManagerOptions;
|
|
60
63
|
resourceManager?: ResourceManagerOptions;
|
|
64
|
+
pubSubManager?: PubSubManagerOptions;
|
|
65
|
+
syncMessageManager?: any;
|
|
61
66
|
bodyParser?: any;
|
|
62
67
|
cors?: any;
|
|
63
68
|
dataWrapping?: boolean;
|
|
@@ -72,6 +77,8 @@ export interface ApplicationOptions {
|
|
|
72
77
|
pmSock?: string;
|
|
73
78
|
name?: string;
|
|
74
79
|
authManager?: AuthManagerOptions;
|
|
80
|
+
auditManager?: AuditManager;
|
|
81
|
+
lockManager?: LockManagerOptions;
|
|
75
82
|
/**
|
|
76
83
|
* @internal
|
|
77
84
|
*/
|
|
@@ -152,7 +159,8 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
|
|
|
152
159
|
/**
|
|
153
160
|
* @internal
|
|
154
161
|
*/
|
|
155
|
-
|
|
162
|
+
pubSubManager: PubSubManager;
|
|
163
|
+
syncMessageManager: SyncMessageManager;
|
|
156
164
|
requestLogger: Logger;
|
|
157
165
|
protected plugins: Map<string, Plugin<any>>;
|
|
158
166
|
protected _appSupervisor: AppSupervisor;
|
|
@@ -161,6 +169,7 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
|
|
|
161
169
|
private _maintainingCommandStatus;
|
|
162
170
|
private _maintainingStatusBeforeCommand;
|
|
163
171
|
private _actionCommand;
|
|
172
|
+
lockManager: LockManager;
|
|
164
173
|
constructor(options: ApplicationOptions);
|
|
165
174
|
private static staticCommands;
|
|
166
175
|
static addCommand(callback: (app: Application) => void): void;
|
|
@@ -212,6 +221,8 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
|
|
|
212
221
|
get acl(): import("@nocobase/acl").ACL;
|
|
213
222
|
protected _authManager: AuthManager;
|
|
214
223
|
get authManager(): AuthManager;
|
|
224
|
+
protected _auditManager: AuditManager;
|
|
225
|
+
get auditManager(): AuditManager;
|
|
215
226
|
protected _locales: Locale;
|
|
216
227
|
/**
|
|
217
228
|
* 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,11 @@ 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_available_action = require("./acl/available-action");
|
|
80
|
+
var import_audit_manager = require("./audit-manager");
|
|
77
81
|
const _Application = class _Application extends import_koa.default {
|
|
78
82
|
constructor(options) {
|
|
79
83
|
super();
|
|
@@ -112,7 +116,8 @@ const _Application = class _Application extends import_koa.default {
|
|
|
112
116
|
/**
|
|
113
117
|
* @internal
|
|
114
118
|
*/
|
|
115
|
-
|
|
119
|
+
pubSubManager;
|
|
120
|
+
syncMessageManager;
|
|
116
121
|
requestLogger;
|
|
117
122
|
plugins = /* @__PURE__ */ new Map();
|
|
118
123
|
_appSupervisor = import_app_supervisor.AppSupervisor.getInstance();
|
|
@@ -121,6 +126,7 @@ const _Application = class _Application extends import_koa.default {
|
|
|
121
126
|
_maintainingCommandStatus;
|
|
122
127
|
_maintainingStatusBeforeCommand;
|
|
123
128
|
_actionCommand;
|
|
129
|
+
lockManager;
|
|
124
130
|
static addCommand(callback) {
|
|
125
131
|
this.staticCommands.push(callback);
|
|
126
132
|
}
|
|
@@ -214,6 +220,10 @@ const _Application = class _Application extends import_koa.default {
|
|
|
214
220
|
get authManager() {
|
|
215
221
|
return this._authManager;
|
|
216
222
|
}
|
|
223
|
+
_auditManager;
|
|
224
|
+
get auditManager() {
|
|
225
|
+
return this._auditManager;
|
|
226
|
+
}
|
|
217
227
|
_locales;
|
|
218
228
|
/**
|
|
219
229
|
* This method is deprecated and should not be used.
|
|
@@ -346,6 +356,9 @@ const _Application = class _Application extends import_koa.default {
|
|
|
346
356
|
if (this.cacheManager) {
|
|
347
357
|
await this.cacheManager.close();
|
|
348
358
|
}
|
|
359
|
+
if (this.pubSubManager) {
|
|
360
|
+
await this.pubSubManager.close();
|
|
361
|
+
}
|
|
349
362
|
if (this.telemetry.started) {
|
|
350
363
|
await this.telemetry.shutdown();
|
|
351
364
|
}
|
|
@@ -820,7 +833,12 @@ const _Application = class _Application extends import_koa.default {
|
|
|
820
833
|
this._cronJobManager = new import_cron_job_manager.CronJobManager(this);
|
|
821
834
|
this._cli = this.createCLI();
|
|
822
835
|
this._i18n = (0, import_helper.createI18n)(options);
|
|
823
|
-
this.
|
|
836
|
+
this.pubSubManager = (0, import_pub_sub_manager.createPubSubManager)(this, options.pubSubManager);
|
|
837
|
+
this.syncMessageManager = new import_sync_message_manager.SyncMessageManager(this, options.syncMessageManager);
|
|
838
|
+
this.lockManager = new import_lock_manager.LockManager({
|
|
839
|
+
defaultAdapter: process.env.LOCK_ADAPTER_DEFAULT,
|
|
840
|
+
...options.lockManager
|
|
841
|
+
});
|
|
824
842
|
this.context.db = this.db;
|
|
825
843
|
this.context.resourcer = this.resourceManager;
|
|
826
844
|
this.context.resourceManager = this.resourceManager;
|
|
@@ -841,10 +859,18 @@ const _Application = class _Application extends import_koa.default {
|
|
|
841
859
|
default: "basic",
|
|
842
860
|
...this.options.authManager || {}
|
|
843
861
|
});
|
|
862
|
+
this._auditManager = new import_audit_manager.AuditManager();
|
|
844
863
|
this.resourceManager.define({
|
|
845
864
|
name: "auth",
|
|
846
865
|
actions: import_auth.actions
|
|
847
866
|
});
|
|
867
|
+
this._dataSourceManager.afterAddDataSource((dataSource) => {
|
|
868
|
+
if (dataSource.collectionManager instanceof import_data_source_manager.SequelizeCollectionManager) {
|
|
869
|
+
for (const [actionName, actionParams] of Object.entries(import_available_action.availableActions)) {
|
|
870
|
+
dataSource.acl.setAvailableAction(actionName, actionParams);
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
});
|
|
848
874
|
this._dataSourceManager.use(this._authManager.middleware(), { tag: "auth" });
|
|
849
875
|
this._dataSourceManager.use(import_validate_filter_params.default, { tag: "validate-filter-params", before: ["auth"] });
|
|
850
876
|
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,326 @@
|
|
|
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
|
+
if (typeof action !== "string") {
|
|
264
|
+
if (action.getUserInfo) {
|
|
265
|
+
const userInfo = await action.getUserInfo(ctx);
|
|
266
|
+
if (userInfo) {
|
|
267
|
+
if (userInfo.userId) {
|
|
268
|
+
auditLog.userId = userInfo.userId;
|
|
269
|
+
}
|
|
270
|
+
if (userInfo.roleName) {
|
|
271
|
+
auditLog.roleName = userInfo.roleName;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (action.getMetaData) {
|
|
276
|
+
const extra = await action.getMetaData(ctx);
|
|
277
|
+
auditLog.metadata = { ...metadata, ...extra };
|
|
278
|
+
} else {
|
|
279
|
+
const defaultMetaData = await this.getDefaultMetaData(ctx);
|
|
280
|
+
auditLog.metadata = { ...metadata, ...defaultMetaData };
|
|
281
|
+
}
|
|
282
|
+
if (action.getSourceAndTarget) {
|
|
283
|
+
const sourceAndTarget = await action.getSourceAndTarget(ctx);
|
|
284
|
+
if (sourceAndTarget) {
|
|
285
|
+
auditLog.sourceCollection = sourceAndTarget.sourceCollection;
|
|
286
|
+
auditLog.sourceRecordUK = sourceAndTarget.sourceRecordUK;
|
|
287
|
+
auditLog.targetCollection = sourceAndTarget.targetCollection;
|
|
288
|
+
auditLog.targetRecordUK = sourceAndTarget.targetRecordUK;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
} else {
|
|
292
|
+
const defaultMetaData = await this.getDefaultMetaData(ctx);
|
|
293
|
+
auditLog.metadata = { ...metadata, ...defaultMetaData };
|
|
294
|
+
}
|
|
295
|
+
this.logger.log(auditLog);
|
|
296
|
+
} catch (err) {
|
|
297
|
+
(_a = ctx.log) == null ? void 0 : _a.error(err);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
// 中间件
|
|
301
|
+
middleware() {
|
|
302
|
+
return async (ctx, next) => {
|
|
303
|
+
const reqId = ctx.reqId;
|
|
304
|
+
let metadata = {};
|
|
305
|
+
try {
|
|
306
|
+
await next();
|
|
307
|
+
} catch (err) {
|
|
308
|
+
metadata = {
|
|
309
|
+
status: ctx.status,
|
|
310
|
+
errMsg: err.message
|
|
311
|
+
};
|
|
312
|
+
throw err;
|
|
313
|
+
} finally {
|
|
314
|
+
if (this.logger) {
|
|
315
|
+
this.output(ctx, reqId, metadata);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
__name(_AuditManager, "AuditManager");
|
|
322
|
+
let AuditManager = _AuditManager;
|
|
323
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
324
|
+
0 && (module.exports = {
|
|
325
|
+
AuditManager
|
|
326
|
+
});
|
package/lib/helper.js
CHANGED
|
@@ -82,6 +82,7 @@ function registerMiddlewares(app, options) {
|
|
|
82
82
|
}, "generateReqId"),
|
|
83
83
|
{ tag: "generateReqId" }
|
|
84
84
|
);
|
|
85
|
+
app.use(app.auditManager.middleware(), { tag: "audit", after: "generateReqId" });
|
|
85
86
|
app.use((0, import_logger.requestLogger)(app.name, app.requestLogger, (_a = options.logger) == null ? void 0 : _a.request), { tag: "logger" });
|
|
86
87
|
app.use(
|
|
87
88
|
(0, import_cors.default)({
|
package/lib/index.d.ts
CHANGED
|
@@ -14,7 +14,8 @@ export * as middlewares from './middlewares';
|
|
|
14
14
|
export * from './migration';
|
|
15
15
|
export * from './plugin';
|
|
16
16
|
export * from './plugin-manager';
|
|
17
|
-
export * from './
|
|
17
|
+
export * from './audit-manager';
|
|
18
|
+
export * from './pub-sub-manager';
|
|
18
19
|
export declare const OFFICIAL_PLUGIN_PREFIX = "@nocobase/plugin-";
|
|
19
20
|
export { appendToBuiltInPlugins, findAllPlugins, findBuiltInPlugins, findLocalPlugins, packageNameTrim, } from './plugin-manager/findPackageNames';
|
|
20
21
|
export { runPluginStaticImports } from './run-plugin-static-imports';
|
package/lib/index.js
CHANGED
|
@@ -56,7 +56,8 @@ var middlewares = __toESM(require("./middlewares"));
|
|
|
56
56
|
__reExport(src_exports, require("./migration"), module.exports);
|
|
57
57
|
__reExport(src_exports, require("./plugin"), module.exports);
|
|
58
58
|
__reExport(src_exports, require("./plugin-manager"), module.exports);
|
|
59
|
-
__reExport(src_exports, require("./
|
|
59
|
+
__reExport(src_exports, require("./audit-manager"), module.exports);
|
|
60
|
+
__reExport(src_exports, require("./pub-sub-manager"), module.exports);
|
|
60
61
|
var import_findPackageNames = require("./plugin-manager/findPackageNames");
|
|
61
62
|
var import_run_plugin_static_imports = require("./run-plugin-static-imports");
|
|
62
63
|
const OFFICIAL_PLUGIN_PREFIX = "@nocobase/plugin-";
|
|
@@ -76,5 +77,6 @@ const OFFICIAL_PLUGIN_PREFIX = "@nocobase/plugin-";
|
|
|
76
77
|
...require("./migration"),
|
|
77
78
|
...require("./plugin"),
|
|
78
79
|
...require("./plugin-manager"),
|
|
79
|
-
...require("./
|
|
80
|
+
...require("./audit-manager"),
|
|
81
|
+
...require("./pub-sub-manager")
|
|
80
82
|
});
|
package/lib/locale/locale.d.ts
CHANGED
|
@@ -25,6 +25,7 @@ export declare class Locale {
|
|
|
25
25
|
resourceStorers: Registry<ResourceStorer>;
|
|
26
26
|
constructor(app: Application);
|
|
27
27
|
load(): Promise<void>;
|
|
28
|
+
reset(): Promise<void>;
|
|
28
29
|
reload(): Promise<void>;
|
|
29
30
|
setLocaleFn(name: string, fn: (lang: string) => Promise<any>): void;
|
|
30
31
|
registerResourceStorer(name: string, storer: ResourceStorer): void;
|
package/lib/locale/locale.js
CHANGED
|
@@ -61,6 +61,13 @@ const _Locale = class _Locale {
|
|
|
61
61
|
this.app.log.debug("locale resource loaded", { submodule: "locale", method: "onAfterLoad" });
|
|
62
62
|
this.app.setMaintainingMessage("locale resource loaded");
|
|
63
63
|
});
|
|
64
|
+
this.app.syncMessageManager.subscribe("localeManager", async (message) => {
|
|
65
|
+
switch (message.type) {
|
|
66
|
+
case "reload":
|
|
67
|
+
await this.reset();
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
});
|
|
64
71
|
}
|
|
65
72
|
async load() {
|
|
66
73
|
this.cache = await this.app.cacheManager.createCache({
|
|
@@ -70,11 +77,15 @@ const _Locale = class _Locale {
|
|
|
70
77
|
});
|
|
71
78
|
await this.get(this.defaultLang);
|
|
72
79
|
}
|
|
73
|
-
async
|
|
80
|
+
async reset() {
|
|
74
81
|
const storers = Array.from(this.resourceStorers.getValues());
|
|
75
82
|
const promises = storers.map((storer) => storer.reset());
|
|
76
83
|
await Promise.all([this.cache.reset(), ...promises]);
|
|
77
84
|
}
|
|
85
|
+
async reload() {
|
|
86
|
+
await this.reset();
|
|
87
|
+
this.app.syncMessageManager.publish("localeManager", { type: "reload" });
|
|
88
|
+
}
|
|
78
89
|
setLocaleFn(name, fn) {
|
|
79
90
|
this.localeFn.set(name, fn);
|
|
80
91
|
}
|