Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression: team sync not accepting multiple teams #21768

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/ldap/server/ldap.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,14 @@ export default class LDAP {
values[key] = value;
}
}

if (key === 'ou' && Array.isArray(value)) {
value.forEach((item, index) => {
if (item instanceof Buffer) {
value[index] = item.toString();
}
});
}
});

return values;
Expand Down
9 changes: 7 additions & 2 deletions app/ldap/server/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,17 @@ export function mapLDAPGroupsToChannels(ldap, ldapUser, user) {

for (const channel of channels) {
let room = Rooms.findOneByNonValidatedName(channel);

if (!room) {
room = createRoomForSync(channel);
}
if (isUserInLDAPGroup(ldap, ldapUser, user, ldapField)) {
userChannels.push(room._id);
} else if (syncUserRolesEnforceAutoChannels) {
if (room.teamMain) {
logger.error(`Can't add user to channel ${ channel } because it is a team.`);
} else {
userChannels.push(room._id);
}
} else if (syncUserRolesEnforceAutoChannels && !room.teamMain) {
const subscription = Subscriptions.findOneByRoomIdAndUserId(room._id, user._id);
if (subscription) {
removeUserFromRoom(room._id, user);
Expand Down
3 changes: 2 additions & 1 deletion ee/app/ldap-enterprise/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import { onLicense } from '../../license/server';

onLicense('ldap-enterprise', () => {
const { createSettings } = require('./settings');
const { validateLDAPRolesMappingChanges } = require('./ldapEnterprise');
const { validateLDAPRolesMappingChanges, validateLDAPTeamsMappingChanges } = require('./ldapEnterprise');
const { onLdapLogin } = require('./listener');

Meteor.startup(function() {
createSettings();
validateLDAPRolesMappingChanges();
validateLDAPTeamsMappingChanges();

let LDAP_Enable_LDAP_Roles_To_RC_Roles;
let LDAP_Enable_LDAP_Groups_To_RC_Teams;
Expand Down
34 changes: 29 additions & 5 deletions ee/app/ldap-enterprise/server/ldapEnterprise.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ const validateLDAPRolesMappingStructure = (mappedRoles) => {
}
};

const validateLDAPTeamsMappingStructure = (mappedTeams) => {
const mappedRocketChatTeams = Object.values(mappedTeams);
const validStructureMapping = mappedRocketChatTeams.every(mustBeAnArrayOfStrings);
if (!validStructureMapping) {
throw new Error('Please verify your mapping for LDAP X RocketChat Teams. The structure is invalid, the structure should be an object like: {key: LdapTeam, value: [An array of rocket.chat teams]}');
}
};

export const getLdapRolesByUsername = (username, ldap) => {
const searchOptions = {
filter: settings.get('LDAP_Query_To_Get_User_Groups').replace(/#{username}/g, username),
Expand All @@ -43,9 +51,13 @@ export const getLdapTeamsByUsername = (username, ldap) => {
scope: ldap.options.User_Search_Scope || 'sub',
sizeLimit: ldap.options.Search_Size_Limit,
};
const getLdapTeams = (ldapUserGroups) => ldapUserGroups.filter((field) => field && field.ou).map((field) => field.ou);
const ldapUserGroups = ldap.searchAllSync(ldap.options.BaseDN, searchOptions);
return Array.isArray(ldapUserGroups) ? getLdapTeams(ldapUserGroups) : [];

if (!Array.isArray(ldapUserGroups)) {
return [];
}

return ldapUserGroups.filter((field) => field && field.ou).map((field) => field.ou).flat();
};

export const getRocketChatRolesByLdapRoles = (mappedRoles, ldapUserRoles) => {
Expand Down Expand Up @@ -79,16 +91,15 @@ export const getRocketChatTeamsByLdapTeams = (mappedTeams, ldapUserTeams) => {
return [];
}

const rcTeams = filteredTeams.map((ldapTeam) => mappedTeams[ldapTeam]);
return [...new Set(rcTeams)];
return [...new Set(filteredTeams.map((ldapTeam) => mappedTeams[ldapTeam]).flat())];
};

export const updateUserUsingMappedLdapRoles = (userId, roles) => {
Meteor.users.update({ _id: userId }, { $set: { roles } });
};

async function updateUserUsingMappedLdapTeamsAsync(userId, teamNames, map) {
const allTeamNames = [...new Set(Object.values(map))];
const allTeamNames = [...new Set(Object.values(map).flat())];
const allTeams = await Team.listByNames(allTeamNames, { projection: { _id: 1, name: 1 } });

const inTeamIds = allTeams.filter(({ name }) => teamNames.includes(name)).map(({ _id }) => _id);
Expand Down Expand Up @@ -118,3 +129,16 @@ export const validateLDAPRolesMappingChanges = () => {
}
});
};

export const validateLDAPTeamsMappingChanges = () => {
settings.get('LDAP_Groups_To_Rocket_Chat_Teams', (key, value) => {
try {
if (value) {
const mappedTeams = JSON.parse(value);
validateLDAPTeamsMappingStructure(mappedTeams);
}
} catch (error) {
logger.error(error);
}
});
};