javascript - 在 Strapi.io 中实施审计跟踪/日志
问题描述
我正在尝试创建一个基本的审计跟踪/日志。这是下面的代码,可以更好地理解我要完成的工作。
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)
);
}
});
},
};
};
要使上述工作正常进行,您需要进入 localhost://1337 中的 Strapi.io 仪表板并创建一个名为 Trails 的“集合类型”,并将 9 个条目添加到此集合类型中,如下所示:
- 内容类型:文本
- 行动:文字
- 内容:JSON
- 作者:JSON
- 请求:JSON
- 方法:文字
- 路线:文字
- 参数:JSON
- 状态码:数字
解决方案
这看起来是基于这里的例子strapi audit
但是,为了让这个工作,我需要改变这个
module.exports = (strapi) => {
对此
module.exports = strapi => {
然后为了正确保存内容,我必须将对 service.content_type.create 的调用更改为显式而不是 Json.stringify()....
这是我的代码
推荐阅读
- php - Sylius:如何使用 SyliusResourceBundle 正确呈现实体上的图像预览?
- c++ - ssh_channel_write 和 read 之间的时间异常长?
- typescript - 没有从坐标获取时区
- c# - 以编程方式在 Win 10 中切换焦点辅助模式
- intellij-idea - 如何启用 Flutter/Dart 语言实验?
- windows-7 - 为什么格式化 win 7 电脑到 windows 10 的 CPU-Z bench 分数翻了一番?
- ios - 如何使用昵称代替全名 Apple Developer Account?
- css - Angular 7/CSS 动画:将焦点上的表单字段扩展指定数量?
- react-native - 如何根据情况用图像填充空白处
- maven - 通过 maven 命令运行时如何修复 system.getenv 错误?