Refactor assets auth & remove duplicate code
MODIFY
src/server/rest/v1/service/AssetService.ts
src/server/rest/v1/service/AssetService.ts
@@ -269,8 +269,8 @@ export default class AssetService {
// Filter
const filteredRequest = AssetValidator.getInstance().validateAssetsGetReq(req.query);
// Check dynamic auth
- const authorizationAssetsInErrorFilter = await AuthorizationService.checkAndGetAssetsInErrorAuthorizations(
- req.tenant, req.user, filteredRequest);
+ const authorizationAssetsInErrorFilter = await AuthorizationService.checkAndGetAssetsAuthorizations(
+ req.tenant, req.user, Action.IN_ERROR, filteredRequest);
// Get the assets
const assets = await AssetStorage.getAssetsInError(req.tenant,
{
@@ -375,7 +375,7 @@ export default class AssetService {
const filteredRequest = AssetValidator.getInstance().validateAssetsGetReq(req.query);
// Check dynamic auth
const authorizationAssetsFilter = await AuthorizationService.checkAndGetAssetsAuthorizations(
- req.tenant, req.user, filteredRequest);
+ req.tenant, req.user, Action.LIST, filteredRequest);
// Get the assets
const assets = await AssetStorage.getAssets(req.tenant,
{

MODIFY
src/server/rest/v1/service/AuthorizationService.ts
src/server/rest/v1/service/AuthorizationService.ts
@@ -250,7 +250,7 @@ export default class AuthorizationService {
}
public static async checkAndGetAssetsAuthorizations(tenant: Tenant, userToken: UserToken,
- filteredRequest?: HttpAssetsRequest): Promise<AuthorizationFilter> {
+ authAction: Action, filteredRequest?: HttpAssetsRequest): Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
dataSources: new Map(),
@@ -258,7 +258,7 @@ export default class AuthorizationService {
authorized: false,
};
// Check static & dynamic authorization
- await AuthorizationService.canPerformAuthorizationAction(tenant, userToken, Entity.ASSET, Action.LIST,
+ await AuthorizationService.canPerformAuthorizationAction(tenant, userToken, Entity.ASSET, authAction,
authorizationFilters, filteredRequest, null, true);
return authorizationFilters;
}
@@ -277,23 +277,6 @@ 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(

MODIFY
src/server/rest/v1/service/UtilsService.ts
src/server/rest/v1/service/UtilsService.ts
@@ -551,7 +551,7 @@ export default class UtilsService {
});
}
// Check dynamic auth
- const authorizationFilter = await AuthorizationService.checkAndGetAssetsAuthorizations(tenant, userToken);
+ const authorizationFilter = await AuthorizationService.checkAndGetAssetsAuthorizations(tenant, userToken, Action.LIST);
if (!authorizationFilter.authorized) {
throw new AppAuthError({
errorCode: HTTPAuthError.FORBIDDEN,

Refactor assets auth & remove duplicate code