Use strong typing.
MODIFY
src/server/rest/v1/service/AuthorizationService.ts
src/server/rest/v1/service/AuthorizationService.ts
import { Action, AuthorizationActions, AuthorizationContext, AuthorizationFilter, Entity, SiteAreaAuthorizationActions } from '../../../../types/Authorization';
import { CompanyDataResult, SiteAreaDataResult, SiteDataResult } from '../../../../types/DataResult';
-import { HttpAssignAssetsToSiteAreaRequest, HttpSiteAreasRequest } from '../../../../types/requests/HttpSiteAreaRequest';
+import { HttpAssignAssetsToSiteAreaRequest, HttpSiteAreaRequest, HttpSiteAreasRequest } from '../../../../types/requests/HttpSiteAreaRequest';
+import { HttpChargingStationRequest, HttpChargingStationsRequest } from '../../../../types/requests/HttpChargingStationRequest';
import { HttpCompaniesRequest, HttpCompanyRequest } from '../../../../types/requests/HttpCompanyRequest';
-import { HttpSiteAssignUsersRequest, HttpSiteRequest, HttpSiteUsersRequest } from '../../../../types/requests/HttpSiteRequest';
+import { HttpSiteAssignUsersRequest, HttpSiteRequest, HttpSiteUsersRequest, HttpSitesRequest } from '../../../../types/requests/HttpSiteRequest';
import { HttpTagsRequest, HttpUserAssignSitesRequest, HttpUserRequest, HttpUserSitesRequest, HttpUsersRequest } from '../../../../types/requests/HttpUserRequest';
import User, { UserRole } from '../../../../types/User';
@@ -14,8 +15,8 @@ import Company from '../../../../types/Company';
import Constants from '../../../../utils/Constants';
import DynamicAuthorizationFactory from '../../../../authorization/DynamicAuthorizationFactory';
import { HTTPAuthError } from '../../../../types/HTTPError';
+import { HttpAssetsRequest } from '../../../../types/requests/HttpAssetRequest';
import HttpByIDRequest from '../../../../types/requests/HttpByIDRequest';
-import { HttpChargingStationRequest } from '../../../../types/requests/HttpChargingStationRequest';
import { ServerAction } from '../../../../types/Server';
import Site from '../../../../types/Site';
import SiteArea from '../../../../types/SiteArea';
@@ -86,7 +87,7 @@ export default class AuthorizationService {
}
public static async addSitesAuthorizations(tenant: Tenant, userToken: UserToken, sites: SiteDataResult, authorizationFilter: AuthorizationFilter,
- filteredRequest: Record<string, any>): Promise<void> {
+ filteredRequest: HttpSitesRequest): Promise<void> {
// Add canCreate flag to root
sites.canCreate = await AuthorizationService.canPerformAuthorizationAction(tenant, userToken, Entity.SITE, Action.CREATE, authorizationFilter);
@@ -97,7 +98,7 @@ export default class AuthorizationService {
}
public static async addSiteAuthorizations(tenant: Tenant, userToken: UserToken, site: Site, authorizationFilter: AuthorizationFilter,
- filteredRequest: Record<string, any>): Promise<void> {
+ filteredRequest: Partial<HttpSitesRequest>): Promise<void> {
// Enrich
if (!site.issuer) {
site.canRead = true;
@@ -132,7 +133,7 @@ export default class AuthorizationService {
}
public static async checkAndGetSiteUsersAuthorizationFilters(tenant: Tenant, userToken: UserToken,
- filteredRequest: Record<string, any>): Promise<AuthorizationFilter> {
+ filteredRequest: HttpSiteUsersRequest): Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
dataSources: new Map(),
@@ -336,8 +337,8 @@ export default class AuthorizationService {
}
}
- public static async checkAndGetUsersAuthorizationFilters(
- tenant: Tenant, userToken: UserToken, filteredRequest: HttpUsersRequest): Promise<AuthorizationFilter> {
+ public static async checkAndGetUsersAuthorizationFilters(tenant: Tenant, userToken: UserToken,
+ filteredRequest: HttpUsersRequest): Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
dataSources: new Map(),
@@ -357,8 +358,8 @@ export default class AuthorizationService {
return authorizationFilters;
}
- public static async checkAndGetAssetsAuthorizationFilters(
- tenant: Tenant, userToken: UserToken, filteredRequest: Record<string, any>): Promise<AuthorizationFilter> {
+ public static async checkAndGetAssetsAuthorizationFilters(tenant: Tenant, userToken: UserToken,
+ filteredRequest?: HttpAssetsRequest): Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
dataSources: new Map(),
@@ -371,8 +372,8 @@ export default class AuthorizationService {
return authorizationFilters;
}
- public static async checkAndGetUserAuthorizationFilters(
- tenant: Tenant, userToken: UserToken, filteredRequest: HttpUserRequest): Promise<AuthorizationFilter> {
+ public static async checkAndGetUserAuthorizationFilters(tenant: Tenant, userToken: UserToken,
+ filteredRequest: HttpUserRequest): Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
dataSources: new Map(),
@@ -401,8 +402,8 @@ export default class AuthorizationService {
return authorizationFilters;
}
- public static async checkAndGetTagsAuthorizationFilters(
- tenant: Tenant, userToken: UserToken, filteredRequest: HttpTagsRequest): Promise<AuthorizationFilter> {
+ public static async checkAndGetTagsAuthorizationFilters(tenant: Tenant, userToken: UserToken,
+ filteredRequest: HttpTagsRequest): Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
dataSources: new Map(),
@@ -425,8 +426,8 @@ export default class AuthorizationService {
return authorizationFilters;
}
- public static async checkAndGetTagAuthorizationFilters(
- tenant: Tenant, userToken: UserToken, filteredRequest: HttpByIDRequest): Promise<AuthorizationFilter> {
+ public static async checkAndGetTagAuthorizationFilters(tenant: Tenant, userToken: UserToken,
+ filteredRequest: HttpByIDRequest): Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
dataSources: new Map(),
@@ -509,7 +510,7 @@ export default class AuthorizationService {
return authorizationFilters;
}
- public static async checkAndGetSiteAreaAuthorizationFilters(tenant: Tenant, userToken: UserToken, filteredRequest: Record<string, any>,
+ public static async checkAndGetSiteAreaAuthorizationFilters(tenant: Tenant, userToken: UserToken, filteredRequest: Partial<SiteArea>,
action: Action): Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
@@ -517,12 +518,13 @@ export default class AuthorizationService {
projectFields: [],
authorized: false,
};
-
- await this.canPerformAuthorizationAction(tenant, userToken, Entity.SITE_AREA, action, authorizationFilters, filteredRequest);
+ await this.canPerformAuthorizationAction(tenant, userToken, Entity.SITE_AREA, action,
+ authorizationFilters, filteredRequest);
return authorizationFilters;
}
- public static async checkAndGetSiteAreasAuthorizationFilters(tenant: Tenant, userToken: UserToken, filteredRequest: HttpSiteAreasRequest): Promise<AuthorizationFilter> {
+ public static async checkAndGetSiteAreasAuthorizationFilters(tenant: Tenant, userToken: UserToken,
+ filteredRequest: HttpSiteAreasRequest): Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
dataSources: new Map(),
@@ -530,13 +532,13 @@ export default class AuthorizationService {
authorized: false,
};
// Check static & dynamic authorization
- await this.canPerformAuthorizationAction(
- tenant, userToken, Entity.SITE_AREAS, Action.LIST, authorizationFilters, filteredRequest);
+ await this.canPerformAuthorizationAction(tenant, userToken, Entity.SITE_AREAS, Action.LIST,
+ authorizationFilters, filteredRequest);
return authorizationFilters;
}
- public static async addSiteAreasAuthorizations(tenant: Tenant, userToken: UserToken, siteAreas: SiteAreaDataResult, authorizationFilter: AuthorizationFilter,
- filteredRequest: Record<string, any>): Promise<void> {
+ public static async addSiteAreasAuthorizations(tenant: Tenant, userToken: UserToken, siteAreas: SiteAreaDataResult,
+ authorizationFilter: AuthorizationFilter, filteredRequest: HttpSiteAreasRequest): Promise<void> {
// Add canCreate flag to root
siteAreas.canCreate = await AuthorizationService.canPerformAuthorizationAction(tenant, userToken, Entity.SITE_AREA, Action.CREATE, authorizationFilter);
@@ -547,26 +549,32 @@ export default class AuthorizationService {
}
public static async addSiteAreaAuthorizations(tenant: Tenant, userToken: UserToken, siteArea: SiteArea, authorizationFilter: AuthorizationFilter,
- filteredRequest: Record<string, any>): Promise<void> {
+ filteredRequest: HttpSiteAreasRequest | HttpSiteAreaRequest): Promise<void> {
// Enrich
if (!siteArea.issuer) {
siteArea.canRead = true;
siteArea.canUpdate = false;
siteArea.canDelete = false;
+ siteArea.canAssignAssets = false;
+ siteArea.canUnassignAssets = false;
+ siteArea.canAssignChargingStations = false;
+ siteArea.canUnassignChargingStations = false;
} else {
- filteredRequest.SiteAreaID = siteArea.id;
- filteredRequest.SiteID = siteArea.siteID;
- siteArea.canRead = await AuthorizationService.canPerformAuthorizationAction(tenant, userToken, Entity.SITE_AREA, Action.READ, authorizationFilter, filteredRequest);
- siteArea.canUpdate = await AuthorizationService.canPerformAuthorizationAction(tenant, userToken, Entity.SITE_AREA, Action.DELETE, authorizationFilter, filteredRequest);
- siteArea.canDelete = await AuthorizationService.canPerformAuthorizationAction(tenant, userToken, Entity.SITE_AREA, Action.UPDATE, authorizationFilter, filteredRequest);
+ // Downcast & enhance filters with values needed in dynamic filters
+ const enhancedFilters: Record<string, any> = filteredRequest;
+ enhancedFilters.SiteAreaID = siteArea.id;
+ enhancedFilters.SiteID = siteArea.siteID;
+ siteArea.canRead = await AuthorizationService.canPerformAuthorizationAction(tenant, userToken, Entity.SITE_AREA, Action.READ, authorizationFilter, enhancedFilters);
+ siteArea.canUpdate = await AuthorizationService.canPerformAuthorizationAction(tenant, userToken, Entity.SITE_AREA, Action.DELETE, authorizationFilter, enhancedFilters);
+ siteArea.canDelete = await AuthorizationService.canPerformAuthorizationAction(tenant, userToken, Entity.SITE_AREA, Action.UPDATE, authorizationFilter, enhancedFilters);
siteArea.canAssignAssets = await AuthorizationService.canPerformAuthorizationAction(tenant, userToken, Entity.SITE_AREA,
- Action.ASSIGN_ASSETS, authorizationFilter, filteredRequest);
+ Action.ASSIGN_ASSETS, authorizationFilter, enhancedFilters);
siteArea.canUnassignAssets = await AuthorizationService.canPerformAuthorizationAction(tenant, userToken, Entity.SITE_AREA,
- Action.UNASSIGN_ASSETS, authorizationFilter, filteredRequest);
+ Action.UNASSIGN_ASSETS, authorizationFilter, enhancedFilters);
siteArea.canAssignChargingStations = await AuthorizationService.canPerformAuthorizationAction(tenant, userToken, Entity.SITE_AREA,
- Action.ASSIGN_CHARGING_STATIONS, authorizationFilter, filteredRequest);
+ Action.ASSIGN_CHARGING_STATIONS, authorizationFilter, enhancedFilters);
siteArea.canUnassignChargingStations = await AuthorizationService.canPerformAuthorizationAction(tenant, userToken, Entity.SITE_AREA,
- Action.UNASSIGN_CHARGING_STATIONS, authorizationFilter, filteredRequest);
+ Action.UNASSIGN_CHARGING_STATIONS, authorizationFilter, enhancedFilters);
}
}
@@ -598,7 +606,7 @@ export default class AuthorizationService {
}
public static async checkAndGetChargingStationsAuthorizationFilters(tenant: Tenant, userToken: UserToken,
- filteredRequest: Record<string,any>):Promise<AuthorizationFilter> {
+ filteredRequest?: HttpChargingStationsRequest):Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
dataSources: new Map(),

MODIFY
src/server/rest/v1/service/SiteAreaService.ts
src/server/rest/v1/service/SiteAreaService.ts
@@ -213,8 +213,8 @@ export default class SiteAreaService {
authorizationSiteAreasFilter.projectFields
);
// Add Auth flags
- await AuthorizationService.addSiteAreasAuthorizations(
- req.tenant, req.user, siteAreas as SiteAreaDataResult,authorizationSiteAreasFilter, filteredRequest);
+ await AuthorizationService.addSiteAreasAuthorizations(req.tenant, req.user, siteAreas as SiteAreaDataResult,
+ authorizationSiteAreasFilter, filteredRequest);
// Return
res.json(siteAreas);
next();
@@ -273,8 +273,8 @@ export default class SiteAreaService {
// Check request data is valid
UtilsService.checkIfSiteAreaValid(filteredRequest, req);
// Check auth
- const authorizationFilters = await AuthorizationService.checkAndGetSiteAreaAuthorizationFilters(
- req.tenant, req.user, filteredRequest, Action.CREATE);
+ const authorizationFilters = await AuthorizationService.checkAndGetSiteAreaAuthorizationFilters(req.tenant, req.user,
+ filteredRequest, Action.CREATE);
if (!authorizationFilters.authorized) {
throw new AppAuthError({
errorCode: HTTPAuthError.FORBIDDEN,
@@ -284,8 +284,8 @@ export default class SiteAreaService {
});
}
// Check Site auth
- await UtilsService.checkAndGetSiteAuthorization(
- req.tenant, req.user, filteredRequest.siteID, Action.READ, action, {});
+ await UtilsService.checkAndGetSiteAuthorization(req.tenant, req.user, filteredRequest.siteID, Action.READ,
+ action, {});
// Create Site Area
const newSiteArea: SiteArea = {
...filteredRequest,

MODIFY
src/server/rest/v1/service/UtilsService.ts
src/server/rest/v1/service/UtilsService.ts
@@ -108,7 +108,7 @@ export default class UtilsService {
public static async checkAndGetChargingStationsAuthorization(tenant: Tenant, userToken: UserToken, action: ServerAction,
additionalFilters: Record<string, any>, applyProjectFields = false): Promise<ChargingStation[]> {
// Check dynamic auth
- const authorizationFilter = await AuthorizationService.checkAndGetChargingStationsAuthorizationFilters(tenant, userToken, {});
+ const authorizationFilter = await AuthorizationService.checkAndGetChargingStationsAuthorizationFilters(tenant, userToken);
if (!authorizationFilter.authorized) {
throw new AppAuthError({
errorCode: HTTPAuthError.FORBIDDEN,
@@ -132,7 +132,7 @@ export default class UtilsService {
public static async checkAndGetAssetsAuthorization(tenant: Tenant, userToken: UserToken,action: ServerAction,
additionalFilters: Record<string, any>, applyProjectFields = false):Promise<Asset[]> {
// Check dynamic auth
- const authorizationFilter = await AuthorizationService.checkAndGetAssetsAuthorizationFilters(tenant, userToken, {});
+ const authorizationFilter = await AuthorizationService.checkAndGetAssetsAuthorizationFilters(tenant, userToken);
if (!authorizationFilter.authorized) {
throw new AppAuthError({
errorCode: HTTPAuthError.FORBIDDEN,
@@ -464,8 +464,8 @@ export default class UtilsService {
// Check mandatory fields
UtilsService.assertIdIsProvided(action, siteAreaID, MODULE_NAME, 'checkAndGetSiteAreaAuthorization', userToken);
// Get dynamic auth
- const authorizationFilter = await AuthorizationService.checkAndGetSiteAreaAuthorizationFilters(
- tenant, userToken, { ID: siteAreaID }, Action.READ);
+ const authorizationFilter = await AuthorizationService.checkAndGetSiteAreaAuthorizationFilters(tenant, userToken,
+ { id: siteAreaID }, Action.READ);
if (!authorizationFilter.authorized) {
throw new AppAuthError({
errorCode: HTTPAuthError.FORBIDDEN,

Use strong typing.