backend: Refactor recording middleware to improve permission checks and error handling

This commit is contained in:
juancarmore 2025-03-21 01:33:37 +01:00
parent e383d10fd6
commit 1dba73178d

View File

@ -7,37 +7,44 @@ import { RoomService } from '../services/room.service.js';
export const withRecordingEnabledAndCorrectPermissions = async (req: Request, res: Response, next: NextFunction) => {
const logger = container.get(LoggerService);
// TODO: Think how to get the roomName from the request
const roomName = req.body.roomName;
const payload = req.session?.tokenClaims;
if (!payload) {
return res.status(403).json({ message: 'Insufficient permissions to access this resource' });
}
let room: OpenViduMeetRoom;
try {
// TODO: Think how to get the roomName from the request
const roomName = req.body.roomName;
const payload = req.body.payload;
const roomService = container.get(RoomService);
const room: OpenViduMeetRoom = await roomService.getOpenViduRoom(roomName);
console.log('room', room);
if (!room.preferences) {
logger.error('No room preferences found checking recording preferences. Refusing access');
return res.status(403).json({ message: 'Recording is disabled in this room' });
}
const { recordingPreferences } = room.preferences;
const { enabled: recordingEnabled } = recordingPreferences;
if (!recordingEnabled) {
return res.status(403).json({ message: 'Recording is disabled in this room' });
}
const sameRoom = payload.video.room === roomName;
const permissions = payload.metadata?.permissions as OpenViduMeetPermissions;
const canRecord = permissions?.canRecord === true;
if (!sameRoom || !canRecord) {
return res.status(403).json({ message: 'Insufficient permissions to record in this room' });
}
return next();
room = await roomService.getOpenViduRoom(roomName);
} catch (error) {
logger.error('Error checking recording preferences:' + error);
return res.status(403).json({ message: 'Recording is disabled in this room' });
}
if (!room.preferences) {
logger.error('No room preferences found checking recording preferences. Refusing access');
return res.status(403).json({ message: 'Recording is disabled in this room' });
}
const { recordingPreferences } = room.preferences;
const { enabled: recordingEnabled } = recordingPreferences;
if (!recordingEnabled) {
return res.status(403).json({ message: 'Recording is disabled in this room' });
}
const sameRoom = payload.video?.room === roomName;
const metadata = JSON.parse(payload.metadata || '{}');
const permissions = metadata.permissions as OpenViduMeetPermissions | undefined;
const canRecord = permissions?.canRecord === true;
if (!sameRoom || !canRecord) {
return res.status(403).json({ message: 'Insufficient permissions to access this resource' });
}
return next();
};