Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 2s
61 lines
2.0 KiB
TypeScript
61 lines
2.0 KiB
TypeScript
import { isBannedSubscription } from '@rocket.chat/core-typings';
|
|
import { Rooms, Subscriptions, Users, Roles } from '@rocket.chat/models';
|
|
|
|
import { canAccessRoomAsync } from './authorization';
|
|
import { hasPermissionAsync } from './authorization/hasPermission';
|
|
import { hasRoleAsync } from './authorization/hasRole';
|
|
import { banUserFromRoom } from './rooms/banUserFromRoom';
|
|
import { roomCoordinator } from './rooms/roomCoordinator';
|
|
import { RoomMemberActions } from '../../definition/IRoomTypeConfig';
|
|
|
|
export const banUserFromRoomMethod = async (fromId: string, data: { rid: string; username: string }): Promise<boolean> => {
|
|
if (!(await hasPermissionAsync(fromId, 'ban-user', data.rid))) {
|
|
throw new Error('Not allowed');
|
|
}
|
|
|
|
const room = await Rooms.findOneById(data.rid);
|
|
|
|
if (!room || !(await roomCoordinator.getRoomDirectives(room.t).allowMemberAction(room, RoomMemberActions.BAN, fromId))) {
|
|
throw new Error('Not allowed');
|
|
}
|
|
|
|
const fromUser = await Users.findOneById(fromId);
|
|
if (!fromUser) {
|
|
throw new Error('Invalid user');
|
|
}
|
|
|
|
if (!(await canAccessRoomAsync(room, fromUser))) {
|
|
throw new Error('The required "roomId" or "roomName" param provided does not match any group');
|
|
}
|
|
|
|
const bannedUser = await Users.findOneByUsernameIgnoringCase(data.username);
|
|
if (!bannedUser) {
|
|
throw new Error('User not found');
|
|
}
|
|
|
|
const subscription = await Subscriptions.findOneByRoomIdAndUserId(data.rid, bannedUser._id, {
|
|
projection: { _id: 1, status: 1 },
|
|
});
|
|
if (!subscription) {
|
|
throw new Error('User is not in this room');
|
|
}
|
|
|
|
// Cannot ban a user who is already banned
|
|
if (isBannedSubscription(subscription)) {
|
|
throw new Error('User is already banned from this room');
|
|
}
|
|
|
|
// Cannot ban the last owner
|
|
if (await hasRoleAsync(bannedUser._id, 'owner', room._id)) {
|
|
const numOwners = await Roles.countUsersInRole('owner', room._id);
|
|
|
|
if (numOwners === 1) {
|
|
throw new Error('You are the last owner. Please set new owner before banning the user.');
|
|
}
|
|
}
|
|
|
|
await banUserFromRoom(data.rid, bannedUser, fromUser);
|
|
|
|
return true;
|
|
};
|