Retrieve site ID from storage
Call storage to retrieve assigned site ID
Not rely on 'static 'user token
remove unnused method
MODIFY
src/authorization/Authorizations.ts
src/authorization/Authorizations.ts
@@ -4,6 +4,7 @@ import Tenant, { TenantComponents } from '../types/Tenant';
import User, { UserRole, UserStatus } from '../types/User';
import AuthorizationConfiguration from '../types/configuration/AuthorizationConfiguration';
+import AuthorizationService from '../server/rest/v1/service/AuthorizationService';
import AuthorizationsManager from './AuthorizationsManager';
import BackendError from '../exception/BackendError';
import ChargingStationStorage from '../storage/mongodb/ChargingStationStorage';
@@ -33,6 +34,7 @@ import TransactionStorage from '../storage/mongodb/TransactionStorage';
import UserStorage from '../storage/mongodb/UserStorage';
import UserToken from '../types/UserToken';
import Utils from '../utils/Utils';
+import _ from 'lodash';
import moment from 'moment';
const MODULE_NAME = 'Authorizations';
@@ -89,20 +91,21 @@ export default class Authorizations {
loggedUser, Entity.CHARGING_STATION, Action.REMOTE_STOP_TRANSACTION, context);
}
- public static getAuthorizedSiteIDs(loggedUser: UserToken, requestedSites: string[]): string[] {
+ public static async getAuthorizedSiteIDs(tenant : Tenant, loggedUser: UserToken, requestedSites: string[]): Promise<string[]> {
if (!Utils.isComponentActiveFromToken(loggedUser, TenantComponents.ORGANIZATION)) {
return null;
}
if (this.isAdmin(loggedUser)) {
return requestedSites;
}
+ const loggedUserAssignedSiteIDs = await AuthorizationService.getAssignedSiteIDs(tenant, loggedUser);
if (Utils.isEmptyArray(requestedSites)) {
- return loggedUser.sites.length > 0 ? loggedUser.sites : null;
+ return loggedUserAssignedSiteIDs.length > 0 ? loggedUserAssignedSiteIDs : null;
}
- return requestedSites.filter((site) => loggedUser.sites.includes(site));
+ return requestedSites.filter((site) => loggedUserAssignedSiteIDs.includes(site));
}
- public static getAuthorizedSiteAdminIDs(loggedUser: UserToken, requestedSites?: string[]): string[] {
+ public static async getAuthorizedSiteAdminIDs(tenant: Tenant, loggedUser: UserToken, requestedSites?: string[]): Promise<string[]> {
if (!Utils.isComponentActiveFromToken(loggedUser, TenantComponents.ORGANIZATION)) {
return null;
}
@@ -112,14 +115,13 @@ export default class Authorizations {
if (this.isAdmin(loggedUser)) {
return requestedSites;
}
- const sites: Set<string> = new Set(loggedUser.sitesAdmin);
- for (const siteID of loggedUser.sitesOwner) {
- sites.add(siteID);
- }
+ const siteAdminSiteIDs = await AuthorizationService.getSiteAdminSiteIDs(tenant, loggedUser);
+ const siteOwnerSiteIDs = await AuthorizationService.getSiteOwnerSiteIDs(tenant, loggedUser);
+ const sites = _.uniq([...siteAdminSiteIDs, ...siteOwnerSiteIDs]);
if (Utils.isEmptyArray(requestedSites)) {
- return [...sites];
+ return sites;
}
- return requestedSites.filter((site) => sites.has(site));
+ return requestedSites.filter((site) => sites.includes(site));
}
public static async buildUserToken(tenant: Tenant, user: User, tags: Tag[]): Promise<UserToken> {

MODIFY
src/server/rest/v1/service/AuthorizationService.ts
src/server/rest/v1/service/AuthorizationService.ts
@@ -680,7 +680,7 @@ export default class AuthorizationService {
return userSites.result.map((userSite) => userSite.siteID);
}
- private static async getSiteOwnerSiteIDs(tenant: Tenant, userToken: UserToken): Promise<string[]> {
+ public static async getSiteOwnerSiteIDs(tenant: Tenant, userToken: UserToken): Promise<string[]> {
// Get the Sites where the user is Site Owner
const userSites = await UserStorage.getUserSites(tenant,
{
@@ -692,7 +692,7 @@ export default class AuthorizationService {
return userSites.result.map((userSite) => userSite.siteID);
}
- private static async getAssignedSiteIDs(tenant: Tenant, userToken: UserToken): Promise<string[]> {
+ public static async getAssignedSiteIDs(tenant: Tenant, userToken: UserToken): Promise<string[]> {
// Get the Sites assigned to the User
const sites = await SiteStorage.getSites(tenant,
{
@@ -704,19 +704,6 @@ export default class AuthorizationService {
return sites.result.map((site) => site.id);
}
- private static async getAssignedAssetIDs(tenant: Tenant, siteID: string): Promise<string[]> {
- // Get the Assets assigned to the Site
- const assets = await AssetStorage.getAssets(tenant,
- {
- siteIDs: [siteID],
- // TODO: Uncomment when the bug will be fixed: https://github.com/sap-labs-france/ev-dashboard/issues/2266
- // issuer: true,
- }, Constants.DB_PARAMS_MAX_LIMIT,
- ['id']
- );
- return assets.result.map((asset) => asset.id);
- }
-
private static async checkAssignedSites(tenant: Tenant, userToken: UserToken,
filteredRequest: { SiteID?: string }, authorizationFilters: AuthorizationFilter): Promise<void> {
if (userToken.role !== UserRole.ADMIN && userToken.role !== UserRole.SUPER_ADMIN) {

MODIFY
src/server/rest/v1/service/ChargingStationService.ts
src/server/rest/v1/service/ChargingStationService.ts
@@ -531,7 +531,7 @@ export default class ChargingStationService {
connectorID: filteredRequest.ConnectorID,
withChargingStation: filteredRequest.WithChargingStation,
withSiteArea: filteredRequest.WithSiteArea,
- siteIDs: Authorizations.getAuthorizedSiteIDs(req.user, filteredRequest.SiteID ? filteredRequest.SiteID.split('|') : null),
+ siteIDs: await Authorizations.getAuthorizedSiteIDs(req.tenant, req.user, filteredRequest.SiteID ? filteredRequest.SiteID.split('|') : null),
},
{
limit: filteredRequest.Limit,
@@ -1043,7 +1043,7 @@ export default class ChargingStationService {
const chargingStations = await ChargingStationStorage.getChargingStationsInError(req.tenant,
{
search: filteredRequest.Search,
- siteIDs: Authorizations.getAuthorizedSiteIDs(req.user, filteredRequest.SiteID ? filteredRequest.SiteID.split('|') : null),
+ siteIDs: await Authorizations.getAuthorizedSiteIDs(req.tenant, req.user, filteredRequest.SiteID ? filteredRequest.SiteID.split('|') : null),
siteAreaIDs: (filteredRequest.SiteAreaID ? filteredRequest.SiteAreaID.split('|') : null),
errorType
},
@@ -1444,7 +1444,7 @@ export default class ChargingStationService {
if (!Utils.isEmptyArray(httpProjectFields)) {
projectFields = projectFields.filter((projectField) => httpProjectFields.includes(projectField));
}
- const siteIDs = Authorizations.getAuthorizedSiteIDs(req.user, filteredRequest.SiteID ? filteredRequest.SiteID.split('|') : null);
+ const siteIDs = await Authorizations.getAuthorizedSiteIDs(req.tenant, req.user, filteredRequest.SiteID ? filteredRequest.SiteID.split('|') : null);
// todo: write this in a nicer way when refactoring the whole auth concept for ChargingStation
if (Utils.isEmptyArray(siteIDs) && !Authorizations.isAdmin(req.user)) {
return { count: 0, result: [] };

MODIFY
src/server/rest/v1/service/TransactionService.ts
src/server/rest/v1/service/TransactionService.ts
@@ -678,9 +678,9 @@ export default class TransactionService {
filter.siteAreaIDs = filteredRequest.SiteAreaID.split('|');
}
if (filteredRequest.SiteID) {
- filter.siteID = Authorizations.getAuthorizedSiteAdminIDs(req.user, filteredRequest.SiteID.split('|'));
+ filter.siteID = await Authorizations.getAuthorizedSiteAdminIDs(req.tenant, req.user, filteredRequest.SiteID.split('|'));
}
- filter.siteAdminIDs = Authorizations.getAuthorizedSiteAdminIDs(req.user);
+ filter.siteAdminIDs = await Authorizations.getAuthorizedSiteAdminIDs(req.tenant, req.user);
}
// Get Reports
const reports = await TransactionStorage.getRefundReports(req.tenant, filter, {
@@ -811,7 +811,7 @@ export default class TransactionService {
startDateTime: filteredRequest.StartDateTime,
chargingStationIDs: filteredRequest.ChargingStationID ? filteredRequest.ChargingStationID.split('|') : null,
siteAreaIDs: filteredRequest.SiteAreaID ? filteredRequest.SiteAreaID.split('|') : null,
- siteIDs: Authorizations.getAuthorizedSiteAdminIDs(req.user, filteredRequest.SiteID ? filteredRequest.SiteID.split('|') : null),
+ siteIDs: await Authorizations.getAuthorizedSiteAdminIDs(req.tenant, req.user, filteredRequest.SiteID ? filteredRequest.SiteID.split('|') : null),
userIDs: filteredRequest.UserID ? filteredRequest.UserID.split('|') : null,
connectorIDs: filteredRequest.ConnectorID ? filteredRequest.ConnectorID.split('|').map((connectorID) => Utils.convertToInt(connectorID)) : null,
},
@@ -1041,8 +1041,8 @@ export default class TransactionService {
withCompany: filteredRequest.WithCompany,
siteAreaIDs: filteredRequest.SiteAreaID ? filteredRequest.SiteAreaID.split('|') : null,
withSiteArea: filteredRequest.WithSiteArea,
- siteIDs: filteredRequest.SiteID ? Authorizations.getAuthorizedSiteAdminIDs(req.user, filteredRequest.SiteID.split('|')) : null,
- siteAdminIDs: Authorizations.getAuthorizedSiteAdminIDs(req.user),
+ siteIDs: filteredRequest.SiteID ? await Authorizations.getAuthorizedSiteAdminIDs(req.tenant, req.user, filteredRequest.SiteID.split('|')) : null,
+ siteAdminIDs: await Authorizations.getAuthorizedSiteAdminIDs(req.tenant, req.user),
startDateTime: filteredRequest.StartDateTime ? filteredRequest.StartDateTime : null,
endDateTime: filteredRequest.EndDateTime ? filteredRequest.EndDateTime : null,
refundStatus: filteredRequest.RefundStatus ? filteredRequest.RefundStatus.split('|') : null,

Retrieve site ID from storage

- Call storage to retrieve assigned site ID
- Not rely on 'static 'user token
- remove unnused method