Check charging stations based on Site IDs
MODIFY
src/server/rest/v1/service/AuthorizationService.ts
src/server/rest/v1/service/AuthorizationService.ts
@@ -8,10 +8,10 @@ import User, { UserRole } from '../../../../types/User';
import AppAuthError from '../../../../exception/AppAuthError';
import Authorizations from '../../../../authorization/Authorizations';
-import ChargingStationStorage from '../../../../storage/mongodb/ChargingStationStorage';
import Constants from '../../../../utils/Constants';
import { HTTPAuthError } from '../../../../types/HTTPError';
import HttpByIDRequest from '../../../../types/requests/HttpByIDRequest';
+import { HttpChargingStationRequest } from '../../../../types/requests/HttpChargingStationRequest';
import { ServerAction } from '../../../../types/Server';
import SiteAreaStorage from '../../../../storage/mongodb/SiteAreaStorage';
import SiteStorage from '../../../../storage/mongodb/SiteStorage';
@@ -609,7 +609,8 @@ export default class AuthorizationService {
}
}
- public static async checkAndGetChargingStationAuthorizationFilters(tenant: Tenant, userToken: UserToken, filteredRequest: HttpSiteAreaRequest):Promise<AuthorizationFilter> {
+ public static async checkAndGetChargingStationAuthorizationFilters(tenant: Tenant, userToken: UserToken,
+ filteredRequest: HttpChargingStationRequest):Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
projectFields: [
@@ -624,20 +625,18 @@ export default class AuthorizationService {
};
// Check Projection
if (!Utils.isEmptyArray(filteredRequest.ProjectFields)) {
- authorizationFilters.projectFields = authorizationFilters.projectFields.filter((projectField) => filteredRequest.ProjectFields.includes(projectField));
+ authorizationFilters.projectFields = authorizationFilters.projectFields.filter(
+ (projectField) => filteredRequest.ProjectFields.includes(projectField));
}
// Not an Admin?
if (userToken.role !== UserRole.ADMIN) {
- // Get assigned ChargingStations IDs
- const chargingStationIDs = await AuthorizationService.getAssignedChargingStationIDs(tenant.id, userToken);
- if (Utils.isEmptyArray(chargingStationIDs) || !chargingStationIDs.includes(filteredRequest.ID)) {
- throw new AppAuthError({
- errorCode: HTTPAuthError.FORBIDDEN,
- user: userToken,
- action: Action.READ, entity: Entity.SITE_AREA,
- module: MODULE_NAME, method: 'checkAndGetSiteAreaAuthorizationFilters',
- });
- } else {
+ // Get assigned Site IDs assigned to user from DB
+ const siteIDs = await AuthorizationService.getAssignedSiteIDs(tenant.id, userToken);
+ if (!Utils.isEmptyArray(siteIDs)) {
+ // Force the filter
+ authorizationFilters.filters.siteIDs = siteIDs;
+ }
+ if (!Utils.isEmptyArray(authorizationFilters.filters.siteIDs)) {
authorizationFilters.authorized = true;
}
}
@@ -771,15 +770,4 @@ export default class AuthorizationService {
const siteArea = await SiteAreaStorage.getSiteArea(tenantID, siteAreaID);
return siteArea.siteID;
}
-
- private static async getAssignedChargingStationIDs(tenantID: string, userToken: UserToken, siteID?: string) {
- // Get the ChargingStation IDs from sites assigned to the user
- const chargingStations = await ChargingStationStorage.getChargingStations(tenantID, {
- siteIDs: Authorizations.getAuthorizedSiteIDs(userToken, siteID ? [siteID] : null),
- issuer: true,
- }, Constants.DB_PARAMS_MAX_LIMIT,
- ['id']);
-
- return _.uniq(_.map(chargingStations.result, 'id'));
- }
}

MODIFY
src/server/rest/v1/service/ChargingStationService.ts
src/server/rest/v1/service/ChargingStationService.ts
@@ -980,7 +980,14 @@ export default class ChargingStationService {
});
}
// check dynamic auth
- const chargingStationAuthorizationFilters = await AuthorizationService.checkAndGetChargingStationAuthorizationFilters(req.tenant, req.user, filteredRequest);
+ const chargingStationAuthorizationFilters =
+ await AuthorizationService.checkAndGetChargingStationAuthorizationFilters(req.tenant, req.user, filteredRequest);
+ if (!chargingStationAuthorizationFilters.authorized) {
+ // Force the failure
+ UtilsService.assertObjectExists(action, null, `Charging Station '${filteredRequest.ID}' does not exist`,
+ MODULE_NAME, 'handleGetChargingStation', req.user);
+ return;
+ }
// Query charging station
const chargingStation = await ChargingStationStorage.getChargingStation(
req.user.tenantID, filteredRequest.ID, chargingStationAuthorizationFilters.filters, chargingStationAuthorizationFilters.projectFields);
@@ -988,7 +995,7 @@ export default class ChargingStationService {
UtilsService.assertObjectExists(action, chargingStation, `Charging Station '${filteredRequest.ID}' does not exist`,
MODULE_NAME, 'handleGetChargingStation', req.user);
// Deleted?
- if (chargingStation.deleted) {
+ if (chargingStation?.deleted) {
throw new AppError({
source: Constants.CENTRAL_SERVER,
errorCode: HTTPError.OBJECT_DOES_NOT_EXIST_ERROR,

MODIFY
src/server/rest/v1/validator/ChargingStationValidator.ts
src/server/rest/v1/validator/ChargingStationValidator.ts
-import { HttpChargingStationCommandRequest, HttpChargingStationConnectorRequest, HttpChargingStationOcppRequest, HttpChargingStationsRequest, HttpDownloadQrCodeRequest } from '../../../../types/requests/HttpChargingStationRequest';
+import { HttpChargingStationCommandRequest, HttpChargingStationConnectorRequest, HttpChargingStationOcppRequest, HttpChargingStationRequest, HttpChargingStationsRequest, HttpDownloadQrCodeRequest } from '../../../../types/requests/HttpChargingStationRequest';
import { ChargingProfile } from '../../../../types/ChargingProfile';
import HttpByIDRequest from '../../../../types/requests/HttpByIDRequest';
@@ -44,7 +44,7 @@ export default class ChargingStationValidator extends SchemaValidator {
return data;
}
- public validateChargingStationGetReq(data: any): HttpByIDRequest {
+ public validateChargingStationGetReq(data: any): HttpChargingStationRequest {
// Validate schema
this.validate(this.chargingStationGet, data);
return data;

MODIFY
src/storage/mongodb/ChargingStationStorage.ts
src/storage/mongodb/ChargingStationStorage.ts
@@ -133,12 +133,13 @@ export default class ChargingStationStorage {
}
public static async getChargingStation(tenantID: string, id: string = Constants.UNKNOWN_STRING_ID,
- params: { includeDeleted?: boolean, issuer?: boolean; } = {}, projectFields?: string[]): Promise<ChargingStation> {
+ params: { includeDeleted?: boolean, issuer?: boolean; siteIDs?: string[] } = {}, projectFields?: string[]): Promise<ChargingStation> {
const chargingStationsMDB = await ChargingStationStorage.getChargingStations(tenantID, {
chargingStationIDs: [id],
withSite: true,
includeDeleted: params.includeDeleted,
issuer: params.issuer,
+ siteIDs: params.siteIDs,
}, Constants.DB_PARAMS_SINGLE_RECORD, projectFields);
return chargingStationsMDB.count === 1 ? chargingStationsMDB.result[0] : null;
}

Check charging stations based on Site IDs