Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Allow integration managers to remove users #9211

Merged
merged 19 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
236 changes: 236 additions & 0 deletions cypress/e2e/integration-manager/kick.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
/*
Copyright 2022 The Matrix.org Foundation C.I.C.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/// <reference types="cypress" />

import { SynapseInstance } from "../../plugins/synapsedocker";
import { MatrixClient } from "../../global";
import { UserCredentials } from "../../support/login";

const ROOM_NAME = "Integration Manager Test";

const INTEGRATION_MANAGER_TOKEN = "Sr_oU-B-Cir_Tx573Ugr";
justinbot marked this conversation as resolved.
Show resolved Hide resolved
const INTEGRATION_MANAGER_HTML = `
<html lang="en">
<head>
<title>Fake Integration Manager</title>
</head>
<body>
<input type="text" id="target-room-id"/>
<input type="text" id="target-user-id"/>
<button name="Send" id="send-action">Press to send action</button>
<script>
document.getElementById("send-action").onclick = () => {
window.parent.postMessage(
{
action: "kick",
room_id: document.getElementById("target-room-id").value,
user_id: document.getElementById("target-user-id").value,
reason: "Removed from room",
},
'*',
);
};
</script>
</body>
</html>
`;

function openIntegrationManager() {
cy.get(".mx_RightPanel_roomSummaryButton").click();
cy.get(".mx_RoomSummaryCard_appsGroup").within(() => {
cy.contains("Add widgets, bridges & bots").click();
});
}

function sendActionFromIntegrationManager(integrationManagerUrl: string, targetRoomId: string, targetUserId: string) {
cy.accessIframe(`iframe[src*="${integrationManagerUrl}"]`).within(() => {
cy.get("#target-room-id").should("exist").type(targetRoomId);
cy.get("#target-user-id").should("exist").type(targetUserId);
cy.get("#send-action").should("exist").click();
});
// Wait for the message to be handled
return cy.wait(100);
}

describe("Kick", () => {
justinbot marked this conversation as resolved.
Show resolved Hide resolved
let testUser: UserCredentials;
let synapse: SynapseInstance;
let integrationManagerUrl: string;

beforeEach(() => {
cy.serveHtmlFile(INTEGRATION_MANAGER_HTML).then(url => {
integrationManagerUrl = url;
});
cy.startSynapse("default").then(data => {
synapse = data;

cy.initTestUser(synapse, "Alice", () => {
cy.window().then(win => {
win.localStorage.setItem("mx_scalar_token", INTEGRATION_MANAGER_TOKEN);
win.localStorage.setItem(`mx_scalar_token_at_${integrationManagerUrl}`, INTEGRATION_MANAGER_TOKEN);
});
}).then(user => {
testUser = user;
});

cy.setAccountData("m.widgets", {
"m.integration_manager": {
content: {
type: "m.integration_manager",
name: "Integration Manager",
url: integrationManagerUrl,
data: {
api_url: integrationManagerUrl,
},
},
id: "integration-manager",
},
}).as("integrationManager");

// Succeed when checking the token is valid
cy.intercept(`${integrationManagerUrl}/account?scalar_token=${INTEGRATION_MANAGER_TOKEN}*`, req => {
req.continue(res => {
return res.send(200, {
user_id: testUser.userId,
});
});
});

cy.createRoom({
name: ROOM_NAME,
}).as("roomId");

cy.getBot(synapse, { displayName: "Bob", autoAcceptInvites: true }).as("bob");
});
});

afterEach(() => {
cy.stopSynapse(synapse);
cy.stopWebServers();
});

it("should kick the target", () => {
cy.all([
cy.get<MatrixClient>("@bob"),
cy.get<string>("@roomId"),
cy.get<{}>("@integrationManager"),
]).then(([targetUser, roomId]) => {
const targetUserId = targetUser.getUserId();
cy.viewRoomByName(ROOM_NAME);
cy.inviteUser(roomId, targetUserId);

openIntegrationManager();
sendActionFromIntegrationManager(integrationManagerUrl, roomId, targetUserId);

cy.getClient().then(client => {
expect(client.getRoom(roomId).getMember(targetUserId).isKicked()).to.be.true;
});
});
});

it("should not kick the target if lacking permissions", () => {
cy.all([
cy.get<MatrixClient>("@bob"),
cy.get<string>("@roomId"),
cy.get<{}>("@integrationManager"),
]).then(([targetUser, roomId]) => {
const targetUserId = targetUser.getUserId();
cy.viewRoomByName(ROOM_NAME);
cy.inviteUser(roomId, targetUserId);
cy.getClient().then(async client => {
await client.sendStateEvent(roomId, 'm.room.power_levels', {
kick: 50,
users: {
[testUser.userId]: 0,
},
});
}).then(() => {
openIntegrationManager();
sendActionFromIntegrationManager(integrationManagerUrl, roomId, targetUserId);

cy.getClient().then(client => {
expect(client.getRoom(roomId).getMember(targetUserId).isKicked()).to.be.false;
});
});
});
});

it("should no-op if the target already left", () => {
cy.all([
cy.get<MatrixClient>("@bob"),
cy.get<string>("@roomId"),
cy.get<{}>("@integrationManager"),
]).then(([targetUser, roomId]) => {
const targetUserId = targetUser.getUserId();
cy.viewRoomByName(ROOM_NAME);
cy.inviteUser(roomId, targetUserId);
// Wait for target to join
cy.wait(100).then(async () => {
justinbot marked this conversation as resolved.
Show resolved Hide resolved
await targetUser.leave(roomId);
}).then(() => {
openIntegrationManager();
sendActionFromIntegrationManager(integrationManagerUrl, roomId, targetUserId);

cy.getClient().then(client => {
expect(client.getRoom(roomId).getMember(targetUserId).isKicked()).to.be.false;
});
});
});
});

it("should no-op if the target was banned", () => {
cy.all([
cy.get<MatrixClient>("@bob"),
cy.get<string>("@roomId"),
cy.get<{}>("@integrationManager"),
]).then(([targetUser, roomId]) => {
const targetUserId = targetUser.getUserId();
cy.viewRoomByName(ROOM_NAME);
cy.inviteUser(roomId, targetUserId);
// Wait for target to join
cy.wait(100);
cy.getClient().then(async client => {
await client.ban(roomId, targetUserId);
}).then(() => {
openIntegrationManager();
sendActionFromIntegrationManager(integrationManagerUrl, roomId, targetUserId);

cy.getClient().then(async client => {
expect(client.getRoom(roomId).getMember(targetUserId).membership).to.eq('ban');
});
});
});
});

it("should no-op if the target was never a room member", () => {
cy.all([
cy.get<MatrixClient>("@bob"),
cy.get<string>("@roomId"),
cy.get<{}>("@integrationManager"),
]).then(([targetUser, roomId]) => {
const targetUserId = targetUser.getUserId();
cy.viewRoomByName(ROOM_NAME);

openIntegrationManager();
sendActionFromIntegrationManager(integrationManagerUrl, roomId, targetUserId);

cy.getClient().then(async client => {
expect(client.getRoom(roomId).getMember(targetUserId)).to.be.null;
});
});
});
});
59 changes: 58 additions & 1 deletion src/ScalarMessaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,29 @@ Example:
}
}

kick
------
Kicks a user from a room. The request will no-op if the user is not in the room.

Request:
- room_id is the room to kick the user from.
- user_id is the user ID to kick.
- reason is an optional string for the kick reason
Response:
{
success: true
}
Example:
{
action: "kick",
room_id: "!foo:bar",
user_id: "@target:example.org",
reason: "Removed from room",
response: {
success: true
}
}

set_bot_options
---------------
Set the m.room.bot.options state event for a bot user.
Expand Down Expand Up @@ -254,6 +277,7 @@ import { _t } from './languageHandler';
import { IntegrationManagers } from "./integrations/IntegrationManagers";
import { WidgetType } from "./widgets/WidgetType";
import { objectClone } from "./utils/objects";
import { EffectiveMembership, getEffectiveMembership } from './utils/membership';

enum Action {
CloseScalar = "close_scalar",
Expand All @@ -266,6 +290,7 @@ enum Action {
CanSendEvent = "can_send_event",
MembershipState = "membership_state",
invite = "invite",
Kick = "kick",
BotOptions = "bot_options",
SetBotOptions = "set_bot_options",
SetBotPower = "set_bot_power",
Expand Down Expand Up @@ -322,6 +347,35 @@ function inviteUser(event: MessageEvent<any>, roomId: string, userId: string): v
});
}

function kickUser(event: MessageEvent<any>, roomId: string, userId: string): void {
logger.log(`Received request to kick ${userId} from room ${roomId}`);
const client = MatrixClientPeg.get();
if (!client) {
sendError(event, _t("You need to be logged in."));
return;
}
const room = client.getRoom(roomId);
if (room) {
// if they are already not in the room we can resolve immediately.
const member = room.getMember(userId);
if (!member || getEffectiveMembership(member.membership) === EffectiveMembership.Leave) {
sendResponse(event, {
success: true,
});
return;
}
}

const reason = event.data.reason;
client.kick(roomId, userId, reason).then(() => {
sendResponse(event, {
success: true,
});
}).catch((err) => {
sendError(event, _t("You need to be able to kick users to do that."), err);
});
}

function setWidget(event: MessageEvent<any>, roomId: string): void {
const widgetId = event.data.widget_id;
let widgetType = event.data.type;
Expand Down Expand Up @@ -710,6 +764,9 @@ const onMessage = function(event: MessageEvent<any>): void {
case Action.invite:
inviteUser(event, roomId, userId);
break;
case Action.Kick:
kickUser(event, roomId, userId);
break;
case Action.BotOptions:
botOptions(event, roomId, userId);
break;
Expand All @@ -729,7 +786,7 @@ const onMessage = function(event: MessageEvent<any>): void {
};

let listenerCount = 0;
let openManagerUrl: string = null;
let openManagerUrl: string | null = null;

export function startListening(): void {
if (listenerCount === 0) {
Expand Down
1 change: 1 addition & 0 deletions src/i18n/strings/en_EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@
"Some invites couldn't be sent": "Some invites couldn't be sent",
"You need to be logged in.": "You need to be logged in.",
"You need to be able to invite users to do that.": "You need to be able to invite users to do that.",
"You need to be able to kick users to do that.": "You need to be able to kick users to do that.",
"Unable to create widget.": "Unable to create widget.",
"Missing roomId.": "Missing roomId.",
"Failed to send request.": "Failed to send request.",
Expand Down