r2
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
async up(queryInterface, Sequelize) {
|
||||
await queryInterface.createTable('AuditLogs', {
|
||||
id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
|
||||
organizationId: { type: Sequelize.INTEGER },
|
||||
userId: { type: Sequelize.INTEGER },
|
||||
userRole: { type: Sequelize.STRING },
|
||||
action: { type: Sequelize.STRING, allowNull: false },
|
||||
entityType: { type: Sequelize.STRING },
|
||||
entityId: { type: Sequelize.INTEGER },
|
||||
details: { type: Sequelize.TEXT },
|
||||
ipAddress: { type: Sequelize.STRING },
|
||||
userAgent: { type: Sequelize.STRING },
|
||||
createdAt: { type: Sequelize.DATE, allowNull: false },
|
||||
});
|
||||
|
||||
await queryInterface.addIndex('AuditLogs', ['organizationId', 'createdAt']);
|
||||
await queryInterface.addIndex('AuditLogs', ['userId']);
|
||||
await queryInterface.addIndex('AuditLogs', ['action']);
|
||||
await queryInterface.addIndex('AuditLogs', ['entityType', 'entityId']);
|
||||
},
|
||||
|
||||
async down(queryInterface) {
|
||||
await queryInterface.dropTable('AuditLogs');
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,25 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const AuditLog = sequelize.define('AuditLog', {
|
||||
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
|
||||
organizationId: { type: DataTypes.INTEGER },
|
||||
userId: { type: DataTypes.INTEGER },
|
||||
userRole: { type: DataTypes.STRING },
|
||||
action: { type: DataTypes.STRING, allowNull: false },
|
||||
entityType: { type: DataTypes.STRING },
|
||||
entityId: { type: DataTypes.INTEGER },
|
||||
details: {
|
||||
type: DataTypes.TEXT,
|
||||
get() { try { return JSON.parse(this.getDataValue('details') || '{}'); } catch { return {}; } },
|
||||
set(v) { this.setDataValue('details', JSON.stringify(v)); },
|
||||
},
|
||||
ipAddress: { type: DataTypes.STRING },
|
||||
userAgent: { type: DataTypes.STRING },
|
||||
}, {
|
||||
updatedAt: false,
|
||||
createdAt: 'createdAt',
|
||||
});
|
||||
|
||||
return { AuditLog };
|
||||
};
|
||||
@@ -0,0 +1,60 @@
|
||||
'use strict';
|
||||
|
||||
const express = require('express');
|
||||
const { Op } = require('sequelize');
|
||||
const { models } = require('../../core/db');
|
||||
const { authenticateToken, authenticateRole } = require('../../core/auth');
|
||||
const { collection } = require('../../core/respond');
|
||||
const { DateTime } = require('luxon');
|
||||
|
||||
const router = express.Router();
|
||||
router.prefix = 'audit-logs';
|
||||
|
||||
const OWNERS = ['Owner', 'SystemAdmin'];
|
||||
|
||||
router.get('/', authenticateToken, authenticateRole(OWNERS), async (req, res, next) => {
|
||||
try {
|
||||
const { action, userId, entityType, from, to, page = 1, limit = 50 } = req.query;
|
||||
const where = { organizationId: req.user.organizationId };
|
||||
if (action) where.action = action;
|
||||
if (userId) where.userId = parseInt(userId, 10);
|
||||
if (entityType) where.entityType = entityType;
|
||||
if (from) where.createdAt = { [Op.gte]: new Date(from) };
|
||||
if (to) where.createdAt = { ...where.createdAt, [Op.lte]: new Date(to) };
|
||||
|
||||
const offset = (parseInt(page, 10) - 1) * parseInt(limit, 10);
|
||||
const { rows, count } = await models.AuditLog.findAndCountAll({
|
||||
where, limit: +limit, offset, order: [['createdAt', 'DESC']],
|
||||
});
|
||||
collection(res, rows, count, +page, +limit);
|
||||
} catch (err) { next(err); }
|
||||
});
|
||||
|
||||
router.get('/export', authenticateToken, authenticateRole(OWNERS), async (req, res, next) => {
|
||||
try {
|
||||
const { action, userId, entityType, from, to } = req.query;
|
||||
const where = { organizationId: req.user.organizationId };
|
||||
if (action) where.action = action;
|
||||
if (userId) where.userId = parseInt(userId, 10);
|
||||
if (entityType) where.entityType = entityType;
|
||||
if (from) where.createdAt = { [Op.gte]: new Date(from) };
|
||||
if (to) where.createdAt = { ...where.createdAt, [Op.lte]: new Date(to) };
|
||||
|
||||
const rows = await models.AuditLog.findAll({ where, order: [['createdAt', 'DESC']] });
|
||||
|
||||
const date = DateTime.now().toISODate();
|
||||
res.setHeader('Content-Type', 'text/csv');
|
||||
res.setHeader('Content-Disposition', `attachment; filename="audit-log-${date}.csv"`);
|
||||
|
||||
const header = 'id,createdAt,userId,userRole,action,entityType,entityId,ipAddress,details\n';
|
||||
res.write(header);
|
||||
|
||||
for (const row of rows) {
|
||||
const details = JSON.stringify(row.details || {}).replace(/"/g, '""');
|
||||
res.write(`${row.id},"${row.createdAt}",${row.userId},"${row.userRole}","${row.action}","${row.entityType}",${row.entityId},"${row.ipAddress}","${details}"\n`);
|
||||
}
|
||||
res.end();
|
||||
} catch (err) { next(err); }
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user