From f5fcdbbdd0f9b4f96ae087528a739a832f3ebf70 Mon Sep 17 00:00:00 2001 From: gnuxie Date: Sat, 1 Apr 2023 12:40:38 +0100 Subject: [PATCH] Commands for managing appservice users. --- src/appservice/AccessControl.ts | 9 ++++ src/appservice/AppService.ts | 2 +- src/appservice/bot/AccessCommands.tsx | 54 +++++++++++++++++++ .../bot/AppserviceCommandHandler.ts | 1 + 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 src/appservice/bot/AccessCommands.tsx diff --git a/src/appservice/AccessControl.ts b/src/appservice/AccessControl.ts index 4d64f897..4156a703 100644 --- a/src/appservice/AccessControl.ts +++ b/src/appservice/AccessControl.ts @@ -28,6 +28,7 @@ limitations under the License. import { Bridge } from "matrix-appservice-bridge"; import { Permalinks } from "../commands/interface-manager/Permalinks"; import AccessControlUnit, { EntityAccess } from "../models/AccessControlUnit"; +import { EntityType, Recommendation } from "../models/ListRule"; import PolicyList from "../models/PolicyList"; /** @@ -74,4 +75,12 @@ export class AccessControl { public getUserAccess(mxid: string): EntityAccess { return this.accessControlUnit.getAccessForUser(mxid, "CHECK_SERVER"); } + + public async allow(mxid: string): Promise { + await this.accessControlList.createPolicy(EntityType.RULE_USER, Recommendation.Allow, mxid); + } + + public async remove(mxid: string): Promise { + await this.accessControlList.unbanEntity(EntityType.RULE_USER, mxid); + } } diff --git a/src/appservice/AppService.ts b/src/appservice/AppService.ts index b483ebf6..f2687cc8 100644 --- a/src/appservice/AppService.ts +++ b/src/appservice/AppService.ts @@ -52,7 +52,7 @@ export class MjolnirAppService { public readonly config: IConfig, public readonly bridge: Bridge, public readonly mjolnirManager: MjolnirManager, - private readonly accessControl: AccessControl, + public readonly accessControl: AccessControl, private readonly dataStore: DataStore, ) { this.api = new Api(config.homeserver.url, mjolnirManager); diff --git a/src/appservice/bot/AccessCommands.tsx b/src/appservice/bot/AccessCommands.tsx new file mode 100644 index 00000000..25f96d3a --- /dev/null +++ b/src/appservice/bot/AccessCommands.tsx @@ -0,0 +1,54 @@ +/** + * Copyright (C) 2022 Gnuxie + * All rights reserved. + */ + +import { defineInterfaceCommand, findTableCommand } from "../../commands/interface-manager/InterfaceCommand"; +import { findPresentationType, parameters, ParsedKeywords } from "../../commands/interface-manager/ParameterParsing"; +import { CommandResult } from "../../commands/interface-manager/Validation"; +import { AppserviceContext } from "./AppserviceCommandHandler"; +import { UserID } from "matrix-bot-sdk"; +import { defineMatrixInterfaceAdaptor } from "../../commands/interface-manager/MatrixInterfaceAdaptor"; +import { tickCrossRenderer } from "../../commands/interface-manager/MatrixHelpRenderer"; + +defineInterfaceCommand({ + designator: ["allow"], + table: "appservice bot", + parameters: parameters([ + { + name: 'user', + acceptor: findPresentationType('UserID'), + } + ]), + command: async function (this: AppserviceContext, _keywords: ParsedKeywords, user: UserID): Promise> { + await this.appservice.accessControl.allow(user.toString()); + return CommandResult.Ok(undefined); + }, + summary: "Allow a user to provision themselves a draupnir using the appservice." +}) + +defineMatrixInterfaceAdaptor({ + interfaceCommand: findTableCommand("appservice bot", "allow"), + renderer: tickCrossRenderer, +}); + +defineInterfaceCommand({ + designator: ["remove"], + table: "appservice bot", + parameters: parameters([ + { + name: 'user', + acceptor: findPresentationType('UserID'), + } + ]), + command: async function (this: AppserviceContext, _keywords: ParsedKeywords, user: UserID): Promise> { + await this.appservice.accessControl.remove(user.toString()); + return CommandResult.Ok(undefined); + }, + summary: "Stop a user from using any provisioned draupnir in the appservice." +}) + +defineMatrixInterfaceAdaptor({ + interfaceCommand: findTableCommand("appservice bot", "remove"), + renderer: tickCrossRenderer, +}); diff --git a/src/appservice/bot/AppserviceCommandHandler.ts b/src/appservice/bot/AppserviceCommandHandler.ts index 6fdcdac3..2853a745 100644 --- a/src/appservice/bot/AppserviceCommandHandler.ts +++ b/src/appservice/bot/AppserviceCommandHandler.ts @@ -22,6 +22,7 @@ export type AppserviceBaseExecutor = (this: AppserviceContext, ...args: any[]) = import '../../commands/interface-manager/MatrixPresentations'; import './ListCommand'; +import './AccessCommands'; import { AppserviceBotEmitter } from './AppserviceBotEmitter';