Skip to content

Commit

Permalink
Commands for managing appservice users.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gnuxie committed Apr 1, 2023
1 parent 4ba052b commit f5fcdbb
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/appservice/AccessControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
Expand Down Expand Up @@ -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<void> {
await this.accessControlList.createPolicy(EntityType.RULE_USER, Recommendation.Allow, mxid);
}

public async remove(mxid: string): Promise<void> {
await this.accessControlList.unbanEntity(EntityType.RULE_USER, mxid);
}
}
2 changes: 1 addition & 1 deletion src/appservice/AppService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
54 changes: 54 additions & 0 deletions src/appservice/bot/AccessCommands.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright (C) 2022 Gnuxie <[email protected]>
* 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<CommandResult<void>> {
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<CommandResult<void>> {
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,
});
1 change: 1 addition & 0 deletions src/appservice/bot/AppserviceCommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type AppserviceBaseExecutor = (this: AppserviceContext, ...args: any[]) =

import '../../commands/interface-manager/MatrixPresentations';
import './ListCommand';
import './AccessCommands';
import { AppserviceBotEmitter } from './AppserviceBotEmitter';


Expand Down

0 comments on commit f5fcdbb

Please sign in to comment.