diff --git a/@sammo/gateway_server/src/procDecorator/ReqGatewayACL.ts b/@sammo/gateway_server/src/procDecorator/ReqGatewayACL.ts new file mode 100644 index 0000000..20727ba --- /dev/null +++ b/@sammo/gateway_server/src/procDecorator/ReqGatewayACL.ts @@ -0,0 +1,37 @@ +import type { SessionCtx } from "@sammo/server_util"; +import type { ProcDecorator } from "@strpc/express/proc_decorator"; +import type { GatewayLoginCtx } from "./ReqGatewayLogin.js"; +import type { GatewayActionType } from "@/schema/User.js"; + +export function ReqGatewayACL(...actions: GatewayActionType[]): ()=>ProcDecorator { + return () => { + return (ctx) => { + if (actions.length === 0) { + return [{ + result: true, + }, ctx]; + } + if(!ctx.allowGatewayAction){ + return [{ + result: false, + type: 'Required ACL', + info: 'No Gateway ACL in session context.' + }, ctx]; + } + + for (const action of actions) { + if (!ctx.allowGatewayAction.has(action)) { + return [{ + result: false, + type: 'Required ACL', + info: `NotEnoughPermission: ${action}` + }, ctx]; + } + } + + return [{ + result: true, + }, ctx]; + } + } +} \ No newline at end of file diff --git a/@sammo/gateway_server/src/procDecorator/ReqGatewayLogin.ts b/@sammo/gateway_server/src/procDecorator/ReqGatewayLogin.ts index 27cadef..a9f421e 100644 --- a/@sammo/gateway_server/src/procDecorator/ReqGatewayLogin.ts +++ b/@sammo/gateway_server/src/procDecorator/ReqGatewayLogin.ts @@ -10,11 +10,11 @@ export type GatewayLoginCtx = { allowGatewayAction: Set; loginDate: Date; } -export const loginCtxSessionKey = 'loginCtx'; +export const gatewayLoginCtxSessionKey = 'gatewayLoginCtx'; export function ReqGatewayLogin(): ProcDecorator { return (ctx) => { - const loginCtx = ctx.session.getItem(loginCtxSessionKey); + const loginCtx = ctx.session.getItem(gatewayLoginCtxSessionKey); if(!loginCtx){ return [{ result: false, diff --git a/@sammo/gateway_server/src/procDecorator/ReqServerACL.ts b/@sammo/gateway_server/src/procDecorator/ReqServerACL.ts new file mode 100644 index 0000000..84c62d4 --- /dev/null +++ b/@sammo/gateway_server/src/procDecorator/ReqServerACL.ts @@ -0,0 +1,38 @@ +import type { SessionCtx } from "@sammo/server_util"; +import type { ProcDecorator } from "@strpc/express/proc_decorator"; +import type { ServerActionType } from "@sammo/gateway_server/exports" +import type { GatewayLoginCtx } from "./ReqGatewayLogin.js"; + +export function ReqServerACL(serverName: string, ...actions: ServerActionType[]): ()=>ProcDecorator { + return () => { + return (ctx) => { + if (actions.length === 0) { + return [{ + result: true, + }, ctx]; + } + const allowServerAction = ctx.allowServerAction.get(serverName); + if(!allowServerAction){ + return [{ + result: false, + type: 'Required ACL', + info: 'No Server ACL in session context.' + }, ctx]; + } + + for (const action of actions) { + if (!allowServerAction.has(action)) { + return [{ + result: false, + type: 'Required ACL', + info: `NotEnoughPermission: ${action}` + }, ctx]; + } + } + + return [{ + result: true, + }, ctx]; + } + } +} \ No newline at end of file diff --git a/@sammo/server/src/procDecorator/ReqACL.ts b/@sammo/server/src/procDecorator/ReqACL.ts index bea1d9b..b23464e 100644 --- a/@sammo/server/src/procDecorator/ReqACL.ts +++ b/@sammo/server/src/procDecorator/ReqACL.ts @@ -11,18 +11,35 @@ export type LoginCtx = { loginDate: Date; } -export function ReqACL(action: ServerActionType): ProcDecorator { - return (ctx) => { - if(ctx.allowServerAction?.has(action)){ +export function ReqACL(...actions: ServerActionType[]): ()=>ProcDecorator { + return () => { + return (ctx) => { + if (actions.length === 0) { + return [{ + result: true, + }, ctx]; + } + if(!ctx.allowServerAction){ + return [{ + result: false, + type: 'Required ACL', + info: 'No Server ACL in session context.' + }, ctx]; + } + + for (const action of actions) { + if (!ctx.allowServerAction.has(action)) { + return [{ + result: false, + type: 'Required ACL', + info: `NotEnoughPermission: ${action}` + }, ctx]; + } + } + return [{ result: true, }, ctx]; } - - return [{ - result: false, - type: 'Required ACL', - info: `NotEnoughPermission: ${action}` - }, ctx]; } } \ No newline at end of file