Optimized Tags
MODIFY
src/server/rest/v1/service/UserService.ts
src/server/rest/v1/service/UserService.ts
@@ -1024,11 +1024,21 @@ export default class UserService {
const tagID = UserSecurity.filterTagRequestByID(req.query);
UtilsService.assertIdIsProvided(action, tagID, MODULE_NAME, 'handleGetTag', req.user);
// Get the tag
- const tag = await UserStorage.getTag(req.user.tenantID, tagID, { withUser: true });
+ const tag = await UserStorage.getTag(req.user.tenantID, tagID, { withUser: true },
+ [
+ 'id', 'issuer', 'description', 'active', 'transactionsCount', 'default',
+ 'userID', 'user.id', 'user.name', 'user.firstName', 'user.email'
+ ]
+ );
UtilsService.assertObjectExists(action, tag, `Tag with ID '${tagID}' does not exist`,
MODULE_NAME, 'handleGetTag', req.user);
+ // Check Users
+ if (!Authorizations.canReadUser(req.user, tag.userID)) {
+ delete tag.userID;
+ delete tag.user;
+ }
// Return
- res.json(UserSecurity.filterTagResponse(tag, req.user));
+ res.json(tag);
next();
}
@@ -1042,6 +1052,14 @@ export default class UserService {
module: MODULE_NAME, method: 'handleGetTags'
});
}
+ // Check Users
+ let userProject: string[] = [];
+ if (Authorizations.canListUsers(req.user)) {
+ userProject = [
+ 'userID', 'user.id', 'user.name', 'user.firstName', 'user.email',
+ 'createdBy.name', 'createdBy.firstName', 'lastChangedBy.name', 'lastChangedBy.firstName'
+ ];
+ }
// Filter
const filteredRequest = UserSecurity.filterTagsRequest(req.query);
// Get the tags
@@ -1053,11 +1071,11 @@ export default class UserService {
withUser: true,
},
{ limit: filteredRequest.Limit, skip: filteredRequest.Skip, sort: filteredRequest.Sort, onlyRecordCount: filteredRequest.OnlyRecordCount },
- ['id', 'userID', 'active', 'ocpiToken', 'description', 'issuer', 'default', 'user.id', 'user.name', 'user.firstName', 'user.email',
- 'createdOn', 'createdBy', 'lastChangedOn', 'lastChangedBy'],
+ [
+ 'id', 'userID', 'active', 'ocpiToken', 'description', 'issuer', 'default',
+ 'createdOn', 'lastChangedOn', ...userProject
+ ],
);
- // Filter
- UserSecurity.filterTagsResponse(tags, req.user);
// Return
res.json(tags);
next();

MODIFY
src/server/rest/v1/service/security/UserSecurity.ts
src/server/rest/v1/service/security/UserSecurity.ts
@@ -221,24 +221,6 @@ export default class UserSecurity {
users.result = filteredUsers;
}
- static filterTagsResponse(tags: DataResult<Tag>, loggedUser: UserToken): void {
- const filteredTags = [];
- if (!tags.result) {
- return null;
- }
- if (!Authorizations.canListTags(loggedUser)) {
- return null;
- }
- for (const tag of tags.result) {
- // Filter
- const filteredTag = UserSecurity.filterTagResponse(tag, loggedUser);
- if (filteredTag) {
- filteredTags.push(filteredTag);
- }
- }
- tags.result = filteredTags;
- }
-
public static filterTagUpdateRequest(request: any, loggedUser: UserToken): Partial<Tag> {
return UserSecurity.filterTagRequest(request, loggedUser);
}
@@ -262,31 +244,6 @@ export default class UserSecurity {
return filteredTag;
}
- static filterTagResponse(tag: Tag, loggedUser: UserToken): Tag {
- const filteredTag = {} as Tag;
- if (!tag) {
- return null;
- }
- // Check auth
- if (Authorizations.canReadTag(loggedUser)) {
- filteredTag.id = tag.id;
- filteredTag.issuer = tag.issuer;
- filteredTag.description = tag.description;
- filteredTag.active = tag.active;
- filteredTag.transactionsCount = tag.transactionsCount;
- filteredTag.userID = tag.userID;
- filteredTag.default = tag.default;
- if (tag.user) {
- filteredTag.user = UserSecurity.filterMinimalUserResponse(tag.user, loggedUser);
- }
- // Created By / Last Changed By
- if (Authorizations.canUpdateTag(loggedUser)) {
- UtilsSecurity.filterCreatedAndLastChanged(filteredTag, tag, loggedUser);
- }
- }
- return filteredTag;
- }
-
static filterNotificationsRequest(role: UserRole, notifications: UserNotifications): UserNotifications {
// All Users
let filteredNotifications: UserNotifications = {

MODIFY
src/storage/mongodb/UserStorage.ts
src/storage/mongodb/UserStorage.ts
@@ -684,12 +684,12 @@ export default class UserStorage {
}
public static async getTag(tenantID: string, id: string,
- params: { withUser?: boolean; withNbrTransactions?: boolean } = {}): Promise<Tag> {
+ params: { withUser?: boolean; withNbrTransactions?: boolean } = {}, projectFields?: string[]): Promise<Tag> {
const tagMDB = await UserStorage.getTags(tenantID, {
tagIDs: [id],
withUser: params.withUser,
withNbrTransactions: params.withNbrTransactions,
- }, Constants.DB_PARAMS_SINGLE_RECORD);
+ }, Constants.DB_PARAMS_SINGLE_RECORD, projectFields);
return tagMDB.count === 1 ? tagMDB.result[0] : null;
}

Optimized Tags