Regression: Teams should not have same name as users (#21371)

Co-authored-by: Diego Sampaio <chinello@gmail.com>
pull/21373/head^2
Alan Sikora 4 years ago committed by GitHub
parent 5f2bd2c185
commit c616108b24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      app/channel-settings/server/functions/saveRoomName.js
  2. 19
      app/lib/server/functions/checkUsernameAvailability.js
  3. 2
      app/models/server/raw/Team.ts
  4. 2
      server/sdk/types/ITeamService.ts
  5. 6
      server/services/team/service.ts

@ -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);
};

@ -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 = [];
@ -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;
};

@ -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);
}

@ -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>>;

@ -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';
@ -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');
}
@ -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);
}

Loading…
Cancel
Save