73 lines
1.8 KiB
TypeScript
73 lines
1.8 KiB
TypeScript
|
import { Server, Socket } from "socket.io";
|
||
|
import { PermissionObject, PermissionType, PermissionSection, PermissionModule } from "../type/permissionTypes";
|
||
|
import PermissionHelper from "../helpers/permissionHelper";
|
||
|
import ForbiddenRequestException from "../exceptions/forbiddenRequestException";
|
||
|
import { SocketMap } from "../storage/socketMap";
|
||
|
|
||
|
export type EventResponseType = {
|
||
|
answer: any;
|
||
|
type:
|
||
|
| `package-${string}`
|
||
|
| `status-${string}:${string}`
|
||
|
| "display"
|
||
|
| "warning"
|
||
|
| "reload"
|
||
|
| "deleted"
|
||
|
| "action required";
|
||
|
room?: string;
|
||
|
};
|
||
|
|
||
|
type PermissionPass =
|
||
|
| {
|
||
|
type: PermissionType;
|
||
|
section: PermissionSection;
|
||
|
module?: PermissionModule;
|
||
|
}
|
||
|
| "admin";
|
||
|
|
||
|
export let handleEvent = (
|
||
|
permissions: PermissionPass,
|
||
|
handler: (...args: any[]) => Promise<EventResponseType>,
|
||
|
socket: Socket
|
||
|
) => {
|
||
|
return async (...args: any[]) => {
|
||
|
try {
|
||
|
const socketData = SocketMap.read(socket.id);
|
||
|
if (permissions == "admin") {
|
||
|
if (!socketData.isOwner && !socketData.permissions.admin) {
|
||
|
throw new ForbiddenRequestException(`missing admin permission`);
|
||
|
}
|
||
|
} else {
|
||
|
if (
|
||
|
!socketData.isOwner &&
|
||
|
!PermissionHelper.can(socketData.permissions, permissions.type, permissions.section, permissions.module)
|
||
|
) {
|
||
|
throw new ForbiddenRequestException(`missing required permission`);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
const { answer, type, room } = await handler(...args);
|
||
|
if (room === undefined || room == "") {
|
||
|
socket.emit(type, answer);
|
||
|
} else {
|
||
|
socket.to(room).emit(type, answer);
|
||
|
}
|
||
|
} catch (e) {
|
||
|
socket.emit("error", e.message);
|
||
|
}
|
||
|
};
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
socket.on(
|
||
|
"event",
|
||
|
{ permissions }
|
||
|
handleResponse(
|
||
|
async (data:any) => {
|
||
|
throw new Error("failed")
|
||
|
},
|
||
|
socket,
|
||
|
)
|
||
|
);
|
||
|
*/
|