AuditManager
Overview
AuditManager is the resource audit management module in NocoBase, used to register resource interfaces that need to be audited.
Basic Usage
import { Plugin } from '@nocobase/server';
class PluginCustomAuditResourceServer extends Plugin {
async load() {
this.app.auditManager.registerAction('resource:action');
}
}
Class Methods
setLogger()
Sets the output method for audit logs.
const auditManager = new AuditManager();
auditManager.setLogger({
log: async (auditLog: AuditLog) => console.log(auditLog);
})
Signature
setLogger(logger: AuditLogger)
Type
export interface AuditLog {
uuid: string;
dataSource: string;
resource: string;
action: string;
sourceCollection?: string;
sourceRecordUK?: string;
targetCollection?: string;
targetRecordUK?: string;
userId: string;
roleName: string;
ip: string;
ua: string;
status: number;
metadata?: Record<string, any>;
}
export interface AuditLogger {
log(auditLog: AuditLog): Promise<void>;
}
registerAction()
Registers a resource action to be audited.
Signature
registerAction(action: Action)
Type
export interface UserInfo {
userId?: string;
roleName?: string;
}
export interface SourceAndTarget {
sourceCollection?: string;
sourceRecordUK?: string;
targetCollection?: string;
targetRecordUK?: string;
}
type Action =
| string
| {
name: string;
getMetaData?: (ctx: Context) => Promise<Record<string, any>>;
getUserInfo?: (ctx: Context) => Promise<UserInfo>;
getSourceAndTarget?: (ctx: Context) => Promise<SourceAndTarget>;
};
Details
Several writing styles are supported:
- Apply to all resources
registerActions(['create']);
- Apply to all actions of a specific resource
resource:*
registerActions(['app:*']);
- Apply to a specific action of a specific resource
resource:action
registerAction(['pm:update']);
- Supports passing custom
getMetaData, getUserInfo, and getSourceAndTarget methods for the action
registerActions([
'create',
{ name: 'auth:signIn', getMetaData, getUserInfo, getSourceAndTarget },
]);
When registered interfaces overlap, the more specific registration method has higher priority. For example:
-
registerActions('create')
-
registerAction({ name: 'user:*', getMetaData })
-
registerAction({ name: 'user:create', getMetaData })
For the user:create interface, 3 will take effect.
registerActions()
Registers multiple resource actions to be audited.
Signature
registerActions(actions: Action[])