Skip to content

Commit

Permalink
Regression: LDAP Refactoring (#23231)
Browse files Browse the repository at this point in the history
Co-authored-by: Guilherme Gazzo <guilhermegazzo@gmail.com>
  • Loading branch information
pierre-lehnen-rc and ggazzo authored Sep 23, 2021
1 parent 0a32226 commit c2ab5e1
Show file tree
Hide file tree
Showing 16 changed files with 525 additions and 346 deletions.
6 changes: 3 additions & 3 deletions app/lib/server/functions/setUserAvatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Users } from '../../../models/server';
import { SystemLogger } from '../../../../server/lib/logger/system';
import { api } from '../../../../server/sdk/api';

export const setUserAvatar = function(user, dataURI, contentType, service) {
export const setUserAvatar = function(user, dataURI, contentType, service, etag = null) {
let encoding;
let image;

Expand Down Expand Up @@ -64,8 +64,8 @@ export const setUserAvatar = function(user, dataURI, contentType, service) {

fileStore.insert(file, buffer, (err, result) => {
Meteor.setTimeout(function() {
Users.setAvatarData(user._id, service, result.etag);
api.broadcast('user.avatarUpdate', { username: user.username, avatarETag: result.etag });
Users.setAvatarData(user._id, service, etag || result.etag);
api.broadcast('user.avatarUpdate', { username: user.username, avatarETag: etag || result.etag });
}, 500);
});
};
14 changes: 13 additions & 1 deletion app/models/server/raw/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,24 @@ export class UsersRaw extends BaseRaw {
return this.findOne(query);
}

async findLDAPUsers(options) {
findLDAPUsers(options) {
const query = { ldap: true };

return this.find(query, options);
}

findConnectedLDAPUsers(options) {
const query = {
ldap: true,
'services.resume.loginTokens': {
$exists: true,
$ne: [],
},
};

return this.find(query, options);
}

isUserInRole(userId, roleName) {
const query = {
_id: userId,
Expand Down
4 changes: 4 additions & 0 deletions definition/ldap/ILDAPOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ export interface ILDAPConnectionOptions {
groupFilterGroupMemberAttribute?: string;
groupFilterGroupMemberFormat?: string;
groupFilterGroupName?: string;
authentication: boolean;
authenticationUserDN: string;
authenticationPassword: string;
attributesToQuery: Array<string>;
}
5 changes: 0 additions & 5 deletions ee/definition/ldap/ILDAPEEOptions.ts

This file was deleted.

65 changes: 43 additions & 22 deletions ee/server/configuration/ldap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,61 @@ import { LDAPEE } from '../sdk';
import { settings } from '../../../app/settings/server';
import { logger } from '../../../server/lib/ldap/Logger';
import { cronJobs } from '../../../app/utils/server/lib/cron/Cronjobs';
import { LDAPEEConnection } from '../lib/ldap/Connection';
import { LDAPEEManager } from '../lib/ldap/Manager';
import { callbacks } from '../../../app/callbacks/server';
import type { LDAPConnection } from '../../../server/lib/ldap/Connection';
import type { IImportUser } from '../../../definition/IImportUser';
import type { ILDAPEntry } from '../../../definition/ldap/ILDAPEntry';
import type { SettingValue } from '../../../definition/ISetting';
import type { SettingCallback } from '../../../app/settings/lib/settings';
import { onLicense } from '../../app/license/server';
import { addSettings } from '../settings/ldap';

Meteor.startup(() => onLicense('ldap-enterprise', () => {
addSettings();

onLicense('ldap-enterprise', () => {
// Configure background sync cronjob
const jobName = 'LDAP_Sync';
const addCronJob = _.debounce(Meteor.bindEnvironment(function addCronJobDebounced() {
if (settings.get('LDAP_Background_Sync') !== true) {
logger.info('Disabling LDAP Background Sync');
if (cronJobs.nextScheduledAtDate(jobName)) {
cronJobs.remove(jobName);
function configureBackgroundSync(jobName: string, enableSetting: string, intervalSetting: string, cb: () => {}): SettingCallback {
let lastSchedule: string;

return _.debounce(Meteor.bindEnvironment(function addCronJobDebounced() {
if (settings.get('LDAP_Enable') !== true || settings.get(enableSetting) !== true) {
if (cronJobs.nextScheduledAtDate(jobName)) {
logger.info({ msg: 'Disabling LDAP Background Sync', jobName });
cronJobs.remove(jobName);
}
return;
}

const schedule = settings.get<string>(intervalSetting);
if (schedule) {
if (schedule !== lastSchedule && cronJobs.nextScheduledAtDate(jobName)) {
cronJobs.remove(jobName);
}

lastSchedule = schedule;
logger.info({ msg: 'Enabling LDAP Background Sync', jobName });
cronJobs.add(jobName, schedule, () => cb(), 'text');
}
return;
}
}), 500);
}

const schedule = settings.get<string>('LDAP_Background_Sync_Interval');
if (schedule) {
logger.info('Enabling LDAP Background Sync');
cronJobs.add(jobName, schedule, () => Promise.await(LDAPEE.sync()), 'text');
}
}), 500);
const addCronJob = configureBackgroundSync('LDAP_Sync', 'LDAP_Background_Sync', 'LDAP_Background_Sync_Interval', () => Promise.await(LDAPEE.sync()));
const addAvatarCronJob = configureBackgroundSync('LDAP_AvatarSync', 'LDAP_Background_Sync_Avatars', 'LDAP_Background_Sync_Avatars_Interval', () => Promise.await(LDAPEE.syncAvatars()));
const addLogoutCronJob = configureBackgroundSync('LDAP_AutoLogout', 'LDAP_Sync_AutoLogout_Enabled', 'LDAP_Sync_AutoLogout_Interval', () => Promise.await(LDAPEE.syncLogout()));

Meteor.defer(() => {
settings.get('LDAP_Background_Sync', addCronJob);
settings.get('LDAP_Background_Sync_Interval', addCronJob);
settings.get('LDAP_Background_Sync_Avatars', addAvatarCronJob);
settings.get('LDAP_Background_Sync_Avatars_Interval', addAvatarCronJob);
settings.get('LDAP_Sync_AutoLogout_Enabled', addLogoutCronJob);
settings.get('LDAP_Sync_AutoLogout_Interval', addLogoutCronJob);

settings.get('LDAP_Enable', (key: string, value: SettingValue, initialLoad?: boolean) => {
addCronJob(key, value, initialLoad);
addAvatarCronJob(key, value, initialLoad);
addLogoutCronJob(key, value, initialLoad);
});

settings.get('LDAP_Groups_To_Rocket_Chat_Teams', (_key, value) => {
try {
Expand All @@ -46,12 +71,8 @@ onLicense('ldap-enterprise', () => {
});
});

callbacks.add('getLDAPConnectionClass', function(): typeof LDAPConnection {
return LDAPEEConnection;
}, callbacks.priority.HIGH, 'replaceLDAPConnectionClass');

callbacks.add('mapLDAPUserData', (userData: IImportUser, ldapUser: ILDAPEntry) => {
LDAPEEManager.copyCustomFields(ldapUser, userData);
LDAPEEManager.copyActiveState(ldapUser, userData);
}, callbacks.priority.MEDIUM, 'mapLDAPCustomFields');
});
}));
65 changes: 0 additions & 65 deletions ee/server/lib/ldap/Connection.ts

This file was deleted.

Loading

0 comments on commit c2ab5e1

Please sign in to comment.