@@ -244,4 +244,109 @@ Also, update the resource configuration in `./resources/auditLogs.ts`:
244244 }),
245245 ],
246246 }
247+ ` ` `
248+ ## Logging client ip country
249+
250+ Audit log can also log the client's country if needed.
251+
252+ First, you need to migrate the ` audit_logs ` table in ` ./ schema .prisma ` :
253+
254+ ` ` ` ts title = ' ./schema.prisma'
255+ model audit_logs {
256+ id String @id
257+ created_at DateTime // / timestamp of applied change
258+ resource_id String // / identifier of resource where change were applied
259+ user_id String // / identifier of user who made the changes
260+ action String // / type of change (create, edit, delete)
261+ diff String? // / delta betwen before/after versions
262+ record_id String? // / identifier of record that been changed
263+ ip_address String? // / client ip address
264+ // diff-add
265+ country String? // / client country
266+
267+ // diff-add
268+ @@index([ip_address ]) // / index for fast lookups by IP
269+ }
270+ ` ` `
271+
272+ And ` prisma migrate ` :
273+
274+ ` ` ` bash
275+ npm run makemigration -- -- name add - ip - address - to - audit - logs ; npm run migrate : local
276+ ` ` `
277+
278+ Update the resource configuration in ` ./ resources / auditLogs .ts ` :
279+
280+ ` ` ` ts title = ' ./resources/auditLogs.ts'
281+ export default {
282+ dataSource: ' maindb' ,
283+ table: ' audit_logs' ,
284+ columns: [
285+ ...
286+ { name: ' action' , required: false },
287+ { name: ' diff' , required: false , type: AdminForthDataTypes .JSON , showIn: {
288+ list: false ,
289+ edit: false ,
290+ create: false ,
291+ filter: false ,
292+ } },
293+ { name: ' record_id' , required: false },
294+ { name: ' ip_address' , required: false },
295+ // diff-add
296+ {
297+ // diff-add
298+ name: " country" ,
299+ // diff-add
300+ required: false ,
301+ // diff-add
302+ components: {
303+ // diff-add
304+ list: ' @/renderers/CountryFlag.vue'
305+ // diff-add
306+ show : ' @/renderers/CountryFlag.vue'
307+ // diff-add
308+ },
309+ // diff-add
310+ },
311+ ],
312+ ...
313+ plugins : [
314+ new AuditLogPlugin ({
315+ resourceColumns: {
316+ resourceIdColumnName: ' resource_id' ,
317+ resourceActionColumnName: ' action' ,
318+ resourceDataColumnName: ' diff' ,
319+ resourceUserIdColumnName: ' user_id' ,
320+ resourceRecordIdColumnName: ' record_id' ,
321+ resourceCreatedColumnName: ' created_at'
322+ resourceIpColumnName : " ip_address" ,
323+ // diff-add
324+ resourceCountryColumnName: " country" ,
325+ }
326+ }),
327+ ],
328+ }
329+ ` ` `
330+
331+ ### Providing Country Headers
332+
333+ If your deployed app has header with user country in ISO 3166-1 alpha-2 format, you can specify this header, so country will be taken from it:
334+
335+ ` ` ` ts
336+ plugins : [
337+ new AuditLogPlugin ({
338+ // diff-add
339+ isoCountryCodeRequestHeader: ' CF-IPCountry' ,
340+ resourceColumns: {
341+ resourceIdColumnName: ' resource_id' ,
342+ resourceActionColumnName: ' action' ,
343+ resourceDataColumnName: ' diff' ,
344+ resourceUserIdColumnName: ' user_id' ,
345+ resourceRecordIdColumnName: ' record_id' ,
346+ resourceCreatedColumnName: ' created_at'
347+ resourceIpColumnName : " ip_address" ,
348+ resourceCountryColumnName: " country" ,
349+ }
350+ }),
351+ ],
247352` ` `
0 commit comments