added user permissions to rooms
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
import crypto from 'crypto';
|
||||
import { closeTunnelIfUnused } from "./tunnel.manager";
|
||||
|
||||
interface RoomPermissions {
|
||||
canControl: boolean;
|
||||
canManageQueue: boolean;
|
||||
}
|
||||
|
||||
interface RoomUser {
|
||||
id: string;
|
||||
username: string;
|
||||
@@ -8,6 +13,8 @@ interface RoomUser {
|
||||
isHost: boolean;
|
||||
isGuest: boolean;
|
||||
userId?: number;
|
||||
permissions?: RoomPermissions;
|
||||
ipAddress?: string;
|
||||
}
|
||||
|
||||
interface SourceContext {
|
||||
@@ -55,8 +62,14 @@ interface RoomData {
|
||||
exposed: boolean;
|
||||
publicUrl?: string;
|
||||
queue: QueueItem[];
|
||||
bannedIPs: Set<string>;
|
||||
}
|
||||
|
||||
export const DEFAULT_GUEST_PERMISSIONS: RoomPermissions = {
|
||||
canControl: false,
|
||||
canManageQueue: false
|
||||
};
|
||||
|
||||
const rooms = new Map<string, RoomData>();
|
||||
|
||||
export function generateRoomId(): string {
|
||||
@@ -77,13 +90,81 @@ export function createRoom(name: string, host: RoomUser, password?: string, expo
|
||||
metadata: null,
|
||||
exposed,
|
||||
publicUrl,
|
||||
queue: []
|
||||
queue: [],
|
||||
bannedIPs: new Set() // NUEVO
|
||||
};
|
||||
|
||||
rooms.set(roomId, room);
|
||||
return room;
|
||||
}
|
||||
|
||||
export function updateUserPermissions(roomId: string, userId: string, permissions: Partial<RoomPermissions>): boolean {
|
||||
const room = rooms.get(roomId);
|
||||
if (!room) return false;
|
||||
|
||||
const user: any = room.users.get(userId);
|
||||
if (!user) return false;
|
||||
|
||||
if (user.isHost) return false;
|
||||
|
||||
user.permissions = {
|
||||
...user.permissions,
|
||||
...permissions
|
||||
};
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
export function banUserIP(roomId: string, ipAddress: string): boolean {
|
||||
const room = rooms.get(roomId);
|
||||
if (!room) return false;
|
||||
|
||||
room.bannedIPs.add(ipAddress);
|
||||
|
||||
// Remover a todos los usuarios con esa IP
|
||||
Array.from(room.users.values()).forEach(user => {
|
||||
if (user.ipAddress === ipAddress) {
|
||||
removeUserFromRoom(roomId, user.id);
|
||||
}
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
export function unbanUserIP(roomId: string, ipAddress: string): boolean {
|
||||
const room = rooms.get(roomId);
|
||||
if (!room) return false;
|
||||
|
||||
return room.bannedIPs.delete(ipAddress);
|
||||
}
|
||||
|
||||
export function isIPBanned(roomId: string, ipAddress: string): boolean {
|
||||
const room = rooms.get(roomId);
|
||||
if (!room) return false;
|
||||
return room.bannedIPs.has(ipAddress);
|
||||
}
|
||||
|
||||
export function getBannedIPs(roomId: string): string[] {
|
||||
const room = rooms.get(roomId);
|
||||
if (!room) return [];
|
||||
return Array.from(room.bannedIPs);
|
||||
}
|
||||
|
||||
export function hasPermission(roomId: string, userId: string, permission: keyof RoomPermissions): boolean {
|
||||
const room = rooms.get(roomId);
|
||||
if (!room) return false;
|
||||
|
||||
const user = room.users.get(userId);
|
||||
if (!user) return false;
|
||||
|
||||
// El host siempre tiene todos los permisos
|
||||
if (user.isHost) return true;
|
||||
|
||||
// Si no tiene permisos definidos, usar defaults
|
||||
const userPerms = user.permissions || DEFAULT_GUEST_PERMISSIONS;
|
||||
return userPerms[permission] || false;
|
||||
}
|
||||
|
||||
export function getRoom(roomId: string): RoomData | null {
|
||||
return rooms.get(roomId) || null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user