Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:RocketChat/Rocket.Chat into remo…
Browse files Browse the repository at this point in the history
…ve_breadcrumbs

* 'develop' of github.com:RocketChat/Rocket.Chat:
  Regression: Teams should not have same name as users (#21371)
  • Loading branch information
gabriellsh committed Mar 31, 2021
2 parents 334ce05 + c616108 commit 3bac7cf
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
7 changes: 7 additions & 0 deletions app/channel-settings/server/functions/saveRoomName.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ import { Meteor } from 'meteor/meteor';
import { Rooms, Messages, Subscriptions, Integrations } from '../../../models';
import { roomTypes, getValidRoomName } from '../../../utils';
import { callbacks } from '../../../callbacks';
import { checkUsernameAvailability } from '../../../lib/server/functions';

const updateRoomName = (rid, displayName, isDiscussion) => {
if (isDiscussion) {
return Rooms.setFnameById(rid, displayName) && Subscriptions.updateFnameByRoomId(rid, displayName);
}
const slugifiedRoomName = getValidRoomName(displayName, rid);

// Check if the username is available
if (!checkUsernameAvailability(slugifiedRoomName)) {
throw new Meteor.Error('error-duplicate-handle', `A room, team or user with name '${ slugifiedRoomName }' already exists`, { function: 'RocketChat.updateRoomName', handle: slugifiedRoomName });
}

return Rooms.setNameById(rid, slugifiedRoomName, displayName) && Subscriptions.updateNameAndAlertByRoomId(rid, slugifiedRoomName, displayName);
};

Expand Down
19 changes: 15 additions & 4 deletions app/lib/server/functions/checkUsernameAvailability.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import s from 'underscore.string';

import { escapeRegExp } from '../../../../lib/escapeRegExp';
import { settings } from '../../../settings';
import { Team } from '../../../../server/sdk';

let usernameBlackList = [];

Expand All @@ -20,9 +21,19 @@ export const checkUsernameAvailability = function(username) {
return false;
}

return !Meteor.users.findOne({
username: {
$regex: toRegExp(username),
},
// Make sure no users are using this username
const existingUser = Meteor.users.findOne({
username: toRegExp(username),
}, { fields: { _id: 1 } });
if (existingUser) {
return false;
}

// Make sure no teams are using this username
const existingTeam = Promise.await(Team.getOneByName(toRegExp(username), { projection: { _id: 1 } }));
if (existingTeam) {
return false;
}

return true;
};
2 changes: 1 addition & 1 deletion app/models/server/raw/Team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class TeamRaw extends BaseRaw<T> {
}, options);
}

findOneByName(name: string, options?: FindOneOptions<T>): Promise<T | null> {
findOneByName(name: string | RegExp, options?: FindOneOptions<T>): Promise<T | null> {
return this.col.findOne({ name }, options);
}

Expand Down
2 changes: 1 addition & 1 deletion server/sdk/types/ITeamService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export interface ITeamService {
deleteByName(teamName: string): Promise<boolean>;
unsetTeamIdOfRooms(teamId: string): void;
getOneById(teamId: string, options?: FindOneOptions<ITeam>): Promise<ITeam | undefined>;
getOneByName(teamName: string, options?: FindOneOptions<ITeam>): Promise<ITeam | null>;
getOneByName(teamName: string | RegExp, options?: FindOneOptions<ITeam>): Promise<ITeam | null>;
getOneByMainRoomId(teamId: string): Promise<ITeam | null>;
getOneByRoomId(teamId: string): Promise<ITeam | undefined>;
getMatchingTeamRooms(teamId: string, rids: Array<string>): Promise<Array<string>>;
Expand Down
6 changes: 3 additions & 3 deletions server/services/team/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { IRoom } from '../../../definition/IRoom';
import { addUserToRoom } from '../../../app/lib/server/functions/addUserToRoom';
import { canAccessRoom } from '../authorization/canAccessRoom';
import { escapeRegExp } from '../../../lib/escapeRegExp';
import { checkUsernameAvailability } from '../../../app/lib/server/functions';

export class TeamService extends ServiceClass implements ITeamService {
protected name = 'team';
Expand Down Expand Up @@ -44,8 +45,7 @@ export class TeamService extends ServiceClass implements ITeamService {
}

async create(uid: string, { team, room = { name: team.name, extraData: {} }, members, owner }: ITeamCreateParams): Promise<ITeam> {
const existingTeam = await this.TeamModel.findOneByName(team.name, { projection: { _id: 1 } });
if (existingTeam) {
if (!checkUsernameAvailability(team.name)) {
throw new Error('team-name-already-exists');
}

Expand Down Expand Up @@ -604,7 +604,7 @@ export class TeamService extends ServiceClass implements ITeamService {
return this.TeamModel.findOneById(teamId, options);
}

async getOneByName(teamName: string, options?: FindOneOptions<ITeam>): Promise<ITeam | null> {
async getOneByName(teamName: string | RegExp, options?: FindOneOptions<ITeam>): Promise<ITeam | null> {
return this.TeamModel.findOneByName(teamName, options);
}

Expand Down

0 comments on commit 3bac7cf

Please sign in to comment.