Add authorizations on the list.
MODIFY
src/server/rest/v1/service/AuthorizationService.ts
src/server/rest/v1/service/AuthorizationService.ts
import { Action, AuthorizationFilter, Entity } from '../../../../types/Authorization';
-import { CompanyDataResult, SiteDataResult } from '../../../../types/DataResult';
+import { CompanyDataResult, SiteAreaDataResult, SiteDataResult } from '../../../../types/DataResult';
import { HttpCompaniesRequest, HttpCompanyRequest } from '../../../../types/requests/HttpCompanyRequest';
import { HttpSiteAreaRequest, HttpSiteAreasRequest } from '../../../../types/requests/HttpSiteAreaRequest';
import { HttpSiteAssignUsersRequest, HttpSiteRequest, HttpSiteUsersRequest } from '../../../../types/requests/HttpSiteRequest';
@@ -12,7 +12,6 @@ import Constants from '../../../../utils/Constants';
import { HTTPAuthError } from '../../../../types/HTTPError';
import HttpByIDRequest from '../../../../types/requests/HttpByIDRequest';
import { ServerAction } from '../../../../types/Server';
-import Site from '../../../../types/Site';
import SiteAreaStorage from '../../../../storage/mongodb/SiteAreaStorage';
import SiteStorage from '../../../../storage/mongodb/SiteStorage';
import Tenant from '../../../../types/Tenant';
@@ -21,7 +20,6 @@ import UserStorage from '../../../../storage/mongodb/UserStorage';
import UserToken from '../../../../types/UserToken';
import Utils from '../../../../utils/Utils';
import _ from 'lodash';
-import { filter } from 'bluebird';
const MODULE_NAME = 'AuthorizationService';
@@ -585,6 +583,24 @@ export default class AuthorizationService {
return authorizationFilters;
}
+ public static async addSiteAreasAuthorizations(tenant: Tenant, userToken: UserToken, siteAreas: SiteAreaDataResult) {
+ // Add canCreate flag to root
+ siteAreas.canCreate = Authorizations.canCreateSite(userToken);
+ // Enrich
+ for (const siteArea of siteAreas.result) {
+ siteArea.canRead = Authorizations.canReadSite(userToken);
+ // update can be performed by admin or site admin
+ if (userToken.role === UserRole.ADMIN) {
+ siteArea.canDelete = true;
+ siteArea.canUpdate = true;
+ } else {
+ const canModify = await AuthorizationService.checkUpdateDeleteSiteAreaAuthorization(tenant, userToken, siteArea.id);
+ siteArea.canUpdate = Authorizations.canUpdateSiteArea(userToken) && canModify;
+ siteArea.canDelete = Authorizations.canDeleteSiteArea(userToken) && canModify;
+ }
+ }
+ }
+
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
@@ -18,6 +18,7 @@ import Logging from '../../../../utils/Logging';
import OCPPUtils from '../../../ocpp/utils/OCPPUtils';
import { ServerAction } from '../../../../types/Server';
import SiteArea from '../../../../types/SiteArea';
+import { SiteAreaDataResult } from '../../../../types/DataResult';
import SiteAreaSecurity from './security/SiteAreaSecurity';
import SiteAreaStorage from '../../../../storage/mongodb/SiteAreaStorage';
import SiteStorage from '../../../../storage/mongodb/SiteStorage';
@@ -405,9 +406,8 @@ export default class SiteAreaService {
},
readSiteAreasAuthorizationFilters.projectFields
);
-
- // todo: add for each! can Read/Update/Delete + global canCreate
-
+ // Add Auth flags
+ await AuthorizationService.addSiteAreasAuthorizations(req.tenant, req.user, siteAreas as SiteAreaDataResult);
// Return
res.json(siteAreas);
next();

MODIFY
src/types/DataResult.ts
src/types/DataResult.ts
import Company from './Company';
import Site from './Site';
+import SiteArea from './SiteArea';
import { Transaction } from '@google-cloud/firestore';
export interface DataResult<T> {
@@ -14,6 +15,10 @@ export interface SiteDataResult extends DataResult<Site>{
canCreate: boolean;
}
+export interface SiteAreaDataResult extends DataResult<SiteArea>{
+ canCreate: boolean;
+}
+
export interface TransactionDataResult {
count: number;
result: Transaction[];

MODIFY
src/types/SiteArea.ts
src/types/SiteArea.ts
import ChargingStation, { Voltage } from '../types/ChargingStation';
import Address from './Address';
+import { AuthorizationActions } from './Authorization';
import ConnectorStats from './ConnectorStats';
import Consumption from './Consumption';
import CreatedUpdatedProps from './CreatedUpdatedProps';
import Site from '../types/Site';
-export default interface SiteArea extends CreatedUpdatedProps {
+export default interface SiteArea extends CreatedUpdatedProps, AuthorizationActions {
id: string;
name: string;
issuer: boolean;

Add authorizations on the list.