Migrate endpoints to new auth & refactor
List assets migration
List assets in error migration
Refactor for better lisibility & auth alignement
MODIFY
src/authorization/AuthorizationsDefinition.ts
src/authorization/AuthorizationsDefinition.ts
@@ -340,12 +340,16 @@ export const AUTHORIZATION_DEFINITION: AuthorizationDefinition = {
Action.CHECK_CONNECTION, Action.RETRIEVE_CONSUMPTION, Action.CREATE_CONSUMPTION]
},
{
- resource: Entity.ASSET, action: [Action.LIST, Action.IN_ERROR],
+ resource: Entity.ASSET, action: Action.LIST,
attributes: [
'id', 'name', 'siteAreaID', 'siteArea.id', 'siteArea.name', 'siteArea.siteID', 'siteID', 'assetType', 'coordinates',
'dynamicAsset', 'usesPushAPI', 'connectionID', 'meterID', 'currentInstantWatts', 'currentStateOfCharge', 'issuer'
]
},
+ {
+ resource: Entity.ASSET, action: Action.IN_ERROR,
+ attributes: ['id', 'name', 'errorCodeDetails', 'errorCode']
+ },
{ resource: Entity.SETTING, action: Action.LIST },
{ resource: Entity.SETTING, action: [Action.CREATE, Action.READ, Action.UPDATE, Action.DELETE] },
{
@@ -1030,21 +1034,7 @@ export const AUTHORIZATION_DEFINITION: AuthorizationDefinition = {
],
},
{
- resource: Entity.ASSET, action: Action.LIST,
- condition: {
- Fn: 'custom:dynamicAuthorizations',
- args: {
- asserts: [],
- filters: ['AssignedSites', 'LocalIssuer']
- }
- },
- attributes: [
- 'id', 'name', 'siteAreaID', 'siteArea.id', 'siteArea.name', 'siteArea.siteID', 'siteID', 'assetType', 'coordinates',
- 'dynamicAsset', 'connectionID', 'meterID', 'currentInstantWatts', 'currentStateOfCharge'
- ],
- },
- {
- resource: Entity.ASSET, action: Action.READ,
+ resource: Entity.ASSET, action: [Action.LIST, Action.READ],
condition: {
Fn: 'custom:dynamicAuthorizations',
args: {

MODIFY
src/server/rest/v1/service/AssetService.ts
src/server/rest/v1/service/AssetService.ts
@@ -318,22 +318,22 @@ export default class AssetService {
}
public static async handleGetAssetsInError(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
+
// Check if component is active
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.ASSET,
Action.LIST, Entity.ASSET, MODULE_NAME, 'handleGetAssetsInError');
- // Check auth
- if (!await Authorizations.canListAssetsInError(req.user)) {
- throw new AppAuthError({
- errorCode: HTTPAuthError.FORBIDDEN,
- user: req.user,
- action: Action.IN_ERROR, entity: Entity.ASSET,
- module: MODULE_NAME, method: 'handleGetAssetsInError'
- });
- }
+
// Filter
const filteredRequest = AssetValidator.getInstance().validateAssetsGetReq(req.query);
- // Build error type
- const errorType = (filteredRequest.ErrorType ? filteredRequest.ErrorType.split('|') : [AssetInErrorType.MISSING_SITE_AREA]);
+
+ // Check dynamic auth
+ const authorizationAssetsFilter = await AuthorizationService.checkAndGetAssetsInErrorAuthorizations(
+ req.tenant, req.user, filteredRequest);
+ if (!authorizationAssetsFilter.authorized) {
+ UtilsService.sendEmptyDataResult(res, next);
+ return;
+ }
+
// Get the assets
const assets = await AssetStorage.getAssetsInError(req.tenant,
{
@@ -341,14 +341,16 @@ export default class AssetService {
search: filteredRequest.Search,
siteAreaIDs: (filteredRequest.SiteAreaID ? filteredRequest.SiteAreaID.split('|') : null),
siteIDs: (filteredRequest.SiteID ? filteredRequest.SiteID.split('|') : null),
- errorType
+ errorType: (filteredRequest.ErrorType ? filteredRequest.ErrorType.split('|') : [AssetInErrorType.MISSING_SITE_AREA]),
+ ...authorizationAssetsFilter.filters
},
- { limit: filteredRequest.Limit,
+ {
+ limit: filteredRequest.Limit,
skip: filteredRequest.Skip,
sort: UtilsService.httpSortFieldsToMongoDB(filteredRequest.SortFields),
onlyRecordCount: filteredRequest.OnlyRecordCount
},
- [ 'id', 'name', 'errorCodeDetails', 'errorCode' ]
+ authorizationAssetsFilter.projectFields
);
res.json(assets);
next();
@@ -455,27 +457,22 @@ export default class AssetService {
}
public static async handleGetAssets(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
+
// Check if component is active
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.ASSET,
Action.LIST, Entity.ASSET, MODULE_NAME, 'handleGetAssets');
- // Check auth
- if (!await Authorizations.canListAssets(req.user)) {
- throw new AppAuthError({
- errorCode: HTTPAuthError.FORBIDDEN,
- user: req.user,
- action: Action.LIST, entity: Entity.ASSET,
- module: MODULE_NAME, method: 'handleGetAssets'
- });
- }
+
// Filter
const filteredRequest = AssetValidator.getInstance().validateAssetsGetReq(req.query);
- // Get authorization filters
- const authorizationAssetsFilters = await AuthorizationService.checkAndGetAssetsAuthorizations(
+
+ // Check dynamic auth
+ const authorizationAssetsFilter = await AuthorizationService.checkAndGetAssetsAuthorizations(
req.tenant, req.user, filteredRequest);
- if (!authorizationAssetsFilters.authorized) {
+ if (!authorizationAssetsFilter.authorized) {
UtilsService.sendEmptyDataResult(res, next);
return;
}
+
// Get the assets
const assets = await AssetStorage.getAssets(req.tenant,
{
@@ -486,7 +483,7 @@ export default class AssetService {
withSiteArea: filteredRequest.WithSiteArea,
withNoSiteArea: filteredRequest.WithNoSiteArea,
dynamicOnly: filteredRequest.DynamicOnly,
- ...authorizationAssetsFilters.filters
+ ...authorizationAssetsFilter.filters
},
{
limit: filteredRequest.Limit,
@@ -494,7 +491,7 @@ export default class AssetService {
sort: UtilsService.httpSortFieldsToMongoDB(filteredRequest.SortFields),
onlyRecordCount: filteredRequest.OnlyRecordCount
},
- authorizationAssetsFilters.projectFields
+ authorizationAssetsFilter.projectFields
);
res.json(assets);
next();

MODIFY
src/server/rest/v1/service/AuthorizationService.ts
src/server/rest/v1/service/AuthorizationService.ts
@@ -263,6 +263,23 @@ export default class AuthorizationService {
return authorizationFilters;
}
+ public static async checkAndGetAssetsInErrorAuthorizations(tenant: Tenant, userToken: UserToken,
+ filteredRequest?: HttpAssetsRequest): Promise<AuthorizationFilter> {
+ const authorizationFilters: AuthorizationFilter = {
+ filters: {},
+ dataSources: new Map(),
+ projectFields: [],
+ authorized: false,
+ };
+ // Check static & dynamic authorization
+ await AuthorizationService.canPerformAuthorizationAction(tenant, userToken, Entity.ASSET, Action.IN_ERROR,
+ authorizationFilters, filteredRequest, null, true);
+ return authorizationFilters;
+}
+
+
+
+
public static async checkAndGetUserAuthorizations(tenant: Tenant, userToken: UserToken,
filteredRequest: Partial<HttpUserRequest>, authAction: Action, entityData?: EntityData): Promise<AuthorizationFilter> {
return AuthorizationService.checkAndGetEntityAuthorizations(

Migrate endpoints to new auth & refactor

- List assets migration
- List assets in error migration
- Refactor for better lisibility & auth alignement