I am trying to create a basic Audit Trail/Log. Here is the below code to get a better understanding of what I am trying to accomplish.
const removePasswords = (key, value) =>
key === "password" ? undefined : value;
const getContentType = (path) => {
if (path.includes("service-request")) {
return "Service Request";
}
if (path.includes("register")) {
return "Account Registration";
}
if (path.includes("local")) {
return "Account Login";
}
if (path.includes("service")) {
return "Service";
}
if (path.includes("content-types") || path.includes("content-manager")) {
return "Admin";
}
return "Others"
};
const getActionType = (method, path) => {
if (method.toLowerCase() === "post" && path.includes("service-request")) {
return "Created Service Request";
}
if (method.toLowerCase() === "get" && path.includes("content-manager")) {
return "Admin content View";
}
if (method.toLowerCase() === "post" && path.includes("content-manager")) {
return "Admin content create";
}
if (method.toLowerCase() === "put" && path.includes("content-manager")) {
return "Admin content update";
}
if (method.toLowerCase() === "post" && path.includes("register")) {
return "User Register";
}
if (method.toLowerCase() === "post" && path.includes("local")) {
return "User log in";
}
return "Other Activities"
};
module.exports = (strapi) => {
return {
initialize() {
strapi.app.use(async (ctx, next) => {
await next();
console.log("I am running");
if (ctx.state && ctx.state.user) {
const entry = {
contentType: getContentType(ctx._matchedRoute),
action: getActionType(ctx.request.method, ctx._matchedRoute),
statusCode: ctx.response.status,
author: {
id: ctx.state.user.id,
email: ctx.state.user.email,
ip: ctx.request.ip,
},
method: ctx.request.method,
route: ctx._matchedRoute,
params: ctx.params,
request: ctx.request.body,
content: ctx.response.body,
};
if (
(ctx.params.model && ctx.params.model.includes("trail")) ||
(ctx.params.uid && ctx.params.uid.includes("trail"))
) {
//Do nothing
} else {
strapi.services.trails.create(
JSON.stringify(entry, removePasswords)
);
}
}
const entry = {
contentType: getContentType(ctx._matchedRoute),
action: getActionType(ctx.request.method, ctx._matchedRoute),
statusCode: ctx.response.status,
author: {
id:
ctx.response.body && ctx.response.body.user
? ctx.response.body.user.id
: "Not found",
email:
ctx.response.body && ctx.response.body.user
? ctx.response.body.user.email
: "Not found",
ip: ctx.request.ip,
},
method: ctx.request.method,
route: ctx._matchedRoute,
params: ctx.params,
request: ctx.request.body,
content: ctx.response.body,
};
if (
(ctx.params.model && ctx.params.model.includes("trail")) ||
(ctx.params.uid && ctx.params.uid.includes("trail"))
) {
//Do nothing
} else {
// strapi.log.info(entry)
strapi.services.trails.create(
JSON.stringify(entry, removePasswords)
);
}
});
},
};
};
For the above to work you need to go into Strapi.io's dashboard in localhost://1337 and create a "Collection Type" called Trails and add into this collection type 9 entries which are as follows:
This looks to be based on the example here strapi audit
However, In order to get this working, I needed to change this
module.exports = (strapi) => {
To this
module.exports = strapi => {
and then to get the content saving correctly i had to change the calls to service.content_type.create to be explicit instead of the Json.stringify()....
here is my code
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.