refactor(line): share command authorization gate logic

This commit is contained in:
Peter Steinberger
2026-03-07 16:23:54 +00:00
parent f7fef07725
commit b3fd537740

View File

@@ -28,6 +28,7 @@ import {
isSenderAllowed, isSenderAllowed,
normalizeAllowFrom, normalizeAllowFrom,
normalizeDmAllowFromWithStore, normalizeDmAllowFromWithStore,
type NormalizedAllowFrom,
} from "./bot-access.js"; } from "./bot-access.js";
import { import {
getLineSourceInfo, getLineSourceInfo,
@@ -350,17 +351,15 @@ async function shouldProcessLineEvent(
return denied; return denied;
} }
} }
const allowForCommands = effectiveGroupAllow; return {
const senderAllowedForCommands = isSenderAllowed({ allow: allowForCommands, senderId }); allowed: true,
const useAccessGroups = cfg.commands?.useAccessGroups !== false; commandAuthorized: resolveLineCommandAuthorized({
const rawText = resolveEventRawText(event); cfg,
const commandGate = resolveControlCommandGate({ event,
useAccessGroups, senderId,
authorizers: [{ configured: allowForCommands.hasEntries, allowed: senderAllowedForCommands }], allow: effectiveGroupAllow,
allowTextCommands: true, }),
hasControlCommand: hasControlCommand(rawText, cfg), };
});
return { allowed: true, commandAuthorized: commandGate.commandAuthorized };
} }
if (dmPolicy === "disabled") { if (dmPolicy === "disabled") {
@@ -386,17 +385,15 @@ async function shouldProcessLineEvent(
return denied; return denied;
} }
const allowForCommands = effectiveDmAllow; return {
const senderAllowedForCommands = isSenderAllowed({ allow: allowForCommands, senderId }); allowed: true,
const useAccessGroups = cfg.commands?.useAccessGroups !== false; commandAuthorized: resolveLineCommandAuthorized({
const rawText = resolveEventRawText(event); cfg,
const commandGate = resolveControlCommandGate({ event,
useAccessGroups, senderId,
authorizers: [{ configured: allowForCommands.hasEntries, allowed: senderAllowedForCommands }], allow: effectiveDmAllow,
allowTextCommands: true, }),
hasControlCommand: hasControlCommand(rawText, cfg), };
});
return { allowed: true, commandAuthorized: commandGate.commandAuthorized };
} }
function resolveEventRawText(event: MessageEvent | PostbackEvent): string { function resolveEventRawText(event: MessageEvent | PostbackEvent): string {
@@ -413,6 +410,27 @@ function resolveEventRawText(event: MessageEvent | PostbackEvent): string {
return ""; return "";
} }
function resolveLineCommandAuthorized(params: {
cfg: OpenClawConfig;
event: MessageEvent | PostbackEvent;
senderId?: string;
allow: NormalizedAllowFrom;
}): boolean {
const senderAllowedForCommands = isSenderAllowed({
allow: params.allow,
senderId: params.senderId,
});
const useAccessGroups = params.cfg.commands?.useAccessGroups !== false;
const rawText = resolveEventRawText(params.event);
const commandGate = resolveControlCommandGate({
useAccessGroups,
authorizers: [{ configured: params.allow.hasEntries, allowed: senderAllowedForCommands }],
allowTextCommands: true,
hasControlCommand: hasControlCommand(rawText, params.cfg),
});
return commandGate.commandAuthorized;
}
async function handleMessageEvent(event: MessageEvent, context: LineHandlerContext): Promise<void> { async function handleMessageEvent(event: MessageEvent, context: LineHandlerContext): Promise<void> {
const { cfg, account, runtime, mediaMaxBytes, processMessage } = context; const { cfg, account, runtime, mediaMaxBytes, processMessage } = context;
const message = event.message; const message = event.message;