feat: ACL 관련 정리

This commit is contained in:
2024-03-09 15:07:10 +00:00
parent 5928655bc1
commit 99238636e6
4 changed files with 103 additions and 11 deletions
@@ -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<Q extends GatewayLoginCtx & SessionCtx>(...actions: GatewayActionType[]): ()=>ProcDecorator<Q, Q> {
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];
}
}
}
@@ -10,11 +10,11 @@ export type GatewayLoginCtx = {
allowGatewayAction: Set<GatewayActionType>;
loginDate: Date;
}
export const loginCtxSessionKey = 'loginCtx';
export const gatewayLoginCtxSessionKey = 'gatewayLoginCtx';
export function ReqGatewayLogin<Q extends SessionCtx>(): ProcDecorator<GatewayLoginCtx & Q, Q> {
return (ctx) => {
const loginCtx = ctx.session.getItem<GatewayLoginCtx>(loginCtxSessionKey);
const loginCtx = ctx.session.getItem<GatewayLoginCtx>(gatewayLoginCtxSessionKey);
if(!loginCtx){
return [{
result: false,
@@ -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<Q extends GatewayLoginCtx & SessionCtx>(serverName: string, ...actions: ServerActionType[]): ()=>ProcDecorator<Q, Q> {
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];
}
}
}
+26 -9
View File
@@ -11,18 +11,35 @@ export type LoginCtx = {
loginDate: Date;
}
export function ReqACL<Q extends LoginCtx & SessionCtx>(action: ServerActionType): ProcDecorator<Q, Q> {
return (ctx) => {
if(ctx.allowServerAction?.has(action)){
export function ReqACL<Q extends LoginCtx & SessionCtx>(...actions: ServerActionType[]): ()=>ProcDecorator<Q, Q> {
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];
}
}