Update GetSiteAreas logic
MODIFY
src/server/rest/v1/service/AuthorizationService.ts
src/server/rest/v1/service/AuthorizationService.ts
import { Action, AuthorizationFilter, Entity } from '../../../../types/Authorization';
import { HttpCompaniesRequest, HttpCompanyRequest } from '../../../../types/requests/HttpCompanyRequest';
+import { HttpSiteAreaRequest, HttpSiteAreasRequest } from '../../../../types/requests/HttpSiteAreaRequest';
import { HttpSiteAssignUsersRequest, HttpSiteRequest, HttpSiteUsersRequest } from '../../../../types/requests/HttpSiteRequest';
import { HttpTagsRequest, HttpUserAssignSitesRequest, HttpUserRequest, HttpUserSitesRequest, HttpUsersRequest } from '../../../../types/requests/HttpUserRequest';
import User, { UserRole } from '../../../../types/User';
@@ -10,7 +11,6 @@ import Company from '../../../../types/Company';
import Constants from '../../../../utils/Constants';
import { HTTPAuthError } from '../../../../types/HTTPError';
import HttpByIDRequest from '../../../../types/requests/HttpByIDRequest';
-import { HttpSiteAreaRequest } from '../../../../types/requests/HttpSiteAreaRequest';
import { ServerAction } from '../../../../types/Server';
import Site from '../../../../types/Site';
import SiteAreaStorage from '../../../../storage/mongodb/SiteAreaStorage';
@@ -520,6 +520,43 @@ export default class AuthorizationService {
return authorizationFilters;
}
+ public static async checkAndGetSiteAreasAuthorizationFilters(tenant: Tenant, userToken: UserToken, filteredRequest: HttpSiteAreasRequest): Promise<AuthorizationFilter> {
+ const authorizationFilters: AuthorizationFilter = {
+ filters: {},
+ projectFields: [
+ 'id', 'name', 'siteID', 'maximumPower', 'voltage', 'numberOfPhases', 'accessControl', 'smartCharging', 'address',
+ 'site.id', 'site.name', 'issuer', 'distanceMeters', 'createdOn', 'createdBy', 'lastChangedOn', 'lastChangedBy'
+ ],
+ authorized: userToken.role === UserRole.ADMIN,
+ };
+ // Check Projection
+ if (!Utils.isEmptyArray(filteredRequest.ProjectFields)) {
+ authorizationFilters.projectFields = authorizationFilters.projectFields.filter((projectField) => filteredRequest.ProjectFields.includes(projectField));
+ }
+ // Not an Admin?
+ if (userToken.role !== UserRole.ADMIN) {
+ // Get assigned SiteArea IDs
+ const siteAreaIDs = await AuthorizationService.getAssignedSiteAreaIDs(tenant.id, userToken);
+ if (!Utils.isEmptyArray(siteAreaIDs)) {
+ // Force the filter
+ authorizationFilters.filters.siteAreaIDs = siteAreaIDs;
+ // Check if filter is provided
+ if (Utils.objectHasProperty(filteredRequest, 'SiteAreaID') &&
+ !Utils.isNullOrUndefined(filteredRequest['SiteAreaID'])) {
+ const filteredSiteAreaIDs: string[] = filteredRequest['SiteAreaID'].split('|');
+ // Override
+ authorizationFilters.filters.siteAreaIDs = filteredSiteAreaIDs.filter(
+ (siteAreaID) => authorizationFilters.filters.siteAreaIDs.includes(siteAreaID));
+ }
+ }
+ if (!Utils.isEmptyArray(authorizationFilters.filters.siteAreaIDs)) {
+ authorizationFilters.authorized = true;
+ }
+ }
+
+ return authorizationFilters;
+ }
+
private static async getAssignedSitesCompanyIDs(tenantID: string, userToken: UserToken, siteID?: string): Promise<string[]> {
// Get the Company IDs of the assigned Sites
const sites = await SiteStorage.getSites(tenantID,

MODIFY
src/server/rest/v1/service/SiteAreaService.ts
src/server/rest/v1/service/SiteAreaService.ts
@@ -304,6 +304,7 @@ export default class SiteAreaService {
public static async handleGetSiteAreaImage(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
// Filter
const filteredRequest = SiteAreaSecurity.filterSiteImageRequest(req.query);
+ // Check mandatory fields
UtilsService.assertIdIsProvided(action, filteredRequest.ID, MODULE_NAME, 'handleGetSiteAreaImage', req.user);
// Get it
const siteAreaImage = await SiteAreaStorage.getSiteAreaImage(filteredRequest.TenantID, filteredRequest.ID);
@@ -329,7 +330,7 @@ export default class SiteAreaService {
// Check if component is active
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.ORGANIZATION,
Action.LIST, Entity.SITE_AREAS, MODULE_NAME, 'handleGetSiteAreas');
- // Check auth
+ // Check static auth
if (!Authorizations.canListSiteAreas(req.user)) {
throw new AppAuthError({
errorCode: HTTPAuthError.FORBIDDEN,
@@ -340,6 +341,8 @@ export default class SiteAreaService {
}
// Filter
const filteredRequest = SiteAreaSecurity.filterSiteAreasRequest(req.query);
+ // Check dynamic auth
+ const readSiteAreasAuthorizationFilters = await AuthorizationService.checkAndGetSiteAreasAuthorizationFilters(req.tenant, req.user, filteredRequest);
// Get the sites
const siteAreas = await SiteAreaStorage.getSiteAreas(req.user.tenantID,
{
@@ -348,15 +351,18 @@ export default class SiteAreaService {
withSite: filteredRequest.WithSite,
withChargingStations: filteredRequest.WithChargeBoxes,
withAvailableChargingStations: filteredRequest.WithAvailableChargers,
- siteIDs: Authorizations.getAuthorizedSiteIDs(req.user, filteredRequest.SiteID ? filteredRequest.SiteID.split('|') : null),
+ siteIDs: filteredRequest.SiteID ? filteredRequest.SiteID.split('|') : null,
locCoordinates: filteredRequest.LocCoordinates,
- locMaxDistanceMeters: filteredRequest.LocMaxDistanceMeters
+ locMaxDistanceMeters: filteredRequest.LocMaxDistanceMeters,
+ ...readSiteAreasAuthorizationFilters.filters
+ },
+ {
+ limit: filteredRequest.Limit,
+ skip: filteredRequest.Skip,
+ sort: filteredRequest.SortFields,
+ onlyRecordCount: filteredRequest.OnlyRecordCount
},
- { limit: filteredRequest.Limit, skip: filteredRequest.Skip, sort: filteredRequest.SortFields, onlyRecordCount: filteredRequest.OnlyRecordCount },
- [
- 'id', 'name', 'siteID', 'maximumPower', 'voltage', 'numberOfPhases', 'accessControl', 'smartCharging', 'address',
- 'site.id', 'site.name', 'issuer', 'distanceMeters', 'createdOn', 'createdBy', 'lastChangedOn', 'lastChangedBy'
- ]
+ readSiteAreasAuthorizationFilters.projectFields
);
// Return
res.json(siteAreas);

Update GetSiteAreas logic