Optimized User
MODIFY
src/server/rest/v1/service/SiteService.ts
src/server/rest/v1/service/SiteService.ts
@@ -304,13 +304,6 @@ export default class SiteService {
},
[ 'user.id', 'user.name', 'user.firstName', 'user.email', 'user.role', 'siteAdmin', 'siteOwner', 'siteID' ]
);
- // Filter
- users.result = users.result.map((siteuser) => ({
- siteID: siteuser.siteID,
- siteAdmin: siteuser.siteAdmin,
- siteOwner: siteuser.siteOwner,
- user: UserSecurity.filterUserResponse(siteuser.user, req.user)
- }));
res.json(users);
next();
}

MODIFY
src/server/rest/v1/service/UserService.ts
src/server/rest/v1/service/UserService.ts
@@ -19,7 +19,6 @@ import NotificationHandler from '../../../../notification/NotificationHandler';
import OCPIClientFactory from '../../../../client/ocpi/OCPIClientFactory';
import { OCPIRole } from '../../../../types/ocpi/OCPIRole';
import { ServerAction } from '../../../../types/Server';
-import SessionHashService from './SessionHashService';
import SettingStorage from '../../../../storage/mongodb/SettingStorage';
import SiteStorage from '../../../../storage/mongodb/SiteStorage';
import { StatusCodes } from 'http-status-codes';
@@ -598,7 +597,12 @@ export default class UserService {
});
}
// Get the user
- const user = await UserStorage.getUser(req.user.tenantID, userID);
+ const user = await UserStorage.getUser(req.user.tenantID, userID,
+ [
+ 'id', 'name', 'firstName', 'email', 'role', 'status','issuer', 'locale', 'deleted', 'plateID',
+ 'notificationsActive', 'notifications', 'phone', 'mobile', 'iNumber', 'costCenter', 'address'
+ ]
+ );
UtilsService.assertObjectExists(action, user, `User '${userID}' does not exist`,
MODULE_NAME, 'handleGetUser', req.user);
// Deleted?
@@ -612,9 +616,7 @@ export default class UserService {
action: action
});
}
- res.json(
- UserSecurity.filterUserResponse(user, req.user)
- );
+ res.json(user);
next();
}
@@ -741,14 +743,12 @@ export default class UserService {
onlyRecordCount: filteredRequest.OnlyRecordCount,
skip: filteredRequest.Skip,
sort: filteredRequest.Sort
- }
+ },
+ [
+ 'id', 'name', 'firstName', 'email', 'role', 'status','issuer',
+ 'createdOn', 'lastChangedOn', 'errorCodeDetails', 'errorCode'
+ ]
);
- // Filter
- UserSecurity.filterUsersResponse(users, req.user);
- // Limit to 100
- if (users.result.length > 100) {
- users.result.length = 100;
- }
// Return
res.json(users);
next();
@@ -1026,7 +1026,7 @@ export default class UserService {
// Get the tag
const tag = await UserStorage.getTag(req.user.tenantID, tagID, { withUser: true },
[
- 'id', 'issuer', 'description', 'active', 'transactionsCount', 'default',
+ 'id', 'issuer', 'description', 'active', 'default',
'userID', 'user.id', 'user.name', 'user.firstName', 'user.email'
]
);
@@ -1436,8 +1436,6 @@ export default class UserService {
'billingData.customerID', 'billingData.lastChangedOn'
]
);
- // Filter
- UserSecurity.filterUsersResponse(users, req.user);
return users;
}
}

MODIFY
src/server/rest/v1/service/security/ChargingStationSecurity.ts
src/server/rest/v1/service/security/ChargingStationSecurity.ts
@@ -104,7 +104,7 @@ export default class ChargingStationSecurity {
currentType: connector.currentType,
voltage: connector.voltage,
amperage: connector.amperage,
- user: UserSecurity.filterMinimalUserResponse(connector.user, loggedUser),
+ user: connector.user,
phaseAssignmentToGrid: connector.phaseAssignmentToGrid
};
});

MODIFY
src/server/rest/v1/service/security/UserSecurity.ts
src/server/rest/v1/service/security/UserSecurity.ts
import { HttpSitesAssignUserRequest, HttpTagsRequest, HttpUserMobileTokenRequest, HttpUserRequest, HttpUserSitesRequest, HttpUsersRequest } from '../../../../../types/requests/HttpUserRequest';
-import User, { UserRole } from '../../../../../types/User';
import Authorizations from '../../../../../authorization/Authorizations';
-import Constants from '../../../../../utils/Constants';
-import { DataResult } from '../../../../../types/DataResult';
import Tag from '../../../../../types/Tag';
-import { UserInError } from '../../../../../types/InError';
import UserNotifications from '../../../../../types/UserNotifications';
+import { UserRole } from '../../../../../types/User';
import UserToken from '../../../../../types/UserToken';
import Utils from '../../../../../utils/Utils';
import UtilsSecurity from './UtilsSecurity';
@@ -106,121 +103,6 @@ export default class UserSecurity {
return filteredRequest;
}
- // User
- static filterUserResponse(user: User | UserInError, loggedUser: UserToken): User {
- const filteredUser: User = {} as User;
- if (!user) {
- return null;
- }
- // Check auth
- if (Authorizations.canReadUser(loggedUser, user.id)) {
- // Admin?
- if (Authorizations.canUpdateUser(loggedUser, user.id)) {
- filteredUser.id = user.id;
- filteredUser.issuer = user.issuer;
- filteredUser.name = user.name;
- filteredUser.firstName = user.firstName;
- filteredUser.email = user.email;
- filteredUser.locale = user.locale;
- filteredUser.phone = user.phone;
- filteredUser.mobile = user.mobile;
- filteredUser.notificationsActive = user.notificationsActive;
- if (user.notifications) {
- filteredUser.notifications = UserSecurity.filterNotificationsRequest(user.role, user.notifications);
- }
- filteredUser.iNumber = user.iNumber;
- filteredUser.costCenter = user.costCenter;
- filteredUser.status = user.status;
- filteredUser.eulaAcceptedOn = user.eulaAcceptedOn;
- filteredUser.eulaAcceptedVersion = user.eulaAcceptedVersion;
- filteredUser.plateID = user.plateID;
- filteredUser.role = user.role;
- if (Utils.objectHasProperty(user, 'errorCode')) {
- (filteredUser as UserInError).errorCode = (user as UserInError).errorCode;
- }
- if (user.address) {
- filteredUser.address = UtilsSecurity.filterAddressRequest(user.address);
- }
- if (user.billingData) {
- filteredUser.billingData = user.billingData;
- }
- } else {
- // Set only necessary info
- // Demo user?
- if (Authorizations.isDemo(loggedUser)) {
- filteredUser.id = null;
- filteredUser.name = Constants.ANONYMIZED_VALUE;
- filteredUser.firstName = Constants.ANONYMIZED_VALUE;
- } else {
- filteredUser.id = user.id;
- filteredUser.name = user.name;
- filteredUser.firstName = user.firstName;
- }
- filteredUser.issuer = user.issuer;
- filteredUser.email = user.email;
- filteredUser.locale = user.locale;
- filteredUser.phone = user.phone;
- filteredUser.mobile = user.mobile;
- filteredUser.notificationsActive = user.notificationsActive;
- if (user.notifications) {
- filteredUser.notifications = UserSecurity.filterNotificationsRequest(user.role, user.notifications);
- }
- filteredUser.iNumber = user.iNumber;
- filteredUser.costCenter = user.costCenter;
- filteredUser.plateID = user.plateID;
- filteredUser.role = user.role;
- if (Utils.objectHasProperty(user, 'errorCode')) {
- (filteredUser as UserInError).errorCode = (user as UserInError).errorCode;
- }
- if (user.address) {
- filteredUser.address = UtilsSecurity.filterAddressRequest(user.address);
- }
- }
- // Created By / Last Changed By
- UtilsSecurity.filterCreatedAndLastChanged(filteredUser, user, loggedUser);
- }
- return filteredUser;
- }
-
- // User
- static filterMinimalUserResponse(user: User, loggedUser: UserToken): User {
- const filteredUser = {} as User;
- if (!user) {
- return null;
- }
- // Check auth
- if (Authorizations.canReadUser(loggedUser, user.id)) {
- // Demo user?
- if (Authorizations.isDemo(loggedUser)) {
- filteredUser.id = null;
- filteredUser.name = Constants.ANONYMIZED_VALUE;
- filteredUser.firstName = Constants.ANONYMIZED_VALUE;
- filteredUser.email = Constants.ANONYMIZED_VALUE;
- } else {
- filteredUser.id = user.id;
- filteredUser.name = user.name;
- filteredUser.firstName = user.firstName;
- filteredUser.email = user.email;
- }
- }
- return filteredUser;
- }
-
- static filterUsersResponse(users: DataResult<User | UserInError>, loggedUser: UserToken): void {
- const filteredUsers = [];
- if (!users.result) {
- return null;
- }
- for (const user of users.result) {
- // Filter
- const filteredUser = UserSecurity.filterUserResponse(user, loggedUser);
- if (filteredUser) {
- filteredUsers.push(filteredUser);
- }
- }
- users.result = filteredUsers;
- }
-
public static filterTagUpdateRequest(request: any, loggedUser: UserToken): Partial<Tag> {
return UserSecurity.filterTagRequest(request, loggedUser);
}

MODIFY
src/storage/mongodb/UserStorage.ts
src/storage/mongodb/UserStorage.ts
@@ -106,11 +106,11 @@ export default class UserStorage {
return userMDB.count === 1 ? userMDB.result[0] : null;
}
- public static async getUser(tenantID: string, id: string = Constants.UNKNOWN_OBJECT_ID): Promise<User> {
+ public static async getUser(tenantID: string, id: string = Constants.UNKNOWN_OBJECT_ID, projectFields?: string[]): Promise<User> {
const userMDB = await UserStorage.getUsers(tenantID,
{
userIDs: [id],
- }, Constants.DB_PARAMS_SINGLE_RECORD);
+ }, Constants.DB_PARAMS_SINGLE_RECORD, projectFields);
return userMDB.count === 1 ? userMDB.result[0] : null;
}

Optimized User