Fix jwt-token
This commit is contained in:
@@ -1561,4 +1561,93 @@ router.get('/audit-logs/summary', requireManagementAuth, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Security logs endpoint - view ALL security logs across tenants
|
||||
router.get('/security-logs', requireManagementAuth, async (req, res) => {
|
||||
try {
|
||||
const {
|
||||
page = 1,
|
||||
limit = 50,
|
||||
level = 'all',
|
||||
eventType = 'all',
|
||||
timeRange = '24h',
|
||||
search = ''
|
||||
} = req.query;
|
||||
|
||||
const { SecurityLog } = require('../models');
|
||||
|
||||
// Build where conditions
|
||||
let whereConditions = {};
|
||||
|
||||
// Filter by security level
|
||||
if (level !== 'all') {
|
||||
whereConditions.level = level;
|
||||
}
|
||||
|
||||
// Filter by event type
|
||||
if (eventType !== 'all') {
|
||||
whereConditions.event_type = eventType;
|
||||
}
|
||||
|
||||
// Filter by time range
|
||||
const now = new Date();
|
||||
let startTime;
|
||||
switch (timeRange) {
|
||||
case '1h':
|
||||
startTime = new Date(now.getTime() - 60 * 60 * 1000);
|
||||
break;
|
||||
case '24h':
|
||||
startTime = new Date(now.getTime() - 24 * 60 * 60 * 1000);
|
||||
break;
|
||||
case '7d':
|
||||
startTime = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
|
||||
break;
|
||||
case '30d':
|
||||
startTime = new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000);
|
||||
break;
|
||||
default:
|
||||
startTime = new Date(now.getTime() - 24 * 60 * 60 * 1000);
|
||||
}
|
||||
|
||||
if (timeRange !== 'all') {
|
||||
whereConditions.timestamp = { [Op.gte]: startTime };
|
||||
}
|
||||
|
||||
// Search filter
|
||||
if (search) {
|
||||
whereConditions[Op.or] = [
|
||||
{ message: { [Op.iLike]: `%${search}%` } },
|
||||
{ 'metadata.ip_address': { [Op.iLike]: `%${search}%` } },
|
||||
{ 'metadata.username': { [Op.iLike]: `%${search}%` } },
|
||||
{ 'metadata.tenant_slug': { [Op.iLike]: `%${search}%` } }
|
||||
];
|
||||
}
|
||||
|
||||
const offset = (parseInt(page) - 1) * parseInt(limit);
|
||||
|
||||
const { rows: logs, count: total } = await SecurityLog.findAndCountAll({
|
||||
where: whereConditions,
|
||||
order: [['timestamp', 'DESC']],
|
||||
limit: parseInt(limit),
|
||||
offset: offset
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
logs,
|
||||
total,
|
||||
page: parseInt(page),
|
||||
limit: parseInt(limit),
|
||||
totalPages: Math.ceil(total / parseInt(limit))
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('Management: Error retrieving security logs:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Failed to retrieve security logs',
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user