@adminforth/audit-log
Advanced tools
Comparing version 1.0.36 to 1.0.37
@@ -66,4 +66,33 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
}); | ||
/** | ||
* Create a custom action in the audit log resource | ||
* @param resourceId - The resourceId of the resource that the action is being performed on. Can be null if the action is not related to a specific resource. | ||
* @param actionId - The id of the action being performed, can be random string | ||
* @param data - The data to be stored in the audit log | ||
* @param user - The adminUser user performing the action | ||
*/ | ||
this.logCustomAction = (resourceId, actionId, data, user) => __awaiter(this, void 0, void 0, function* () { | ||
if (resourceId) { | ||
const resource = this.adminforth.config.resources.find((r) => r.resourceId === resourceId); | ||
if (!resource) { | ||
const similarResource = this.adminforth.config.resources.find((r) => r.resourceId.includes(resourceId)); | ||
throw new Error(`Resource ${resourceId} not found. Did you mean ${similarResource.resourceId}?`); | ||
} | ||
} | ||
const record = { | ||
[this.options.resourceColumns.resourceIdColumnName]: resourceId, | ||
[this.options.resourceColumns.resourceActionColumnName]: actionId, | ||
[this.options.resourceColumns.resourceDataColumnName]: { 'oldRecord': {}, 'newRecord': data }, | ||
[this.options.resourceColumns.resourceUserIdColumnName]: user.pk, | ||
[this.options.resourceColumns.resourceRecordIdColumnName]: null, | ||
[this.options.resourceColumns.resourceCreatedColumnName]: dayjs.utc().format() | ||
}; | ||
const auditLogResource = this.adminforth.config.resources.find((r) => r.resourceId === this.auditLogResource); | ||
yield this.adminforth.createResourceRecord({ resource: auditLogResource, record, adminUser: user }); | ||
}); | ||
this.options = options; | ||
} | ||
instanceUniqueRepresentation(pluginOptions) { | ||
return `single`; | ||
} | ||
modifyResourceConfig(adminforth, resourceConfig) { | ||
@@ -70,0 +99,0 @@ super.modifyResourceConfig(adminforth, resourceConfig); |
37
index.ts
@@ -26,9 +26,13 @@ import type { | ||
instanceUniqueRepresentation(pluginOptions: any) : string { | ||
return `single`; | ||
} | ||
static defaultError = 'Sorry, you do not have access to this resource.' | ||
createLogRecord = async (resource: AdminForthResource, action: AllowedActionsEnum, data: Object, user: AdminUser, oldRecord: Object) => { | ||
createLogRecord = async (resource: AdminForthResource, action: AllowedActionsEnum | string, data: Object, user: AdminUser, oldRecord?: Object) => { | ||
const recordIdFieldName = resource.columns.find((c) => c.primaryKey === true)?.name; | ||
const recordId = data?.[recordIdFieldName] || oldRecord?.[recordIdFieldName]; | ||
const connector = this.adminforth.connectors[resource.dataSource]; | ||
const newRecord = action == AllowedActionsEnum.delete ? {} : (await connector.getRecordByPrimaryKey(resource, recordId)) || {}; | ||
@@ -80,2 +84,31 @@ if (action !== AllowedActionsEnum.delete) { | ||
/** | ||
* Create a custom action in the audit log resource | ||
* @param resourceId - The resourceId of the resource that the action is being performed on. Can be null if the action is not related to a specific resource. | ||
* @param actionId - The id of the action being performed, can be random string | ||
* @param data - The data to be stored in the audit log | ||
* @param user - The adminUser user performing the action | ||
*/ | ||
logCustomAction = async (resourceId: string | null, actionId: string, data: Object, user: AdminUser) => { | ||
if (resourceId) { | ||
const resource = this.adminforth.config.resources.find((r) => r.resourceId === resourceId); | ||
if (!resource) { | ||
const similarResource = this.adminforth.config.resources.find((r) => r.resourceId.includes(resourceId)); | ||
throw new Error(`Resource ${resourceId} not found. Did you mean ${similarResource.resourceId}?`) | ||
} | ||
} | ||
const record = { | ||
[this.options.resourceColumns.resourceIdColumnName]: resourceId, | ||
[this.options.resourceColumns.resourceActionColumnName]: actionId, | ||
[this.options.resourceColumns.resourceDataColumnName]: { 'oldRecord': {}, 'newRecord': data }, | ||
[this.options.resourceColumns.resourceUserIdColumnName]: user.pk, | ||
[this.options.resourceColumns.resourceRecordIdColumnName]: null, | ||
[this.options.resourceColumns.resourceCreatedColumnName]: dayjs.utc().format() | ||
} | ||
const auditLogResource = this.adminforth.config.resources.find((r) => r.resourceId === this.auditLogResource); | ||
await this.adminforth.createResourceRecord({ resource: auditLogResource, record, adminUser: user}); | ||
} | ||
modifyResourceConfig(adminforth: IAdminForth, resourceConfig: AdminForthResource) { | ||
@@ -82,0 +115,0 @@ super.modifyResourceConfig(adminforth, resourceConfig); |
{ | ||
"name": "@adminforth/audit-log", | ||
"version": "1.0.36", | ||
"version": "1.0.37", | ||
"description": "AdminForth plugin for logging all actions (who did what and when)", | ||
@@ -9,4 +9,5 @@ "main": "dist/index.js", | ||
"scripts": { | ||
"rollout": "tsc && cp -rf custom dist/ && npm version patch && npm publish --access public", | ||
"postinstall": "npm link adminforth" | ||
"rollout": "tsc && rsync -av --exclude 'node_modules' custom/ dist/ && npm version patch && npm publish --access public", | ||
"build": "tsc", | ||
"prepare": "npm link adminforth" | ||
}, | ||
@@ -13,0 +14,0 @@ "author": "devforth", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
Found 1 instance in 1 package
30571
9
406
0