Use Tenant/User instances instead of IDs
MODIFY
src/authorization/DynamicAuthorizationDataSource.ts
src/authorization/DynamicAuthorizationDataSource.ts
import { DynamicAuthorizationDataSourceData, DynamicAuthorizationDataSourceName } from '../types/Authorization';
+import Tenant from '../types/Tenant';
+import UserToken from '../types/UserToken';
+
export default abstract class DynamicAuthorizationDataSource<T extends DynamicAuthorizationDataSourceData> {
- protected tenantID: string;
- protected userID: string;
+ protected tenant: Tenant;
+ protected userToken: UserToken;
private dataSourceData: T;
- public constructor(tenantID: string, userID: string) {
- this.tenantID = tenantID;
- this.userID = userID;
+ public constructor(tenant: Tenant, user: UserToken) {
+ this.tenant = tenant;
+ this.userToken = user;
}
public setData(dataSourceData: T): void {

MODIFY
src/authorization/DynamicAuthorizationFactory.ts
src/authorization/DynamicAuthorizationFactory.ts
@@ -4,25 +4,27 @@ import AssignedSitesCompaniesDynamicAuthorizationDataSource from './dynamic-data
import AssignedSitesCompaniesDynamicAuthorizationFilter from './dynamic-filters/AssignedSitesCompaniesDynamicAuthorizationFilter';
import DynamicAuthorizationDataSource from './DynamicAuthorizationDataSource';
import DynamicAuthorizationFilter from './DynamicAuthorizationFilter';
+import Tenant from '../types/Tenant';
+import UserToken from '../types/UserToken';
export default class DynamicAuthorizationFactory {
- public static async getDynamicFilter(tenantID: string, userID: string,
+ public static async getDynamicFilter(tenant: Tenant, userToken: UserToken,
filter: DynamicAuthorizationFilterName,
existingDataSources?: Map<DynamicAuthorizationDataSourceName, DynamicAuthorizationDataSource<DynamicAuthorizationDataSourceData>>):
Promise<DynamicAuthorizationFilter> {
let dynamicFilter: DynamicAuthorizationFilter;
switch (filter) {
case DynamicAuthorizationFilterName.ASSIGNED_SITES_COMPANIES:
- dynamicFilter = new AssignedSitesCompaniesDynamicAuthorizationFilter(tenantID, userID);
+ dynamicFilter = new AssignedSitesCompaniesDynamicAuthorizationFilter(tenant, userToken);
}
// Init Data Source
if (dynamicFilter) {
- await DynamicAuthorizationFactory.initFilterDataSources(tenantID, userID, dynamicFilter, existingDataSources);
+ await DynamicAuthorizationFactory.initFilterDataSources(tenant, userToken, dynamicFilter, existingDataSources);
}
return dynamicFilter;
}
- private static async initFilterDataSources(tenantID: string, userID: string,
+ private static async initFilterDataSources(tenant: Tenant, user: UserToken,
dynamicFilter: DynamicAuthorizationFilter,
existingDataSources?: Map<DynamicAuthorizationDataSourceName, DynamicAuthorizationDataSource<DynamicAuthorizationDataSourceData>>): Promise<void> {
// Get Data Source
@@ -32,7 +34,7 @@ export default class DynamicAuthorizationFactory {
if (!dataSource) {
// Create the data source
dataSource = DynamicAuthorizationFactory.getDynamicDataSource(
- tenantID, userID, dataSourceName);
+ tenant, user, dataSourceName);
// Load data
await dataSource.loadData();
// Add
@@ -43,11 +45,11 @@ export default class DynamicAuthorizationFactory {
}
}
- private static getDynamicDataSource(tenantID: string, userID: string,
+ private static getDynamicDataSource(tenant: Tenant, user: UserToken,
dataSource: DynamicAuthorizationDataSourceName): DynamicAuthorizationDataSource<DynamicAuthorizationDataSourceData> {
switch (dataSource) {
case DynamicAuthorizationDataSourceName.ASSIGNED_SITES_COMPANIES:
- return new AssignedSitesCompaniesDynamicAuthorizationDataSource(tenantID, userID);
+ return new AssignedSitesCompaniesDynamicAuthorizationDataSource(tenant, user);
}
}
}

MODIFY
src/authorization/DynamicAuthorizationFilter.ts
src/authorization/DynamicAuthorizationFilter.ts
import { AuthorizationFilter, DynamicAuthorizationDataSourceData, DynamicAuthorizationDataSourceName, Entity } from '../types/Authorization';
import DynamicAuthorizationDataSource from './DynamicAuthorizationDataSource';
+import Tenant from '../types/Tenant';
+import UserToken from '../types/UserToken';
export default abstract class DynamicAuthorizationFilter {
- protected tenantID: string;
- protected userID: string;
+ protected tenant: Tenant;
+ protected userToken: UserToken;
private dataSources: Map<DynamicAuthorizationDataSourceName, DynamicAuthorizationDataSource<DynamicAuthorizationDataSourceData>> = new Map();
- public constructor(tenantID: string, userID: string) {
- this.tenantID = tenantID;
- this.userID = userID;
+ public constructor(tenant: Tenant, user: UserToken) {
+ this.tenant = tenant;
+ this.userToken = user;
}
public getDataSources(): DynamicAuthorizationDataSource<DynamicAuthorizationDataSourceData>[] {

MODIFY
src/authorization/dynamic-data-source/AssignedSitesCompaniesDynamicAuthorizationDataSource.ts
src/authorization/dynamic-data-source/AssignedSitesCompaniesDynamicAuthorizationDataSource.ts
@@ -22,9 +22,9 @@ export default class AssignedSitesCompaniesDynamicAuthorizationDataSource
private async getAssignedSitesCompanyIDs(): Promise<string[]> {
// Get the Company IDs of the assigned Sites
- const sites = await SiteStorage.getSites(this.tenantID,
+ const sites = await SiteStorage.getSites(this.tenant.id,
{
- userID: this.userID,
+ userID: this.userToken.id,
issuer: true,
}, Constants.DB_PARAMS_MAX_LIMIT,
['companyID']

MODIFY
src/server/rest/v1/service/AuthorizationService.ts
src/server/rest/v1/service/AuthorizationService.ts
@@ -861,7 +861,7 @@ export default class AuthorizationService {
// Reset to false
authorizationFilters.authorized = false;
// Get the filter
- const dynamicFilter = await DynamicAuthorizationFactory.getDynamicFilter(tenant.id, userToken.id, filter, authorizationFilters.dataSources);
+ const dynamicFilter = await DynamicAuthorizationFactory.getDynamicFilter(tenant, userToken, filter, authorizationFilters.dataSources);
if (!dynamicFilter) {
// Filter not found -> Not authorized (all auth filter MUST work)
throw new AppAuthError({

Use Tenant/User instances instead of IDs