Skip to content

Commit 91348ff

Browse files
authored
chore: deprecate getUserRoles saveCustomFields setReaction setUsername saveUserProfile setUserPassword methods (RocketChat#36146)
1 parent beba5fc commit 91348ff

35 files changed

Lines changed: 344 additions & 93 deletions

File tree

apps/meteor/app/api/server/v1/channels.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -466,11 +466,11 @@ API.v1.addRoute(
466466
return API.v1.forbidden();
467467
}
468468

469-
const moderators = (
470-
await Subscriptions.findByRoomIdAndRoles(findResult._id, ['moderator'], {
471-
projection: { u: 1 },
472-
}).toArray()
473-
).map((sub: ISubscription) => sub.u);
469+
const moderators = await Subscriptions.findByRoomIdAndRoles(findResult._id, ['moderator'], {
470+
projection: { u: 1, _id: 0 },
471+
})
472+
.map((sub) => sub.u)
473+
.toArray();
474474

475475
return API.v1.success({
476476
moderators,

apps/meteor/app/api/server/v1/groups.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,11 +1215,11 @@ API.v1.addRoute(
12151215
userId: this.userId,
12161216
});
12171217

1218-
const moderators = (
1219-
await Subscriptions.findByRoomIdAndRoles(findResult.rid, ['moderator'], {
1220-
projection: { u: 1 },
1221-
}).toArray()
1222-
).map((sub: any) => sub.u);
1218+
const moderators = await Subscriptions.findByRoomIdAndRoles(findResult.rid, ['moderator'], {
1219+
projection: { u: 1, _id: 0 },
1220+
})
1221+
.map((sub) => sub.u)
1222+
.toArray();
12231223

12241224
return API.v1.success({
12251225
moderators,

apps/meteor/app/api/server/v1/roles.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { api } from '@rocket.chat/core-services';
1+
import { api, Authorization } from '@rocket.chat/core-services';
22
import type { IRole } from '@rocket.chat/core-typings';
33
import { Roles, Users } from '@rocket.chat/models';
4-
import { isRoleAddUserToRoleProps, isRoleDeleteProps, isRoleRemoveUserFromRoleProps } from '@rocket.chat/rest-typings';
4+
import { ajv, isRoleAddUserToRoleProps, isRoleDeleteProps, isRoleRemoveUserFromRoleProps } from '@rocket.chat/rest-typings';
55
import { check, Match } from 'meteor/check';
66
import { Meteor } from 'meteor/meteor';
77

@@ -13,6 +13,7 @@ import { addUserToRole } from '../../../authorization/server/methods/addUserToRo
1313
import { apiDeprecationLogger } from '../../../lib/server/lib/deprecationWarningLogger';
1414
import { notifyOnRoleChanged } from '../../../lib/server/lib/notifyListener';
1515
import { settings } from '../../../settings/server/index';
16+
import type { ExtractRoutesFromAPI } from '../ApiClass';
1617
import { API } from '../api';
1718
import { getPaginationItems } from '../helpers/getPaginationItems';
1819
import { getUserFromParams } from '../helpers/getUserFromParams';
@@ -243,3 +244,43 @@ API.v1.addRoute(
243244
},
244245
},
245246
);
247+
248+
const rolesRoutes = API.v1.get(
249+
'roles.getUsersInPublicRoles',
250+
{
251+
authRequired: true,
252+
response: {
253+
200: ajv.compile<{
254+
users: {
255+
_id: string;
256+
username: string;
257+
roles: string[];
258+
}[];
259+
}>({
260+
type: 'object',
261+
properties: {
262+
users: {
263+
type: 'array',
264+
items: {
265+
type: 'object',
266+
properties: { _id: { type: 'string' }, username: { type: 'string' }, roles: { type: 'array', items: { type: 'string' } } },
267+
},
268+
},
269+
},
270+
}),
271+
},
272+
},
273+
274+
async () => {
275+
return API.v1.success({
276+
users: await Authorization.getUsersFromPublicRoles(),
277+
});
278+
},
279+
);
280+
281+
type RolesEndpoints = ExtractRoutesFromAPI<typeof rolesRoutes>;
282+
283+
declare module '@rocket.chat/rest-typings' {
284+
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-interface
285+
interface Endpoints extends RolesEndpoints {}
286+
}

apps/meteor/app/api/server/v1/rooms.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { isPrivateRoom, isPublicRoom } from '@rocket.chat/core-typings';
44
import { Messages, Rooms, Users, Uploads, Subscriptions } from '@rocket.chat/models';
55
import type { Notifications } from '@rocket.chat/rest-typings';
66
import {
7+
ajv,
78
isGETRoomsNameExists,
89
isRoomsImagesProps,
910
isRoomsMuteUnmuteUserProps,
@@ -23,6 +24,7 @@ import * as dataExport from '../../../../server/lib/dataExport';
2324
import { eraseRoom } from '../../../../server/lib/eraseRoom';
2425
import { findUsersOfRoomOrderedByRole } from '../../../../server/lib/findUsersOfRoomOrderedByRole';
2526
import { openRoom } from '../../../../server/lib/openRoom';
27+
import type { RoomRoles } from '../../../../server/lib/roles/getRoomRoles';
2628
import { hideRoomMethod } from '../../../../server/methods/hideRoom';
2729
import { muteUserInRoom } from '../../../../server/methods/muteUserInRoom';
2830
import { toggleFavoriteMethod } from '../../../../server/methods/toggleFavorite';
@@ -37,12 +39,14 @@ import { sendFileMessage } from '../../../file-upload/server/methods/sendFileMes
3739
import { syncRolePrioritiesForRoomIfRequired } from '../../../lib/server/functions/syncRolePrioritiesForRoomIfRequired';
3840
import { executeArchiveRoom } from '../../../lib/server/methods/archiveRoom';
3941
import { cleanRoomHistoryMethod } from '../../../lib/server/methods/cleanRoomHistory';
42+
import { executeGetRoomRoles } from '../../../lib/server/methods/getRoomRoles';
4043
import { leaveRoomMethod } from '../../../lib/server/methods/leaveRoom';
4144
import { executeUnarchiveRoom } from '../../../lib/server/methods/unarchiveRoom';
4245
import { applyAirGappedRestrictionsValidation } from '../../../license/server/airGappedRestrictionsWrapper';
4346
import type { NotificationFieldType } from '../../../push-notifications/server/methods/saveNotificationSettings';
4447
import { saveNotificationSettingsMethod } from '../../../push-notifications/server/methods/saveNotificationSettings';
4548
import { settings } from '../../../settings/server';
49+
import type { ExtractRoutesFromAPI } from '../ApiClass';
4650
import { API } from '../api';
4751
import { composeRoomWithLastMessage } from '../helpers/composeRoomWithLastMessage';
4852
import { getPaginationItems } from '../helpers/getPaginationItems';
@@ -1005,3 +1009,62 @@ API.v1.addRoute(
10051009
},
10061010
},
10071011
);
1012+
1013+
const isRoomGetRolesPropsSchema = {
1014+
type: 'object',
1015+
properties: {
1016+
rid: { type: 'string' },
1017+
},
1018+
additionalProperties: false,
1019+
required: ['rid'],
1020+
};
1021+
export const roomEndpoints = API.v1.get(
1022+
'rooms.roles',
1023+
{
1024+
authRequired: true,
1025+
query: ajv.compile<{
1026+
rid: string;
1027+
}>(isRoomGetRolesPropsSchema),
1028+
response: {
1029+
200: ajv.compile<{
1030+
roles: RoomRoles[];
1031+
}>({
1032+
type: 'object',
1033+
properties: {
1034+
roles: {
1035+
type: 'array',
1036+
items: {
1037+
type: 'object',
1038+
properties: {
1039+
rid: { type: 'string' },
1040+
u: {
1041+
type: 'object',
1042+
properties: { _id: { type: 'string' }, username: { type: 'string' } },
1043+
required: ['_id', 'username'],
1044+
},
1045+
roles: { type: 'array', items: { type: 'string' } },
1046+
},
1047+
required: ['rid', 'u', 'roles'],
1048+
},
1049+
},
1050+
},
1051+
required: ['roles'],
1052+
}),
1053+
},
1054+
},
1055+
async function () {
1056+
const { rid } = this.queryParams;
1057+
const roles = await executeGetRoomRoles(rid, this.userId);
1058+
1059+
return API.v1.success({
1060+
roles,
1061+
});
1062+
},
1063+
);
1064+
1065+
type RoomEndpoints = ExtractRoutesFromAPI<typeof roomEndpoints>;
1066+
1067+
declare module '@rocket.chat/rest-typings' {
1068+
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-interface
1069+
interface Endpoints extends RoomEndpoints {}
1070+
}

apps/meteor/app/api/server/v1/users.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { MeteorError, Team, api, Calendar } from '@rocket.chat/core-services';
2-
import type { IExportOperation, ILoginToken, IPersonalAccessToken, IUser, UserStatus } from '@rocket.chat/core-typings';
2+
import { type IExportOperation, type ILoginToken, type IPersonalAccessToken, type IUser, type UserStatus } from '@rocket.chat/core-typings';
33
import { Users, Subscriptions } from '@rocket.chat/models';
44
import {
55
isUserCreateParamsPOST,
@@ -176,7 +176,13 @@ API.v1.addRoute(
176176
twoFactorMethod: 'password',
177177
};
178178

179-
await executeSaveUserProfile.call(this as unknown as Meteor.MethodThisType, userData, this.bodyParams.customFields, twoFactorOptions);
179+
await executeSaveUserProfile.call(
180+
this as unknown as Meteor.MethodThisType,
181+
this.user,
182+
userData,
183+
this.bodyParams.customFields,
184+
twoFactorOptions,
185+
);
180186

181187
return API.v1.success({
182188
user: await Users.findOneById(this.userId, { projection: API.v1.defaultFieldsToExclude }),

apps/meteor/app/apps/server/bridges/rooms.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,9 @@ export class AppRoomBridge extends RoomBridge {
237237
}
238238

239239
private async getUsersByRoomIdAndSubscriptionRole(roomId: string, role: string): Promise<IUser[]> {
240-
const subs = (await Subscriptions.findByRoomIdAndRoles(roomId, [role], {
240+
const subs = await Subscriptions.findByRoomIdAndRoles<{ uid: string }>(roomId, [role], {
241241
projection: { uid: '$u._id', _id: 0 },
242-
}).toArray()) as unknown as {
243-
uid: string;
244-
}[];
242+
}).toArray();
245243
// Was this a bug?
246244
const users = await Users.findByIds(subs.map((user: { uid: string }) => user.uid)).toArray();
247245
const userConverter = this.orch.getConverters().get('users');

apps/meteor/app/lib/server/functions/saveUser/validateUserEditing.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export async function validateUserEditing(userId: IUser['_id'], userData: Update
4545
if (
4646
isEditingField(user.username, userData.username) &&
4747
!settings.get('Accounts_AllowUsernameChange') &&
48-
(!canEditOtherUserInfo || editingMyself)
48+
(!canEditOtherUserInfo || (editingMyself && user.username))
4949
) {
5050
throw new MeteorError('error-action-not-allowed', 'Edit username is not allowed', {
5151
method: 'insertOrUpdateUser',

apps/meteor/app/lib/server/lib/deprecationWarningLogger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Logger } from '@rocket.chat/logger';
22
import semver from 'semver';
33

4-
import { metrics } from '../../../metrics/server';
4+
import { metrics } from '../../../metrics/server/lib/metrics';
55

66
const deprecationLogger = new Logger('DeprecationWarning');
77

apps/meteor/app/lib/server/methods/getRoomRoles.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
import type { IRoom, ISubscription } from '@rocket.chat/core-typings';
1+
import type { IRoom } from '@rocket.chat/core-typings';
22
import type { ServerMethods } from '@rocket.chat/ddp-client';
33
import { Rooms } from '@rocket.chat/models';
44
import { check } from 'meteor/check';
55
import { Meteor } from 'meteor/meteor';
66

7+
import type { RoomRoles } from '../../../../server/lib/roles/getRoomRoles';
78
import { getRoomRoles } from '../../../../server/lib/roles/getRoomRoles';
89
import { canAccessRoomAsync } from '../../../authorization/server';
910
import { settings } from '../../../settings/server';
11+
import { methodDeprecationLogger } from '../lib/deprecationWarningLogger';
1012

1113
declare module '@rocket.chat/ddp-client' {
1214
// eslint-disable-next-line @typescript-eslint/naming-convention
1315
interface ServerMethods {
14-
getRoomRoles(rid: IRoom['_id']): ISubscription[];
16+
getRoomRoles(rid: IRoom['_id']): RoomRoles[];
1517
}
1618
}
1719

@@ -36,6 +38,7 @@ export const executeGetRoomRoles = async (rid: IRoom['_id'], fromUserId?: string
3638

3739
Meteor.methods<ServerMethods>({
3840
async getRoomRoles(rid) {
41+
methodDeprecationLogger.method('getRoomRoles', '8.0.0', 'Use the /v1/room.getRoles endpoint instead');
3942
const fromUserId = Meteor.userId();
4043

4144
return executeGetRoomRoles(rid, fromUserId);

apps/meteor/app/lib/server/methods/getUserRoles.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
import { Authorization } from '@rocket.chat/core-services';
2-
import type { IUser, IRocketChatRecord } from '@rocket.chat/core-typings';
2+
import type { IUser } from '@rocket.chat/core-typings';
33
import type { ServerMethods } from '@rocket.chat/ddp-client';
44
import { Meteor } from 'meteor/meteor';
55

6+
import { methodDeprecationLogger } from '../lib/deprecationWarningLogger';
7+
68
declare module '@rocket.chat/ddp-client' {
79
// eslint-disable-next-line @typescript-eslint/naming-convention
810
interface ServerMethods {
9-
getUserRoles(): (IRocketChatRecord & Pick<IUser, '_id' | 'roles' | 'username'>)[];
11+
getUserRoles(): Pick<IUser, '_id' | 'roles' | 'username'>[];
1012
}
1113
}
1214

1315
Meteor.methods<ServerMethods>({
1416
async getUserRoles() {
17+
methodDeprecationLogger.method(
18+
'getUserRoles',
19+
'8.0.0',
20+
'This method is deprecated and will be removed in the future. Use the /v1/roles.getUsersInPublicRoles endpoint instead.',
21+
);
22+
1523
if (!Meteor.userId()) {
1624
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'getUserRoles' });
1725
}

0 commit comments

Comments
 (0)