11import { AdminForthPlugin , suggestIfTypo , AdminForthFilterOperators , Filters , AdminForthDataTypes , rejectApiRawFilters } from "adminforth" ;
2- import type { IAdminForth , IHttpServer , AdminForthResourceColumn , AdminForthComponentDeclaration , AdminForthResource } from "adminforth" ;
2+ import type { IAdminForth , IHttpServer , AdminForthResourceColumn , AdminForthComponentDeclaration , AdminForthResource , AdminUser } from "adminforth" ;
33import type { PluginOptions } from './types.js' ;
44import pLimit from 'p-limit' ;
55
@@ -8,6 +8,7 @@ export default class ImportExport extends AdminForthPlugin {
88 emailField : AdminForthResourceColumn ;
99 authResourceId : string ;
1010 adminforth : IAdminForth ;
11+ auditLogPlugin : Record < string , any > | undefined ;
1112
1213
1314 constructor ( options : PluginOptions ) {
@@ -33,6 +34,29 @@ export default class ImportExport extends AdminForthPlugin {
3334 return errors ;
3435 }
3536
37+ private tryToAuditLogAction ( actionName : 'import' | 'export' , actionDetails : string , adminUser : AdminUser , headers ?: Record < string , string > ) {
38+ if ( ! this . auditLogPlugin ) {
39+ console . warn ( 'AuditLogPlugin not found, skipping audit log for action:' , actionDetails ) ;
40+ return ;
41+ }
42+ try {
43+ this . auditLogPlugin . logCustomAction ( {
44+ resourceId : this . resourceConfig . resourceId ,
45+ recordId : null ,
46+ actionId : actionName ,
47+ oldData : null ,
48+ data : {
49+ details : actionDetails ,
50+
51+ } ,
52+ user : adminUser ,
53+ headers : headers || { } ,
54+ } ) ;
55+ } catch ( e ) {
56+ console . error ( 'Failed to log action to AuditLogPlugin:' , e ) ;
57+ }
58+ }
59+
3660 async modifyResourceConfig ( adminforth : IAdminForth , resourceConfig : AdminForthResource ) {
3761 super . modifyResourceConfig ( adminforth , resourceConfig ) ;
3862 if ( ! resourceConfig . options . pageInjections ) {
@@ -61,6 +85,11 @@ export default class ImportExport extends AdminForthPlugin {
6185
6286 validateConfigAfterDiscover ( adminforth : IAdminForth , resourceConfig : AdminForthResource ) {
6387 // optional method where you can safely check field types after database discovery was performed
88+ try {
89+ this . auditLogPlugin = this . adminforth . getPluginByClassName ( 'AuditLogPlugin' ) ;
90+ } catch ( e ) {
91+ console . warn ( 'Failed to get AuditLogPlugin for imort-export plugin. Audit logging will be skipped.' ) ;
92+ }
6493 }
6594
6695 instanceUniqueRepresentation ( pluginOptions : any ) : string {
@@ -73,7 +102,7 @@ export default class ImportExport extends AdminForthPlugin {
73102 server . endpoint ( {
74103 method : 'POST' ,
75104 path : `/plugin/${ this . pluginInstanceId } /export-csv` ,
76- handler : async ( { body } ) => {
105+ handler : async ( { body, adminUser , headers } ) => {
77106 const { filters, sort } = body ;
78107 const rawFilterError = rejectApiRawFilters ( body . filters ) ;
79108 if ( rawFilterError ) {
@@ -103,6 +132,8 @@ export default class ImportExport extends AdminForthPlugin {
103132 return columns . map ( ( col ) => row [ col . name ] ) ;
104133 } ) ;
105134
135+ this . tryToAuditLogAction ( 'export' , `Export CSV with filters: ${ JSON . stringify ( filters ) } and sort: ${ JSON . stringify ( sort ) } . Total records: ${ rows . length } ` , adminUser , headers ) ;
136+
106137 return {
107138 data : { fields, data : rows } ,
108139 columnsToForceQuote,
@@ -117,6 +148,7 @@ export default class ImportExport extends AdminForthPlugin {
117148 path : `/plugin/${ this . pluginInstanceId } /import-csv` ,
118149 handler : async ( { body, adminUser, query, headers, cookies, requestUrl, response } ) => {
119150 const { data } = body ;
151+ this . tryToAuditLogAction ( 'import' , `Import CSV with ${ Object . keys ( data ) . length } columns` , adminUser , headers ) ;
120152 const columns = this . getColumnNames ( data ) ;
121153 const { errors, resourceColumns } = this . validateColumns ( columns ) ;
122154 const resource = this . adminforth . config . resources . find ( r => r . resourceId === this . resourceConfig . resourceId ) ;
@@ -184,6 +216,7 @@ export default class ImportExport extends AdminForthPlugin {
184216 path : `/plugin/${ this . pluginInstanceId } /import-csv-new-only` ,
185217 handler : async ( { body, adminUser, query, headers, cookies, requestUrl, response } ) => {
186218 const { data } = body ;
219+ this . tryToAuditLogAction ( 'import' , `Import CSV (new only) with ${ Object . keys ( data ) . length } columns` , adminUser , headers ) ;
187220 const columns = this . getColumnNames ( data ) ;
188221 const resource = this . adminforth . config . resources . find ( r => r . resourceId === this . resourceConfig . resourceId ) ;
189222 const { errors, resourceColumns } = this . validateColumns ( columns ) ;
0 commit comments