Files
ipwhitelist-app/v2/src/impersonation/index.js
T

41 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// ═══════════════════════════════════════════════════════════════════════════════
// V2 — Расширение имперсонации (отдельный модуль)
//
// ЭТО: middleware — добавляет дополнительные компании админу.
// НЕ: жёсткая логика в resolveContext или user/index.js.
//
// ENV:
// IMPERSONATION_ORIGINAL — email админа, который имперсонирует
// IMPERSONATION_COMPANY — W-номер компании имперсонируемого
// IMPERSONATION_EXTRA_COMPANIES — доп. W-номера через запятую
// ═══════════════════════════════════════════════════════════════════════════════
const ORIGINAL_EMAIL = process.env.IMPERSONATION_ORIGINAL || '';
const EXTRA_COMPANIES = [
process.env.IMPERSONATION_COMPANY || '',
...(process.env.IMPERSONATION_EXTRA_COMPANIES || '').split(',').map(s => s.trim())
].filter(Boolean);
function enhanceImpersonation(req, res, next) {
if (!ORIGINAL_EMAIL) return next();
const u = req.session && req.session.user;
if (!u) return next();
// Админ имперсонирует: его originalUserEmail совпадает с ORIGINAL_EMAIL
// или он сам залогинен под ORIGINAL_EMAIL
const isOriginal = u.email === ORIGINAL_EMAIL || u.originalUserEmail === ORIGINAL_EMAIL;
if (!isOriginal) return next();
const current = u.allClientIds || [u.clientId || ''];
u.allClientIds = [...new Set([...current, ...EXTRA_COMPANIES])].filter(Boolean);
if (!u.profiles) u.profiles = [];
for (const cid of EXTRA_COMPANIES) {
if (!u.profiles.find(p => p.client_id === cid))
u.profiles.push({ client_id: cid, company_name: cid, is_active_profile: false });
}
next();
}
module.exports = { enhanceImpersonation };