Optimized Tenants
MODIFY
src/server/rest/v1/service/TenantService.ts
src/server/rest/v1/service/TenantService.ts
@@ -112,14 +112,13 @@ export default class TenantService {
});
}
// Get it
- const tenant = await TenantStorage.getTenant(tenantID);
+ const tenant = await TenantStorage.getTenant(tenantID,
+ [ 'id', 'name', 'email', 'subdomain', 'components', 'address', 'logo']
+ );
UtilsService.assertObjectExists(action, tenant, `Tenant with ID '${tenantID}' does not exist`,
MODULE_NAME, 'handleGetTenant', req.user);
// Return
- res.json(
- // Filter
- TenantSecurity.filterTenantResponse(tenant, req.user)
- );
+ res.json(tenant);
next();
}
@@ -135,7 +134,9 @@ export default class TenantService {
}
// Filter
const filteredRequest = TenantSecurity.filterTenantsRequest(req.query);
- const projectFields = ['id', 'name', 'email', 'subdomain', 'logo', 'createdOn', 'createdBy', 'lastChangedOn', 'lastChangedBy'];
+ const projectFields = [
+ 'id', 'name', 'email', 'subdomain', 'logo', 'createdOn', 'createdBy', 'lastChangedOn', 'lastChangedBy'
+ ];
if (filteredRequest.WithComponents) {
projectFields.push('components');
}
@@ -147,8 +148,6 @@ export default class TenantService {
},
{ limit: filteredRequest.Limit, skip: filteredRequest.Skip, sort: filteredRequest.Sort },
projectFields);
- // Filter
- TenantSecurity.filterTenantsResponse(tenants, req.user);
// Return
res.json(tenants);
next();

MODIFY
src/server/rest/v1/service/security/TenantSecurity.ts
src/server/rest/v1/service/security/TenantSecurity.ts
import { HttpTenantVerifyRequest, HttpTenantsRequest } from '../../../../../types/requests/HttpTenantRequest';
-import Authorizations from '../../../../../authorization/Authorizations';
-import { DataResult } from '../../../../../types/DataResult';
-import Tenant from '../../../../../types/Tenant';
-import UserToken from '../../../../../types/UserToken';
import UtilsSecurity from './UtilsSecurity';
import sanitize from 'mongo-sanitize';
@@ -25,47 +21,5 @@ export default class TenantSecurity {
UtilsSecurity.filterSort(request, filteredRequest);
return filteredRequest;
}
-
- static filterTenantResponse(tenant: Tenant, loggedUser: UserToken): Tenant {
- let filteredTenant: Tenant;
- if (!tenant) {
- return null;
- }
- // Check auth
- if (Authorizations.canReadTenant(loggedUser)) {
- // Set only necessary info
- filteredTenant = {} as Tenant;
- filteredTenant.id = tenant.id;
- filteredTenant.name = tenant.name;
- filteredTenant.email = tenant.email;
- filteredTenant.subdomain = tenant.subdomain;
- filteredTenant.components = tenant.components;
- filteredTenant.address = tenant.address;
- filteredTenant.logo = tenant.logo;
- // Created By / Last Changed By
- UtilsSecurity.filterCreatedAndLastChanged(
- filteredTenant, tenant, loggedUser);
- }
- return filteredTenant;
- }
-
- static filterTenantsResponse(tenants: DataResult<Tenant>, loggedUser: UserToken): void {
- const filteredTenants: Tenant[] = [];
- if (!tenants.result) {
- return null;
- }
- if (!Authorizations.canListTenants(loggedUser)) {
- return null;
- }
- for (const tenant of tenants.result) {
- // Filter
- const filteredTenant = TenantSecurity.filterTenantResponse(tenant, loggedUser);
- // Add
- if (filteredTenant) {
- filteredTenants.push(filteredTenant);
- }
- }
- tenants.result = filteredTenants;
- }
}

MODIFY
src/storage/mongodb/TenantStorage.ts
src/storage/mongodb/TenantStorage.ts
@@ -25,14 +25,14 @@ export default class TenantStorage {
}
}
- public static async getTenant(id: string = Constants.UNKNOWN_OBJECT_ID): Promise<Tenant> {
+ public static async getTenant(id: string = Constants.UNKNOWN_OBJECT_ID, projectFields?: string[]): Promise<Tenant> {
// Check in cache
const tenant = TenantStorage.tenants.get(id);
if (!tenant) {
// Call DB
const tenantsMDB = await TenantStorage.getTenants({
tenantIDs: [id]
- }, Constants.DB_PARAMS_SINGLE_RECORD);
+ }, Constants.DB_PARAMS_SINGLE_RECORD, projectFields);
// Add in cache
if (tenantsMDB.count > 0) {
TenantStorage.tenants.set(id, tenantsMDB.result[0]);

Optimized Tenants