removed useless object.result map
removed sites double for loop in userService
MODIFY
src/server/rest/v1/service/SiteService.ts
src/server/rest/v1/service/SiteService.ts
@@ -89,13 +89,11 @@ export default class SiteService {
Action.UPDATE, Entity.SITE, MODULE_NAME, 'handleAssignUsersToSite');
// Filter request
const filteredRequest = SiteValidatorRest.getInstance().validateSiteAssignUsersReq(req.body);
+ const serverAction = action === ServerAction.ADD_USERS_TO_SITE ? Action.ASSIGN_USERS_TO_SITE : Action.UNASSIGN_USERS_FROM_SITE;
// Check and Get Site
- const site = await UtilsService.checkAndGetSiteAuthorization(
- req.tenant, req.user, filteredRequest.siteID, Action.ASSIGN_USERS_TO_SITE, action);
+ const site = await UtilsService.checkAndGetSiteAuthorization(req.tenant, req.user, filteredRequest.siteID, serverAction, action);
// Check and Get Users
- const users = await UtilsService.checkAndGetSiteUsersAuthorization(
- req.tenant, req.user, site, filteredRequest.userIDs, action);
- const serverAction = action === ServerAction.ADD_USERS_TO_SITE ? Action.ASSIGN_USERS_TO_SITE : Action.UNASSIGN_USERS_FROM_SITE;
+ const users = await UtilsService.checkAndGetSiteUsersAuthorization(req.tenant, req.user, site, filteredRequest.userIDs, action);
// Save
for (const user of users) {
const authorized = AuthorizationService.canPerformAction(user, serverAction);
@@ -157,13 +155,6 @@ export default class SiteService {
},
authorizations.projectFields
);
- // Filter
- siteUsers.result = siteUsers.result.map((siteUser) => ({
- siteID: siteUser.siteID,
- siteAdmin: siteUser.siteAdmin,
- siteOwner: siteUser.siteOwner,
- user: siteUser.user
- }));
// add user auth
await AuthorizationService.addSiteUsersAuthorizations(req.tenant, req.user, siteUsers as SiteUserDataResult, authorizations);
res.json(siteUsers);

MODIFY
src/server/rest/v1/service/UserService.ts
src/server/rest/v1/service/UserService.ts
@@ -109,41 +109,29 @@ export default class UserService {
Action.UPDATE, Entity.SITE, 'SiteService', 'handleAssignSitesToUser');
// Filter request
const filteredRequest = UserValidatorRest.getInstance().validateUserSitesAssignReq(req.body);
+ const serverAction = action === ServerAction.ADD_SITES_TO_USER ? Action.ASSIGN_SITES_TO_USER : Action.UNASSIGN_SITES_FROM_USER;
// Check and Get User
- const user = await UtilsService.checkAndGetUserAuthorization(req.tenant, req.user, filteredRequest.userID, Action.READ, action);
+ const user = await UtilsService.checkAndGetUserAuthorization(req.tenant, req.user, filteredRequest.userID, serverAction, action);
// Check and Get Sites
const sites = await UtilsService.checkAndGetUserSitesAuthorization(req.tenant, req.user, user, filteredRequest.siteIDs, action);
// Save
- if (action === ServerAction.ADD_SITES_TO_USER) {
for (const site of sites) {
- const authorized = AuthorizationService.canPerformAction(site, Action.ASSIGN_SITES_TO_USER);
+ const authorized = AuthorizationService.canPerformAction(site, serverAction);
if (!authorized) {
throw new AppAuthError({
errorCode: HTTPAuthError.FORBIDDEN,
user: req.user,
- action: Action.ASSIGN_SITES_TO_USER, entity: Entity.USER_SITE,
+ action: serverAction, entity: Entity.USER_SITE,
module: MODULE_NAME, method: 'checkAndGetPricingDefinitionAuthorization',
value: site.id
});
}
}
+ if (action === ServerAction.ADD_SITES_TO_USER) {
await UserStorage.addSitesToUser(req.tenant, filteredRequest.userID, sites.map((site) => site.id));
} else {
- for (const site of sites) {
- const authorized = AuthorizationService.canPerformAction(site, Action.UNASSIGN_SITES_FROM_USER);
- if (!authorized) {
- throw new AppAuthError({
- errorCode: HTTPAuthError.FORBIDDEN,
- user: req.user,
- action: Action.ASSIGN_USERS_TO_SITE, entity: Entity.USER_SITE,
- module: MODULE_NAME, method: 'checkAndGetPricingDefinitionAuthorization',
- value: site.id
- });
- }
- }
await UserStorage.removeSitesFromUser(req.tenant, filteredRequest.userID, sites.map((site) => site.id));
}
- // Log
await Logging.logInfo({
tenantID: req.tenant.id,
user: req.user,
@@ -381,13 +369,6 @@ export default class UserService {
},
authorizations.projectFields
);
- // Filter
- userSites.result = userSites.result.map((userSite) => ({
- userID: userSite.userID,
- siteAdmin: userSite.siteAdmin,
- siteOwner: userSite.siteOwner,
- site: userSite.site
- }));
// Add Auth flags
await AuthorizationService.addUserSitesAuthorizations(req.tenant, req.user, userSites as UserSiteDataResult , authorizations);
res.json(userSites);

MODIFY
src/server/rest/v1/service/UtilsService.ts
src/server/rest/v1/service/UtilsService.ts
@@ -322,8 +322,7 @@ export default class UtilsService {
// Check mandatory fields
UtilsService.assertIdIsProvided(action, siteID, MODULE_NAME, 'checkAndGetSiteAuthorization', userToken);
// Get dynamic auth
- const authorizations = await AuthorizationService.checkAndGetSiteAuthorizations(
- tenant, userToken, { ID: siteID }, authAction, entityData);
+ const authorizations = await AuthorizationService.checkAndGetSiteAuthorizations(tenant, userToken, { ID: siteID }, authAction, entityData);
// Get Site
const site = await SiteStorage.getSite(tenant, siteID,
{

removed useless object.result map
removed sites double for loop in userService