Files
ldap-auth-service/lib/zupLog.js
2026-08-24 16:38:57 +05:00

45 lines
1.1 KiB
JavaScript

'use strict';
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const logPath = path.join(__dirname, '..', 'data', 'zup-actions.json');
function readLog() {
try {
const parsed = JSON.parse(fs.readFileSync(logPath, 'utf8'));
return Array.isArray(parsed) ? parsed : [];
} catch (err) {
return [];
}
}
function writeLog(entries) {
fs.mkdirSync(path.dirname(logPath), { recursive: true });
fs.writeFileSync(logPath, JSON.stringify(entries, null, 2), 'utf8');
}
function add({ actor, target, targetFio, action }) {
const entry = {
id: crypto.randomBytes(8).toString('hex'),
ts: Date.now(),
actor: String(actor || ''),
target: String(target || ''),
targetFio: String(targetFio || ''),
action: action === 'unblock' ? 'unblock' : 'block'
};
const entries = readLog();
entries.push(entry);
writeLog(entries);
return entry;
}
function forTarget(login) {
const needle = String(login || '').toLowerCase();
return readLog()
.filter((e) => String(e.target).toLowerCase() === needle)
.sort((a, b) => (b.ts || 0) - (a.ts || 0));
}
module.exports = { add, forTarget, logPath };