Optimized Connections
MODIFY
src/server/rest/v1/service/ConnectionService.ts
src/server/rest/v1/service/ConnectionService.ts
@@ -27,8 +27,7 @@ export default class ConnectionService {
source: Constants.CENTRAL_SERVER,
errorCode: HTTPError.GENERAL_ERROR,
message: 'The Connection\'s ID must be provided',
- module: MODULE_NAME,
- method: 'handleGetConnection',
+ module: MODULE_NAME, method: 'handleGetConnection',
user: req.user
});
}
@@ -37,22 +36,22 @@ export default class ConnectionService {
throw new AppAuthError({
errorCode: HTTPAuthError.ERROR,
user: req.user,
- action: Action.READ,
- entity: Entity.CONNECTION,
- module: MODULE_NAME,
- method: 'handleGetConnection',
+ action: Action.READ, entity: Entity.CONNECTION,
+ module: MODULE_NAME, method: 'handleGetConnection',
value: connectionID
});
}
+ // Check User
+ let userProject: string[] = [];
+ if (Authorizations.canListUsers(req.user)) {
+ userProject = [ 'createdBy.name', 'createdBy.firstName', 'lastChangedBy.name', 'lastChangedBy.firstName' ];
+ }
// Get it
- const connection = await ConnectionStorage.getConnection(req.user.tenantID, connectionID);
+ const connection = await ConnectionStorage.getConnection(req.user.tenantID, connectionID,
+ [ 'id', 'connectorId', 'createdAt', 'validUntil', 'lastChangedOn', 'createdOn', ...userProject ]);
UtilsService.assertObjectExists(action, connection, `Connection ID '${connectionID}' does not exist`,
MODULE_NAME, 'handleGetConnection', req.user);
- // Return
- res.json(
- // Filter
- ConnectionSecurity.filterConnectionResponse(connection, req.user)
- );
+ res.json(connection);
next();
}
@@ -62,19 +61,20 @@ export default class ConnectionService {
throw new AppAuthError({
errorCode: HTTPAuthError.ERROR,
user: req.user,
- action: Action.LIST,
- entity: Entity.CONNECTIONS,
- module: MODULE_NAME,
- method: 'handleGetConnections'
+ action: Action.LIST, entity: Entity.CONNECTIONS,
+ module: MODULE_NAME, method: 'handleGetConnections'
});
}
// Filter
const filteredRequest = ConnectionSecurity.filterConnectionsRequest(req.query);
+ // Check Users
+ let userProject: string[] = [];
+ if (Authorizations.canListUsers(req.user)) {
+ userProject = [ 'createdBy.name', 'createdBy.firstName', 'lastChangedBy.name', 'lastChangedBy.firstName' ];
+ }
// Get
- const connections = await ConnectionStorage.getConnectionsByUserId(req.user.tenantID, filteredRequest.UserID);
- // Filter
- ConnectionSecurity.filterConnectionsResponse(connections, req.user);
- // Return
+ const connections = await ConnectionStorage.getConnectionsByUserId(req.user.tenantID, filteredRequest.UserID,
+ [ 'id', 'connectorId', 'createdAt', 'validUntil', 'lastChangedOn', 'createdOn', ...userProject ]);
res.json(connections);
next();
}
@@ -85,10 +85,8 @@ export default class ConnectionService {
throw new AppAuthError({
errorCode: HTTPAuthError.ERROR,
user: req.user,
- action: Action.CREATE,
- entity: Entity.CONNECTION,
- module: MODULE_NAME,
- method: 'handleCreateConnection'
+ action: Action.CREATE, entity: Entity.CONNECTION,
+ module: MODULE_NAME, method: 'handleCreateConnection'
});
}
// Filter

MODIFY
src/server/rest/v1/service/security/ConnectionSecurity.ts
src/server/rest/v1/service/security/ConnectionSecurity.ts
-import Authorizations from '../../../../../authorization/Authorizations';
import Connection from '../../../../../types/Connection';
-import { DataResult } from '../../../../../types/DataResult';
import { HttpConnectionsRequest } from '../../../../../types/requests/HttpConnectionRequest';
-import UserToken from '../../../../../types/UserToken';
import UtilsSecurity from './UtilsSecurity';
import sanitize from 'mongo-sanitize';
@@ -38,46 +35,6 @@ export default class ConnectionSecurity {
return ConnectionSecurity.filterConnectionRequest(request);
}
- public static filterConnectionResponse(connection: Connection, loggedUser: UserToken): Connection {
- let filteredConnection: Connection;
- if (!connection) {
- return null;
- }
- // Check auth
- if (Authorizations.canReadConnection(loggedUser, connection.userId)) {
- // Set only necessary info
- filteredConnection = {
- id: connection.id,
- connectorId: connection.connectorId,
- createdAt: connection.createdAt,
- validUntil: connection.validUntil,
- } as Connection;
- // Created By / Last Changed By
- UtilsSecurity.filterCreatedAndLastChanged(
- filteredConnection, connection, loggedUser);
- }
- return filteredConnection;
- }
-
- public static filterConnectionsResponse(connections: DataResult<Connection>, loggedUser: UserToken): DataResult<Connection> {
- const filteredConnections = [];
- if (!connections.result) {
- return null;
- }
- if (!Authorizations.canListConnections(loggedUser)) {
- return null;
- }
- for (const connection of connections.result) {
- // Filter
- const filteredConnection = ConnectionSecurity.filterConnectionResponse(connection, loggedUser);
- if (filteredConnection) {
- // Add
- filteredConnections.push(filteredConnection);
- }
- }
- connections.result = filteredConnections;
- }
-
private static filterConnectionRequest(request: any): Connection {
return {
connectorId: sanitize(request.connectorId),

MODIFY
src/storage/mongodb/ConnectionStorage.ts
src/storage/mongodb/ConnectionStorage.ts
@@ -33,7 +33,7 @@ export default class ConnectionStorage {
return result.value._id.toHexString();
}
- static async getConnectionByConnectorIdAndUserId(tenantID: string, connectorId: string, userId: string): Promise<Connection> {
+ static async getConnectionByConnectorIdAndUserId(tenantID: string, connectorId: string, userId: string, projectFields?: string[]): Promise<Connection> {
const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'getConnectionByConnectorIdAndUserId');
await Utils.checkTenant(tenantID);
const aggregation = [];
@@ -44,6 +44,8 @@ export default class ConnectionStorage {
DatabaseUtils.pushConvertObjectIDToString(aggregation, 'userId');
// Handle the ID
DatabaseUtils.pushRenameDatabaseID(aggregation);
+ // Project
+ DatabaseUtils.projectFields(aggregation, projectFields);
// Exec
const connections = await global.database.getCollection<any>(tenantID, 'connections')
.aggregate(aggregation)
@@ -56,7 +58,7 @@ export default class ConnectionStorage {
return connection;
}
- static async getConnectionsByUserId(tenantID: string, userID: string): Promise<DataResult<Connection>> {
+ static async getConnectionsByUserId(tenantID: string, userID: string, projectFields?: string[]): Promise<DataResult<Connection>> {
const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'getConnectionsByUserId');
await Utils.checkTenant(tenantID);
const aggregation = [];
@@ -67,6 +69,8 @@ export default class ConnectionStorage {
DatabaseUtils.pushConvertObjectIDToString(aggregation, 'userId');
// Handle the ID
DatabaseUtils.pushRenameDatabaseID(aggregation);
+ // Project
+ DatabaseUtils.projectFields(aggregation, projectFields);
// Get connections
const connectionsMDB = await global.database.getCollection<Connection>(tenantID, 'connections')
.aggregate(aggregation, { collation: { locale: Constants.DEFAULT_LOCALE, strength: 2 }, allowDiskUse: true })
@@ -78,7 +82,7 @@ export default class ConnectionStorage {
};
}
- static async getConnection(tenantID: string, id: string = Constants.UNKNOWN_OBJECT_ID): Promise<Connection> {
+ static async getConnection(tenantID: string, id: string = Constants.UNKNOWN_OBJECT_ID, projectFields?: string[]): Promise<Connection> {
const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'getConnection');
await Utils.checkTenant(tenantID);
const aggregation = [];
@@ -90,6 +94,8 @@ export default class ConnectionStorage {
DatabaseUtils.pushConvertObjectIDToString(aggregation, 'userId');
// Handle the ID
DatabaseUtils.pushRenameDatabaseID(aggregation);
+ // Project
+ DatabaseUtils.projectFields(aggregation, projectFields);
// Exec
const connections = await global.database.getCollection<Connection>(tenantID, 'connections')
.aggregate(aggregation)

Optimized Connections