From 99238636e60e51e1b51440233c9fd4745c6f0e41 Mon Sep 17 00:00:00 2001 From: Hide_D Date: Sat, 9 Mar 2024 15:07:10 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20ACL=20=EA=B4=80=EB=A0=A8=20=EC=A0=95?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/procDecorator/ReqGatewayACL.ts | 37 ++++++++++++++++++ .../src/procDecorator/ReqGatewayLogin.ts | 4 +- .../src/procDecorator/ReqServerACL.ts | 38 +++++++++++++++++++ @sammo/server/src/procDecorator/ReqACL.ts | 35 ++++++++++++----- 4 files changed, 103 insertions(+), 11 deletions(-) create mode 100644 @sammo/gateway_server/src/procDecorator/ReqGatewayACL.ts create mode 100644 @sammo/gateway_server/src/procDecorator/ReqServerACL.ts 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