v2: убраны v2_ префиксы сессии — user/adminMode/token как в основном коде
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "ipwhitelist",
|
||||
"version": "0.5.77",
|
||||
"version": "0.5.78",
|
||||
"description": "IP WhiteList microservice for cloud provider",
|
||||
"main": "server.js",
|
||||
"scripts": {
|
||||
|
||||
@@ -10,9 +10,9 @@ function createAdminRouter() {
|
||||
|
||||
router.get('/', (req, res) => {
|
||||
res.send(`<h2>Admin panel</h2>
|
||||
<p>email: ${req.v2_email}</p>
|
||||
<p>clientId: ${req.v2_clientId}</p>
|
||||
<p>Все компании: ${(req.v2_allClientIds || []).join(', ') || '—'}</p>
|
||||
<p>email: ${req.email}</p>
|
||||
<p>clientId: ${req.clientId}</p>
|
||||
<p>Все компании: ${(req.allClientIds || []).join(', ') || '—'}</p>
|
||||
<p><a href="/v2/app">Пользовательский CRUD</a> | <a href="/v2/logout">Выйти</a></p>
|
||||
`);
|
||||
});
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
// ═══════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
module.exports = {
|
||||
version: '0.5.77',
|
||||
version: '0.5.78',
|
||||
iamUrl: process.env.V2_IAM_API_URL || 'https://auth-api.ngcloud.ru',
|
||||
appUrl: process.env.V2_APP_URL || 'https://whitelist.nodejsk8s.services.ngcloud.ru',
|
||||
};
|
||||
|
||||
@@ -16,17 +16,17 @@ function createCrudRouter() {
|
||||
|
||||
// ── middleware: только для залогиненных ─────────────────────────────────
|
||||
router.use((req, res, next) => {
|
||||
if (!req.session.v2_user) return res.redirect('/v2/login');
|
||||
if (!req.session.user) return res.redirect('/v2/login');
|
||||
next();
|
||||
});
|
||||
|
||||
// ── helpers ──────────────────────────────────────────────────────────────
|
||||
function user(req) { return req.session.v2_user; }
|
||||
function token(req) { return req.session.v2_token; }
|
||||
function user(req) { return req.session.user; }
|
||||
function token(req) { return req.session.token; }
|
||||
|
||||
// activeClientId — текущая выбранная компания
|
||||
function activeClientId(req) {
|
||||
return req.session.v2_user.activeClientId || req.session.v2_user.clientId;
|
||||
return req.session.user.activeClientId || req.session.user.clientId;
|
||||
}
|
||||
|
||||
// Список компаний для выпадающего списка
|
||||
@@ -51,7 +51,7 @@ function createCrudRouter() {
|
||||
|
||||
const switchTo = req.query.switchTo;
|
||||
if (switchTo && allCompanies.find(c => c.client_id === switchTo)) {
|
||||
req.session.v2_user.activeClientId = switchTo;
|
||||
req.session.user.activeClientId = switchTo;
|
||||
return res.redirect('/v2/app');
|
||||
}
|
||||
|
||||
|
||||
@@ -8,10 +8,10 @@
|
||||
* Вызывается перед всеми маршрутами v2.
|
||||
*/
|
||||
function resolveContext(req, res, next) {
|
||||
let u = req.session && req.session.v2_user;
|
||||
const u = req.session && req.session.user;
|
||||
if (!u) return res.redirect('/v2/login');
|
||||
|
||||
const isAdmin = !!(u.isAdmin && req.session.v2_adminMode);
|
||||
const isAdmin = !!(u.isAdmin && req.session.adminMode);
|
||||
const isImpersonated = !!u.isImpersonated;
|
||||
|
||||
// Контекст для CRUD
|
||||
@@ -20,7 +20,7 @@ function resolveContext(req, res, next) {
|
||||
req.v2_isAdmin = isAdmin;
|
||||
req.v2_isImpersonated = isImpersonated;
|
||||
req.v2_impersonatedBy = isImpersonated ? u.email : null; // кто реально (для аудита)
|
||||
req.v2_user = u;
|
||||
req.user = u;
|
||||
req.v2_allClientIds = u.allClientIds || [];
|
||||
req.v2_profiles = u.profiles || [];
|
||||
req.v2_companyName = u.companyName || u.clientId;
|
||||
|
||||
+10
-10
@@ -21,41 +21,41 @@ function t(name, session, expected) {
|
||||
}
|
||||
|
||||
t('нет сессии', {}, { _redirect: '/v2/login' });
|
||||
t('нет v2_user', { v2_user: null }, { _redirect: '/v2/login' });
|
||||
t('нет user', { user: null }, { _redirect: '/v2/login' });
|
||||
|
||||
t('обычный юзер 1 компания', { v2_user: {
|
||||
t('обычный юзер 1 компания', { user: {
|
||||
email: 'u@t.ru', clientId: 'WZ03709', activeClientId: 'WZ03709',
|
||||
allClientIds: ['WZ03709'], companyName: 'test', isAdmin: false, profiles: []
|
||||
}}, { 'v2_email': 'u@t.ru', 'v2_clientId': 'WZ03709', 'v2_isAdmin': false, 'v2_isImpersonated': false, 'v2_impersonatedBy': null });
|
||||
|
||||
t('юзер 2 компании', { v2_user: {
|
||||
t('юзер 2 компании', { user: {
|
||||
email: 'm@t.ru', clientId: 'WZ88888', activeClientId: 'WZ77777',
|
||||
allClientIds: ['WZ88888','WZ77777'], companyName: 'AB', isAdmin: false, profiles: []
|
||||
}}, { 'v2_email': 'm@t.ru', 'v2_clientId': 'WZ77777', 'v2_allClientIds': ['WZ88888','WZ77777'] });
|
||||
|
||||
t('админ adminMode', { v2_user: {
|
||||
t('админ adminMode', { user: {
|
||||
email: 'a@t.ru', clientId: 'WZ01112', activeClientId: 'WZ01112',
|
||||
allClientIds: ['WZ01112'], isAdmin: true, profiles: []
|
||||
}, v2_adminMode: true }, { 'v2_email': 'a@t.ru', 'v2_clientId': 'WZ01112', 'v2_isAdmin': true });
|
||||
}, adminMode: true }, { 'v2_email': 'a@t.ru', 'v2_clientId': 'WZ01112', 'v2_isAdmin': true });
|
||||
|
||||
t('админ без adminMode', { v2_user: {
|
||||
t('админ без adminMode', { user: {
|
||||
email: 'a@t.ru', clientId: 'WZ01112', activeClientId: 'WZ01112',
|
||||
allClientIds: ['WZ01112'], isAdmin: true, profiles: []
|
||||
} }, { 'v2_email': 'a@t.ru', 'v2_clientId': 'WZ01112', 'v2_isAdmin': false });
|
||||
|
||||
t('имперсонация', { v2_user: {
|
||||
t('имперсонация', { user: {
|
||||
email: 'admin@t.ru', clientId: 'WZ01112', activeClientId: 'WZ01112',
|
||||
allClientIds: ['WZ01112','WZ03709'], isAdmin: true, isImpersonated: true,
|
||||
originalUserEmail: 'real@t.ru', impersonatedCompanyId: 'WZ03709', profiles: []
|
||||
}, v2_adminMode: true }, { 'v2_email': 'real@t.ru', 'v2_clientId': 'WZ03709', 'v2_isImpersonated': true, 'v2_impersonatedBy': 'admin@t.ru' });
|
||||
}, adminMode: true }, { 'v2_email': 'real@t.ru', 'v2_clientId': 'WZ03709', 'v2_isImpersonated': true, 'v2_impersonatedBy': 'admin@t.ru' });
|
||||
|
||||
t('имперс. без origEmail', { v2_user: {
|
||||
t('имперс. без origEmail', { user: {
|
||||
email: 'x@t.ru', clientId: 'WZ01112', activeClientId: 'WZ01112',
|
||||
allClientIds: ['WZ01112'], isAdmin: true, isImpersonated: true,
|
||||
originalUserEmail: '', impersonatedCompanyId: '', profiles: []
|
||||
} }, { 'v2_email': 'x@t.ru', 'v2_clientId': 'WZ01112', 'v2_impersonatedBy': 'x@t.ru' });
|
||||
|
||||
t('без activeClientId', { v2_user: {
|
||||
t('без activeClientId', { user: {
|
||||
email: 'y@t.ru', clientId: 'WZ03709', allClientIds: ['WZ03709'],
|
||||
isAdmin: false, profiles: []
|
||||
} }, { 'v2_email': 'y@t.ru', 'v2_clientId': 'WZ03709' });
|
||||
|
||||
+12
-12
@@ -13,10 +13,10 @@ function createUserRouter() {
|
||||
// ── GET / — главная: список записей + форма ────────────────────────────
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const clId = req.v2_clientId;
|
||||
const allCompanies = req.v2_profiles.length > 0
|
||||
? req.v2_profiles
|
||||
: [{ client_id: clId, company_name: req.v2_companyName || clId }];
|
||||
const clId = req.clientId;
|
||||
const allCompanies = req.profiles.length > 0
|
||||
? req.profiles
|
||||
: [{ client_id: clId, company_name: req.companyName || clId }];
|
||||
|
||||
const company = await q.getOrCreateCompany(clId, allCompanies.find(c => c.client_id === clId)?.company_name || clId);
|
||||
const includeDeleted = req.query.deleted === '1';
|
||||
@@ -26,11 +26,11 @@ function createUserRouter() {
|
||||
|
||||
// Переключение компании
|
||||
if (req.query.switchTo && allCompanies.find(c => c.client_id === req.query.switchTo)) {
|
||||
req.session.v2_user.activeClientId = req.query.switchTo;
|
||||
req.session.user.activeClientId = req.query.switchTo;
|
||||
return res.redirect('/v2/app');
|
||||
}
|
||||
|
||||
res.send(renderPage({ entries, company, limit, used, allCompanies, clId, user: req.v2_user, includeDeleted }));
|
||||
res.send(renderPage({ entries, company, limit, used, allCompanies, clId, user: req.user, includeDeleted }));
|
||||
} catch (e) {
|
||||
res.status(500).send('<h2>Ошибка</h2><pre>' + e.message + '</pre><a href="/v2/app">Назад</a>');
|
||||
}
|
||||
@@ -39,9 +39,9 @@ function createUserRouter() {
|
||||
// ── POST /add ───────────────────────────────────────────────────────────
|
||||
router.post('/add', async (req, res) => {
|
||||
try {
|
||||
const clId = req.v2_clientId;
|
||||
const clId = req.clientId;
|
||||
const company = await q.getOrCreateCompany(clId, clId);
|
||||
const result = await q.createEntry(company.id, req.body.cidr || '', req.body.comment || '', req.v2_email, req.v2_impersonatedBy);
|
||||
const result = await q.createEntry(company.id, req.body.cidr || '', req.body.comment || '', req.email, req.impersonatedBy);
|
||||
const msg = result.wasNormalized ? 'Добавлено (адрес нормализован)' : 'Добавлено';
|
||||
res.redirect('/v2/app?msg=' + encodeURIComponent(msg));
|
||||
} catch (e) {
|
||||
@@ -52,9 +52,9 @@ function createUserRouter() {
|
||||
// ── POST /edit/:id ──────────────────────────────────────────────────────
|
||||
router.post('/edit/:id', async (req, res) => {
|
||||
try {
|
||||
const clId = req.v2_clientId;
|
||||
const clId = req.clientId;
|
||||
const company = await q.getOrCreateCompany(clId, clId);
|
||||
const result = await q.updateEntry(parseInt(req.params.id), company.id, req.body.cidr || '', req.body.comment || '', req.v2_email, req.v2_impersonatedBy);
|
||||
const result = await q.updateEntry(parseInt(req.params.id), company.id, req.body.cidr || '', req.body.comment || '', req.email, req.impersonatedBy);
|
||||
const msg = result.wasNormalized ? 'Изменено (адрес нормализован)' : 'Изменено';
|
||||
res.redirect('/v2/app?msg=' + encodeURIComponent(msg));
|
||||
} catch (e) {
|
||||
@@ -65,9 +65,9 @@ function createUserRouter() {
|
||||
// ── POST /delete/:id ────────────────────────────────────────────────────
|
||||
router.post('/delete/:id', async (req, res) => {
|
||||
try {
|
||||
const clId = req.v2_clientId;
|
||||
const clId = req.clientId;
|
||||
const company = await q.getOrCreateCompany(clId, clId);
|
||||
await q.deleteEntry(parseInt(req.params.id), company.id, req.v2_email, req.v2_impersonatedBy);
|
||||
await q.deleteEntry(parseInt(req.params.id), company.id, req.email, req.impersonatedBy);
|
||||
res.redirect('/v2/app?msg=Удалено');
|
||||
} catch (e) {
|
||||
res.redirect('/v2/app?error=' + encodeURIComponent(e.message));
|
||||
|
||||
Reference in New Issue
Block a user