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

[FIX] Sync of non existent field throws exception #8006

Merged
Merged
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
46 changes: 42 additions & 4 deletions packages/rocketchat-ldap/server/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ export function slug(text) {
}


export function getPropertyValue(obj, key) {
try {
return _.reduce(key.split('.'), (acc, el) => acc[el], obj);
} catch (err) {
return undefined;
}
}


export function getLdapUsername(ldapUser) {
const usernameField = RocketChat.settings.get('LDAP_Username_Field');

Expand Down Expand Up @@ -88,16 +97,45 @@ export function getDataToSyncUserData(ldapUser, user) {
break;

default:
if (!_.find(whitelistedUserFields, (el) => el === userField.split('.')[0])) {
const [outerKey, innerKeys] = userField.split(/\.(.+)/);

if (!_.find(whitelistedUserFields, (el) => el === outerKey)) {
logger.debug(`user attribute not whitelisted: ${ userField }`);
return;
}

if (outerKey === 'customFields') {
let customFieldsMeta;

try {
customFieldsMeta = JSON.parse(RocketChat.settings.get('Accounts_CustomFields'));
} catch (e) {
logger.debug('Invalid JSON for Custom Fields');
return;
}

if (!getPropertyValue(customFieldsMeta, innerKeys)) {
logger.debug(`user attribute does not exist: ${ userField }`);
return;
}
}

const tmpUserField = getPropertyValue(user, userField);
const tmpLdapField = RocketChat.templateVarHandler(ldapField, ldapUser);
const userFieldValue = _.reduce(userField.split('.'), (acc, el) => acc[el], user);

if (tmpLdapField && userFieldValue !== tmpLdapField) {
userData[userField] = tmpLdapField;
if (tmpLdapField && tmpUserField !== tmpLdapField) {
// creates the object structure instead of just assigning 'tmpLdapField' to
// 'userData[userField]' in order to avoid the "cannot use the part (...)
// to traverse the element" (MongoDB) error that can happen. Do not handle
// arrays.
// TODO: Find a better solution.
const dKeys = userField.split('.');
const lastKey = _.last(dKeys);
_.reduce(dKeys, (obj, currKey) =>
(currKey === lastKey)
? obj[currKey] = tmpLdapField
: obj[currKey] = obj[currKey] || {}
, userData);
logger.debug(`user.${ userField } changed to: ${ tmpLdapField }`);
}
}
Expand Down