v0.5.3: fix multi-company API isolation (client_id param)
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "ipwhitelist",
|
||||
"version": "0.5.2",
|
||||
"version": "0.5.3",
|
||||
"description": "IP WhiteList microservice for cloud provider",
|
||||
"main": "server.js",
|
||||
"scripts": {
|
||||
|
||||
@@ -37,6 +37,7 @@ function createEntriesRouter({ q }) {
|
||||
|
||||
// Определить компанию из запроса.
|
||||
// Admin с ?company=<id> → компания по ID.
|
||||
// User с ?client_id=<id> (мульти-компания) → компания по clientId.
|
||||
// Иначе → компания пользователя (UPSERT).
|
||||
async function resolveCompany(req) {
|
||||
if (req.user.isAdmin && req.query.company) {
|
||||
@@ -48,7 +49,9 @@ function createEntriesRouter({ q }) {
|
||||
if (!c) throw Object.assign(new Error('Company not found'), { status: 404 });
|
||||
return c;
|
||||
}
|
||||
return q.getOrCreateCompany(req.user.clientId, req.user.companyName);
|
||||
// Мульти-компания: если передан client_id — использовать его
|
||||
const effectiveClientId = req.query.client_id || req.user.clientId;
|
||||
return q.getOrCreateCompany(effectiveClientId, req.user.companyName);
|
||||
}
|
||||
|
||||
// GET /api/v1/entries — список записей + лимит
|
||||
|
||||
+10
-2
@@ -27,7 +27,12 @@ function createRouter() {
|
||||
// Суффикс ?company=<id> для API — только числовой ID или пусто
|
||||
function companyQuery(req) {
|
||||
const id = req.query.company || (req.body && req.body.company_id);
|
||||
return id ? '?company=' + id : '';
|
||||
if (id) return '?company=' + id;
|
||||
// Мульти-компания: передаём активный client_id
|
||||
if (!req.user.isAdmin && req.user.allClientIds && req.user.allClientIds.length > 1) {
|
||||
return '?client_id=' + encodeURIComponent(req.user.activeClientId || req.user.clientId);
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
// GET / — список записей
|
||||
@@ -69,7 +74,10 @@ function createRouter() {
|
||||
} else {
|
||||
// Обычный пользователь: запрашиваем записи для активной компании
|
||||
const activeClientId = req.user.activeClientId || req.user.clientId;
|
||||
const er = await api.get('/api/v1/entries', token);
|
||||
const clientIdParam = req.user.allClientIds && req.user.allClientIds.length > 1
|
||||
? '?client_id=' + encodeURIComponent(activeClientId)
|
||||
: '';
|
||||
const er = await api.get('/api/v1/entries' + clientIdParam, token);
|
||||
entries = er.data.entries || [];
|
||||
limit = er.data.limit || 15;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user