Align site check like charging station
MODIFY
src/server/rest/v1/service/AuthorizationService.ts
src/server/rest/v1/service/AuthorizationService.ts
@@ -42,14 +42,7 @@ export default class AuthorizationService {
if (userToken.role !== UserRole.ADMIN) {
// Get assigned Site IDs from DB
const siteIDs = await AuthorizationService.getAssignedSiteIDs(tenant.id, userToken, filteredRequest.ID);
- if (Utils.isEmptyArray(siteIDs) || !siteIDs.includes(filteredRequest.ID)) {
- throw new AppAuthError({
- errorCode: HTTPAuthError.FORBIDDEN,
- user: userToken,
- action: Action.READ, entity: Entity.SITE,
- module: MODULE_NAME, method: 'checkAndGetSiteAuthorizationFilters',
- });
- } else {
+ if (!Utils.isEmptyArray(siteIDs) && siteIDs.includes(filteredRequest.ID)) {
authorizationFilters.authorized = true;
}
}
@@ -470,17 +463,12 @@ export default class AuthorizationService {
}
// Not an Admin?
if (userToken.role !== UserRole.ADMIN) {
- if (Utils.isTenantComponentActive(tenant, TenantComponents.ORGANIZATION)) {
// Get Company IDs from sssigned Sites
const companyIDs = await AuthorizationService.getAssignedSitesCompanyIDs(tenant.id, userToken);
if (!Utils.isEmptyArray(companyIDs) && companyIDs.includes(filteredRequest.ID)) {
authorizationFilters.authorized = true;
}
- } else {
- authorizationFilters.authorized = true;
}
- }
-
return authorizationFilters;
}

MODIFY
src/server/rest/v1/service/ChargingStationService.ts
src/server/rest/v1/service/ChargingStationService.ts
@@ -986,7 +986,6 @@ export default class ChargingStationService {
if (!chargingStationAuthorizationFilters.authorized) {
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(

MODIFY
src/server/rest/v1/service/CompanyService.ts
src/server/rest/v1/service/CompanyService.ts
@@ -45,7 +45,6 @@ export default class CompanyService {
if (!authorizationCompanyFilters.authorized) {
UtilsService.assertObjectExists(action, null, `Company with ID '${companyID}' does not exist`,
MODULE_NAME, 'handleDeleteCompany', req.user);
- return;
}
// Get
const company = await CompanyStorage.getCompany(req.user.tenantID, companyID, authorizationCompanyFilters.filters);
@@ -101,7 +100,6 @@ export default class CompanyService {
if (!authorizationCompanyFilters.authorized) {
UtilsService.assertObjectExists(action, null, `Company with ID '${filteredRequest.ID}' does not exist`,
MODULE_NAME, 'handleGetCompany', req.user);
- return;
}
// Get company
const company = await CompanyStorage.getCompany(req.user.tenantID, filteredRequest.ID,
@@ -257,7 +255,6 @@ export default class CompanyService {
if (!authorizationCompanyFilters.authorized) {
UtilsService.assertObjectExists(action, null, `Company with ID '${filteredRequest.id}' does not exist`,
MODULE_NAME, 'handleUpdateCompany', req.user);
- return;
}
// Get Company
const company = await CompanyStorage.getCompany(req.user.tenantID, filteredRequest.id, authorizationCompanyFilters.filters);

MODIFY
src/server/rest/v1/service/SiteAreaService.ts
src/server/rest/v1/service/SiteAreaService.ts
@@ -512,7 +512,12 @@ export default class SiteAreaService {
// Check dynamic auth for reading site
const authorizationSiteFilters = await AuthorizationService.checkAndGetSiteAuthorizationFilters(
req.tenant, req.user, { ID: filteredRequest.siteID });
- // Get Site & check it exists
+ // Force the failure
+ if (!authorizationSiteFilters.authorized) {
+ UtilsService.assertObjectExists(action, null, `Site ID '${filteredRequest.siteID}' does not exist`,
+ MODULE_NAME, 'handleCreateSiteArea', req.user);
+ }
+ // Get Site
const site = await SiteStorage.getSite(req.user.tenantID, filteredRequest.siteID, authorizationSiteFilters.filters);
UtilsService.assertObjectExists(action, site, `Site ID '${filteredRequest.siteID}' does not exist`,
MODULE_NAME, 'handleCreateSiteArea', req.user);
@@ -605,7 +610,12 @@ export default class SiteAreaService {
// Check dynamic auth for reading site
const authorizationSiteFilters = await AuthorizationService.checkAndGetSiteAuthorizationFilters(
req.tenant, req.user, { ID: filteredRequest.siteID });
- // Get Site & check it exists
+ // Force the failure
+ if (!authorizationSiteFilters.authorized) {
+ UtilsService.assertObjectExists(action, null, `Site ID '${filteredRequest.siteID}' does not exist`,
+ MODULE_NAME, 'handleUpdateSiteArea', req.user);
+ }
+ // Get Site
const site = await SiteStorage.getSite(req.user.tenantID, filteredRequest.siteID, authorizationSiteFilters.filters);
UtilsService.assertObjectExists(action, site, `Site ID '${filteredRequest.siteID}' does not exist`,
MODULE_NAME, 'handleUpdateSiteArea', req.user);

MODIFY
src/server/rest/v1/service/SiteService.ts
src/server/rest/v1/service/SiteService.ts
@@ -81,10 +81,15 @@ export default class SiteService {
// Check dynamic auth for reading site
const authorizationSiteFilters = await AuthorizationService.checkAndGetSiteAuthorizationFilters(
req.tenant, req.user, { ID: filteredRequest.siteID });
- // Get the Site and check it exists
+ // Force the failure
+ if (!authorizationSiteFilters.authorized) {
+ UtilsService.assertObjectExists(action, null, `Site ID '${filteredRequest.siteID}' does not exist`,
+ MODULE_NAME, 'handleUpdateSiteUserAdmin', req.user);
+ }
+ // Get the Site
const site = await SiteStorage.getSite(
req.user.tenantID, filteredRequest.siteID, authorizationSiteFilters.filters);
- UtilsService.assertObjectExists(action, site, `Site with ID '${filteredRequest.siteID}' does not exist`,
+ UtilsService.assertObjectExists(action, site, `Site ID '${filteredRequest.siteID}' does not exist`,
MODULE_NAME, 'handleUpdateSiteUserAdmin', req.user);
// Get dynamic auth filters for reading user
const authorizationUserFilters = await AuthorizationService.checkAndGetUserAuthorizationFilters(
@@ -166,10 +171,15 @@ export default class SiteService {
// Check dynamic auth for reading site
const authorizationSiteFilters = await AuthorizationService.checkAndGetSiteAuthorizationFilters(
req.tenant, req.user, { ID: filteredRequest.siteID });
- // Get the Site and check it exists
+ // Force the failure
+ if (!authorizationSiteFilters.authorized) {
+ UtilsService.assertObjectExists(action, null, `Site ID '${filteredRequest.siteID}' does not exist`,
+ MODULE_NAME, 'handleUpdateSiteUserOwner', req.user);
+ }
+ // Get the Site
const site = await SiteStorage.getSite(
req.user.tenantID, filteredRequest.siteID, authorizationSiteFilters.filters);
- UtilsService.assertObjectExists(action, site, `Site with ID '${filteredRequest.siteID}' does not exist`,
+ UtilsService.assertObjectExists(action, site, `Site ID '${filteredRequest.siteID}' does not exist`,
MODULE_NAME, 'handleUpdateSiteUserOwner', req.user);
// Get dynamic auth filters for reading user
const authorizationUserFilters = await AuthorizationService.checkAndGetUserAuthorizationFilters(
@@ -242,10 +252,15 @@ export default class SiteService {
// Check dynamic auth for reading
const authorizationSiteFilters = await AuthorizationService.checkAndGetSiteAuthorizationFilters(
req.tenant, req.user, { ID: filteredRequest.siteID });
- // Get the Site & check that it exists
+ // Force the failure
+ if (!authorizationSiteFilters.authorized) {
+ UtilsService.assertObjectExists(action, null, `Site ID '${filteredRequest.siteID}' does not exist`,
+ MODULE_NAME, 'handleAssignUsersToSite', req.user);
+ }
+ // Get the Site
const site = await SiteStorage.getSite(
req.user.tenantID, filteredRequest.siteID, authorizationSiteFilters.filters);
- UtilsService.assertObjectExists(action, site, `Site '${filteredRequest.siteID}' does not exist`,
+ UtilsService.assertObjectExists(action, site, `Site ID '${filteredRequest.siteID}' does not exist`,
MODULE_NAME, 'handleAssignUsersToSite', req.user);
// OCPI Site
if (!site.issuer) {
@@ -365,9 +380,13 @@ export default class SiteService {
// Check dynamic auth
const authorizationSiteFilters = await AuthorizationService.checkAndGetSiteAuthorizationFilters(
req.tenant, req.user, { ID: siteID });
- // Get the site
+ // Force the failure
+ if (!authorizationSiteFilters.authorized) {
+ UtilsService.assertObjectExists(action, null, `Site ID '${siteID}' does not exist`,
+ MODULE_NAME, 'handleDeleteSite', req.user);
+ }
+ // Get the Site
const site = await SiteStorage.getSite(req.user.tenantID, siteID, authorizationSiteFilters.filters);
- // Check that the site exists
UtilsService.assertObjectExists(action, site, `Site with ID '${siteID}' does not exist`,
MODULE_NAME, 'handleDeleteSite', req.user);
// OCPI Site
@@ -416,7 +435,12 @@ export default class SiteService {
// Check dynamic auth
const authorizationSiteFilters = await AuthorizationService.checkAndGetSiteAuthorizationFilters(
req.tenant, req.user, filteredRequest);
- // Get it
+ // Force the failure
+ if (!authorizationSiteFilters.authorized) {
+ UtilsService.assertObjectExists(action, null, `Site ID '${filteredRequest.ID}' does not exist`,
+ MODULE_NAME, 'handleGetSite', req.user);
+ }
+ // Get the Site
const site = await SiteStorage.getSite(req.user.tenantID, filteredRequest.ID,
{
withCompany: filteredRequest.WithCompany,

Align site check like charging station