Refactor assign users to site.
MODIFY
src/server/rest/v1/service/AuthorizationService.ts
src/server/rest/v1/service/AuthorizationService.ts
@@ -168,28 +168,10 @@ export default class AuthorizationService {
projectFields: [],
authorized: false,
};
- // Check static auth
- const authorizationContext: AuthorizationContext = {};
- const authResult = action === ServerAction.ADD_USERS_TO_SITE ?
- await Authorizations.canAssignUsersSites(userToken, authorizationContext) :
- await Authorizations.canUnassignUsersSites(userToken, authorizationContext);
- authorizationFilters.authorized = authResult.authorized;
- // Check
- if (!authorizationFilters.authorized) {
- throw new AppAuthError({
- errorCode: HTTPAuthError.FORBIDDEN,
- user: userToken,
- action: action === ServerAction.ADD_USERS_TO_SITE ? Action.ASSIGN : Action.UNASSIGN,
- entity: Entity.USERS_SITES,
- module: MODULE_NAME, method: 'checkAssignSiteUsersAuthorizationFilters',
- });
- }
- // Process dynamic filters
- await AuthorizationService.processDynamicFilters(tenant, userToken, Action.READ, Entity.SITE,
- authorizationFilters, authorizationContext, { SiteID: filteredRequest.siteID });
- // Filter projected fields
- authorizationFilters.projectFields = AuthorizationService.filterProjectFields(
- authResult.fields, filteredRequest.ProjectFields);
+ // Check static & dynamic authorization
+ const authAction = action === ServerAction.ADD_USERS_TO_SITE ? Action.ASSIGN : Action.UNASSIGN;
+ await this.canPerformAuthorizationAction(
+ tenant, userToken, Entity.USERS_SITES, authAction, authorizationFilters, filteredRequest);
return authorizationFilters;
}
@@ -361,7 +343,7 @@ export default class AuthorizationService {
}
public static async checkAndGetUsersAuthorizationFilters(
- tenant: Tenant, userToken: UserToken, filteredRequest: HttpUsersRequest): Promise<AuthorizationFilter> {
+ tenant: Tenant, userToken: UserToken, filteredRequest: Record<string, any>): Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
dataSources: new Map(),

MODIFY
src/server/rest/v1/service/SiteService.ts
src/server/rest/v1/service/SiteService.ts
@@ -147,20 +147,9 @@ export default class SiteService {
Action.UPDATE, Entity.SITE, MODULE_NAME, 'handleAssignUsersToSite');
// Filter request
const filteredRequest = SiteSecurity.filterAssignSiteUsers(req.body);
- // Check mandatory fields
- UtilsService.assertIdIsProvided(action, filteredRequest.siteID, MODULE_NAME, 'handleAssignUsersToSite', req.user);
- if (Utils.isEmptyArray(filteredRequest.userIDs)) {
- throw new AppError({
- source: Constants.CENTRAL_SERVER,
- errorCode: HTTPError.GENERAL_ERROR,
- message: 'The User\'s IDs must be provided',
- module: MODULE_NAME, method: 'handleAssignUsersToSite',
- user: req.user
- });
- }
// Check and Get Site
const site = await UtilsService.checkAndGetSiteAuthorization(
- req.tenant, req.user, filteredRequest.siteID, Action.UPDATE, action, {});
+ req.tenant, req.user, filteredRequest.siteID, Action.READ, action, {});
// Check and Get Users
const users = await UtilsService.checkSiteUsersAuthorization(
req.tenant, req.user, site, filteredRequest.userIDs, action, {});
@@ -173,8 +162,11 @@ export default class SiteService {
// Log
await Logging.logSecurityInfo({
tenantID: req.user.tenantID,
- user: req.user, module: MODULE_NAME, method: 'handleAssignUsersToSite',
- message: 'Site\'s Users have been removed successfully', action: action
+ user: req.user,
+ module: MODULE_NAME,
+ method: 'handleAssignUsersToSite',
+ message: 'Site\'s Users have been removed successfully',
+ action: action
});
// Ok
res.json(Constants.REST_RESPONSE_SUCCESS);

MODIFY
src/server/rest/v1/service/UtilsService.ts
src/server/rest/v1/service/UtilsService.ts
@@ -101,6 +101,45 @@ export default class UtilsService {
return chargingStation;
}
+ public static async checkAndGetUsersAuthorization(tenant: Tenant, userToken: UserToken, action: ServerAction,
+ additionalFilters: Record<string, any>, applyProjectFields = false): Promise<User[]> {
+ // Check dynamic auth
+ const authorizationFilter = await AuthorizationService.checkAndGetUsersAuthorizationFilters(tenant, userToken, {});
+ if (!authorizationFilter.authorized) {
+ throw new AppAuthError({
+ errorCode: HTTPAuthError.FORBIDDEN,
+ user: userToken,
+ action: Action.LIST,
+ entity: Entity.USERS,
+ module: MODULE_NAME, method: 'checkAndGetUsersAuthorization',
+ });
+ }
+ // Get Users
+ const users = await UserStorage.getUsers(tenant.id,
+ {
+ ...additionalFilters,
+ ...authorizationFilter.filters,
+ }, Constants.DB_PARAMS_MAX_LIMIT,
+ applyProjectFields ? authorizationFilter.projectFields : null
+ );
+ // Check
+ for (const user of users.result) {
+ // External User
+ if (!user.issuer) {
+ throw new AppError({
+ source: Constants.CENTRAL_SERVER,
+ errorCode: HTTPError.GENERAL_ERROR,
+ message: 'User not issued by the organization',
+ module: MODULE_NAME, method: 'checkSiteUsersAuthorization',
+ user: userToken,
+ actionOnUser: user,
+ action: action
+ });
+ }
+ }
+ return users.result;
+ }
+
public static async checkAndGetCompanyAuthorization(tenant: Tenant, userToken: UserToken, companyID: string, authAction: Action,
action: ServerAction, additionalFilters: Record<string, any>, applyProjectFields = false): Promise<Company> {
// Check mandatory fields
@@ -282,17 +321,11 @@ export default class UtilsService {
module: MODULE_NAME, method: 'checkSiteUsersAuthorization',
});
}
- // Get Users
- const users = await UserStorage.getUsers(tenant.id,
- {
- userIDs,
- ...additionalFilters,
- ...authorizationFilter.filters,
- }, Constants.DB_PARAMS_MAX_LIMIT,
- applyProjectFields ? authorizationFilter.projectFields : null
- );
+ // Get users
+ const users = await this.checkAndGetUsersAuthorization(tenant, userToken, action,
+ { userIDs, ...additionalFilters }, applyProjectFields);
// Must have the same result
- if (userIDs.length !== users.result.length) {
+ if (userIDs.length !== users.length) {
throw new AppAuthError({
errorCode: HTTPAuthError.FORBIDDEN,
user: userToken,
@@ -301,22 +334,7 @@ export default class UtilsService {
module: MODULE_NAME, method: 'checkSiteUsersAuthorization',
});
}
- // Check
- for (const user of users.result) {
- // External User
- if (!user.issuer) {
- throw new AppError({
- source: Constants.CENTRAL_SERVER,
- errorCode: HTTPError.GENERAL_ERROR,
- message: 'User not issued by the organization',
- module: MODULE_NAME, method: 'checkSiteUsersAuthorization',
- user: userToken,
- actionOnUser: user,
- action: action
- });
- }
- }
- return users.result;
+ return users;
}
public static async checkSiteAreaAssetsAuthorization(tenant: Tenant, userToken: UserToken, siteArea: SiteArea, assetIDs: string[],

Refactor assign users to site.