-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(audit): New endpoint for listing members of any room (#32916)
- Loading branch information
Showing
9 changed files
with
425 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@rocket.chat/meteor": minor | ||
"@rocket.chat/i18n": minor | ||
--- | ||
|
||
Added a new Audit endpoint `audit/rooms.members` that allows users with `view-members-list-all-rooms` to fetch a list of the members of any room even if the user is not part of it. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import type { IUser, IRoom } from '@rocket.chat/core-typings'; | ||
import { Rooms, AuditLog } from '@rocket.chat/models'; | ||
import type { PaginatedRequest, PaginatedResult } from '@rocket.chat/rest-typings'; | ||
import Ajv from 'ajv'; | ||
|
||
import { API } from '../../../app/api/server/api'; | ||
import { getPaginationItems } from '../../../app/api/server/helpers/getPaginationItems'; | ||
import { findUsersOfRoom } from '../../../server/lib/findUsersOfRoom'; | ||
|
||
const ajv = new Ajv({ | ||
coerceTypes: true, | ||
}); | ||
|
||
type AuditRoomMembersParams = PaginatedRequest<{ | ||
roomId: string; | ||
filter: string; | ||
}>; | ||
|
||
const auditRoomMembersSchema = { | ||
type: 'object', | ||
properties: { | ||
roomId: { type: 'string', minLength: 1 }, | ||
filter: { type: 'string' }, | ||
count: { type: 'number' }, | ||
offset: { type: 'number' }, | ||
sort: { type: 'string' }, | ||
}, | ||
required: ['roomId'], | ||
additionalProperties: false, | ||
}; | ||
|
||
export const isAuditRoomMembersProps = ajv.compile<AuditRoomMembersParams>(auditRoomMembersSchema); | ||
|
||
declare module '@rocket.chat/rest-typings' { | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
interface Endpoints { | ||
'/v1/audit/rooms.members': { | ||
GET: ( | ||
params: AuditRoomMembersParams, | ||
) => PaginatedResult<{ members: Pick<IUser, '_id' | 'name' | 'username' | 'status' | '_updatedAt'>[] }>; | ||
}; | ||
} | ||
} | ||
|
||
API.v1.addRoute( | ||
'audit/rooms.members', | ||
{ authRequired: true, permissionsRequired: ['view-members-list-all-rooms'], validateParams: isAuditRoomMembersProps }, | ||
{ | ||
async get() { | ||
const { roomId, filter } = this.queryParams; | ||
const { count: limit, offset: skip } = await getPaginationItems(this.queryParams); | ||
const { sort } = await this.parseJsonQuery(); | ||
|
||
const room = await Rooms.findOneById<Pick<IRoom, '_id' | 'name' | 'fname'>>(roomId, { projection: { _id: 1, name: 1, fname: 1 } }); | ||
if (!room) { | ||
return API.v1.notFound(); | ||
} | ||
|
||
const { cursor, totalCount } = findUsersOfRoom({ | ||
rid: room._id, | ||
filter, | ||
skip, | ||
limit, | ||
...(sort?.username && { sort: { username: sort.username } }), | ||
}); | ||
|
||
const [members, total] = await Promise.all([cursor.toArray(), totalCount]); | ||
|
||
await AuditLog.insertOne({ | ||
ts: new Date(), | ||
results: total, | ||
u: { | ||
_id: this.user._id, | ||
username: this.user.username, | ||
name: this.user.name, | ||
avatarETag: this.user.avatarETag, | ||
}, | ||
fields: { | ||
msg: 'Room_members_list', | ||
rids: [room._id], | ||
type: 'room_member_list', | ||
room: room.name || room.fname, | ||
filters: filter, | ||
}, | ||
}); | ||
|
||
return API.v1.success({ | ||
members, | ||
count: members.length, | ||
offset: skip, | ||
total, | ||
}); | ||
}, | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.