removed comments and duplicate for loop
todo handle import user
MODIFY
src/server/rest/v1/service/AuthorizationService.ts
src/server/rest/v1/service/AuthorizationService.ts
@@ -196,7 +196,6 @@ export default class AuthorizationService {
return authorizations;
}
- // LIST
public static async checkAndGetSiteUsersAuthorizations(tenant: Tenant, userToken: UserToken,
filteredRequest: Partial<HttpSiteUsersRequest>, failsWithException = true): Promise<AuthorizationFilter> {
const authorizations: AuthorizationFilter = {
@@ -229,7 +228,7 @@ export default class AuthorizationService {
return authorizations;
}
- // ASSIGN/UNASSIGN USERS TO/FROM SITE
+ // Assign/unassign users to/from site
public static async checkAssignSiteUsersAuthorizations(tenant: Tenant, action: ServerAction, userToken: UserToken,
filteredRequest: Partial<HttpSiteAssignUsersRequest>): Promise<AuthorizationFilter> {
const authorizations: AuthorizationFilter = {
@@ -245,7 +244,7 @@ export default class AuthorizationService {
return authorizations;
}
- // ASSIGN/UNASSIGN SITES TO/FROM USER
+ // Assign/unassign sites to/from user
public static async checkAssignUserSitesAuthorizations(
tenant: Tenant, action: ServerAction, userToken: UserToken,
filteredRequest: Partial<HttpUserSitesAssignRequest>): Promise<AuthorizationFilter> {

MODIFY
src/server/rest/v1/service/SiteService.ts
src/server/rest/v1/service/SiteService.ts
@@ -91,38 +91,27 @@ export default class SiteService {
const filteredRequest = SiteValidatorRest.getInstance().validateSiteAssignUsersReq(req.body);
// Check and Get Site
const site = await UtilsService.checkAndGetSiteAuthorization(
- req.tenant, req.user, filteredRequest.siteID, Action.READ, action);
+ req.tenant, req.user, filteredRequest.siteID, Action.ASSIGN_USERS_TO_SITE, 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;
// Save
- if (action === ServerAction.ADD_USERS_TO_SITE) {
for (const user of users) {
- const authorized = AuthorizationService.canPerformAction(user, Action.ASSIGN_USERS_TO_SITE);
+ const authorized = AuthorizationService.canPerformAction(user, serverAction);
if (!authorized) {
throw new AppAuthError({
errorCode: HTTPAuthError.FORBIDDEN,
user: req.user,
- action: Action.ASSIGN_USERS_TO_SITE, entity: Entity.SITE_USER,
+ action: serverAction, entity: Entity.SITE_USER,
module: MODULE_NAME, method: 'handleAssignUsersToSite',
value: site.id
});
}
}
+ if (action === ServerAction.ADD_USERS_TO_SITE) {
await SiteStorage.addUsersToSite(req.tenant, site.id, users.map((user) => user.id));
} else {
- for (const user of users) {
- const authorized = AuthorizationService.canPerformAction(user, Action.UNASSIGN_USERS_FROM_SITE);
- if (!authorized) {
- throw new AppAuthError({
- errorCode: HTTPAuthError.FORBIDDEN,
- user: req.user,
- action: Action.UNASSIGN_USERS_FROM_SITE, entity: Entity.SITE_USER,
- module: MODULE_NAME, method: 'handleAssignUsersToSite',
- value: site.id
- });
- }
- }
await SiteStorage.removeUsersFromSite(req.tenant, site.id, users.map((user) => user.id));
}
await Logging.logInfo({

MODIFY
src/server/rest/v1/service/UserService.ts
src/server/rest/v1/service/UserService.ts
@@ -326,8 +326,7 @@ export default class UserService {
// Filter
const userID = UserValidatorRest.getInstance().validateUserGetReq(req.query).ID.toString();
// Check and Get User
- const user = await UtilsService.checkAndGetUserAuthorization(
- req.tenant, req.user, userID, Action.READ, action, null, null, null);
+ const user = await UtilsService.checkAndGetUserAuthorization(req.tenant, req.user, userID, Action.READ, action);
// Get the user image
const userImage = await UserStorage.getUserImage(req.tenant, user.id);
res.json(userImage);
@@ -440,7 +439,6 @@ export default class UserService {
next();
}
- // eslint-disable-next-line @typescript-eslint/require-await
public static async handleImportUsers(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
// Check auth
if (!(await Authorizations.canImportUsers(req.user)).authorized) {

removed comments and duplicate for loop
todo handle import user