mirror of
https://github.com/wekan/wekan.git
synced 2026-03-06 05:40:16 +01:00
Migrate wekan-ldap to async API for Meteor 3.0
- Replace Meteor.wrapAsync with native Promises - Convert all sync methods to async - Use async DB operations (findOneAsync, updateAsync) - Bump version 0.0.2 → 0.1.0
This commit is contained in:
parent
eb0c9ac1e6
commit
7d56dca80b
6 changed files with 102 additions and 83 deletions
|
|
@ -230,7 +230,7 @@ export function getDataToSyncUserData(ldapUser, user) {
|
|||
}
|
||||
|
||||
|
||||
export function syncUserData(user, ldapUser) {
|
||||
export async function syncUserData(user, ldapUser) {
|
||||
log_info('Syncing user data');
|
||||
log_debug('user', {'email': user.email, '_id': user._id});
|
||||
// log_debug('ldapUser', ldapUser.object);
|
||||
|
|
@ -239,7 +239,7 @@ export function syncUserData(user, ldapUser) {
|
|||
const username = slug(getLdapUsername(ldapUser));
|
||||
if (user && user._id && username !== user.username) {
|
||||
log_info('Syncing user username', user.username, '->', username);
|
||||
Meteor.users.findOne({ _id: user._id }, { $set: { username }});
|
||||
await Meteor.users.findOneAsync({ _id: user._id }, { $set: { username }});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -248,7 +248,7 @@ export function syncUserData(user, ldapUser) {
|
|||
log_debug('fullname=',fullname);
|
||||
if (user && user._id && fullname !== '') {
|
||||
log_info('Syncing user fullname:', fullname);
|
||||
Meteor.users.update({ _id: user._id }, { $set: { 'profile.fullname' : fullname, }});
|
||||
await Meteor.users.updateAsync({ _id: user._id }, { $set: { 'profile.fullname' : fullname, }});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -258,7 +258,7 @@ export function syncUserData(user, ldapUser) {
|
|||
|
||||
if (user && user._id && email !== '') {
|
||||
log_info('Syncing user email:', email);
|
||||
Meteor.users.update({
|
||||
await Meteor.users.updateAsync({
|
||||
_id: user._id
|
||||
}, {
|
||||
$set: {
|
||||
|
|
@ -270,7 +270,7 @@ export function syncUserData(user, ldapUser) {
|
|||
|
||||
}
|
||||
|
||||
export function addLdapUser(ldapUser, username, password) {
|
||||
export async function addLdapUser(ldapUser, username, password) {
|
||||
const uniqueId = getLdapUserUniqueID(ldapUser);
|
||||
|
||||
const userObject = {
|
||||
|
|
@ -310,7 +310,7 @@ export function addLdapUser(ldapUser, username, password) {
|
|||
userObject._id = Accounts.createUser(userObject);
|
||||
|
||||
// Add the services.ldap identifiers
|
||||
Meteor.users.update({ _id: userObject._id }, {
|
||||
await Meteor.users.updateAsync({ _id: userObject._id }, {
|
||||
$set: {
|
||||
'services.ldap': { id: uniqueId.value },
|
||||
'emails.0.verified': true,
|
||||
|
|
@ -321,14 +321,14 @@ export function addLdapUser(ldapUser, username, password) {
|
|||
return error;
|
||||
}
|
||||
|
||||
syncUserData(userObject, ldapUser);
|
||||
await syncUserData(userObject, ldapUser);
|
||||
|
||||
return {
|
||||
userId: userObject._id,
|
||||
};
|
||||
}
|
||||
|
||||
export function importNewUsers(ldap) {
|
||||
export async function importNewUsers(ldap) {
|
||||
if (LDAP.settings_get('LDAP_ENABLE') !== true) {
|
||||
log_error('Can\'t run LDAP Import, LDAP is disabled');
|
||||
return;
|
||||
|
|
@ -336,16 +336,16 @@ export function importNewUsers(ldap) {
|
|||
|
||||
if (!ldap) {
|
||||
ldap = new LDAP();
|
||||
ldap.connectSync();
|
||||
await ldap.connect();
|
||||
}
|
||||
|
||||
let count = 0;
|
||||
ldap.searchUsersSync('*', Meteor.bindEnvironment((error, ldapUsers, {next, end} = {}) => {
|
||||
ldap.searchUsers('*', Meteor.bindEnvironment(async (error, ldapUsers, {next, end} = {}) => {
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
ldapUsers.forEach((ldapUser) => {
|
||||
for (const ldapUser of ldapUsers) {
|
||||
count++;
|
||||
|
||||
const uniqueId = getLdapUserUniqueID(ldapUser);
|
||||
|
|
@ -362,7 +362,7 @@ export function importNewUsers(ldap) {
|
|||
}
|
||||
|
||||
// Add user if it was not added before
|
||||
let user = Meteor.users.findOne(userQuery);
|
||||
let user = await Meteor.users.findOneAsync(userQuery);
|
||||
|
||||
if (!user && username && LDAP.settings_get('LDAP_MERGE_EXISTING_USERS') === true) {
|
||||
const userQuery = {
|
||||
|
|
@ -371,20 +371,20 @@ export function importNewUsers(ldap) {
|
|||
|
||||
log_debug('userQuery merge', userQuery);
|
||||
|
||||
user = Meteor.users.findOne(userQuery);
|
||||
user = await Meteor.users.findOneAsync(userQuery);
|
||||
if (user) {
|
||||
syncUserData(user, ldapUser);
|
||||
await syncUserData(user, ldapUser);
|
||||
}
|
||||
}
|
||||
|
||||
if (!user) {
|
||||
addLdapUser(ldapUser, username);
|
||||
await addLdapUser(ldapUser, username);
|
||||
}
|
||||
|
||||
if (count % 100 === 0) {
|
||||
log_info('Import running. Users imported until now:', count);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (end) {
|
||||
log_info('Import finished. Users imported:', count);
|
||||
|
|
@ -394,7 +394,7 @@ export function importNewUsers(ldap) {
|
|||
}));
|
||||
}
|
||||
|
||||
function sync() {
|
||||
async function sync() {
|
||||
if (LDAP.settings_get('LDAP_ENABLE') !== true) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -402,7 +402,7 @@ function sync() {
|
|||
const ldap = new LDAP();
|
||||
|
||||
try {
|
||||
ldap.connectSync();
|
||||
await ldap.connect();
|
||||
|
||||
let users;
|
||||
if (LDAP.settings_get('LDAP_BACKGROUND_SYNC_KEEP_EXISTANT_USERS_UPDATED') === true) {
|
||||
|
|
@ -410,25 +410,25 @@ function sync() {
|
|||
}
|
||||
|
||||
if (LDAP.settings_get('LDAP_BACKGROUND_SYNC_IMPORT_NEW_USERS') === true) {
|
||||
importNewUsers(ldap);
|
||||
await importNewUsers(ldap);
|
||||
}
|
||||
|
||||
if (LDAP.settings_get('LDAP_BACKGROUND_SYNC_KEEP_EXISTANT_USERS_UPDATED') === true) {
|
||||
users.forEach(function(user) {
|
||||
for await (const user of users) {
|
||||
let ldapUser;
|
||||
|
||||
if (user.services && user.services.ldap && user.services.ldap.id) {
|
||||
ldapUser = ldap.getUserByIdSync(user.services.ldap.id, user.services.ldap.idAttribute);
|
||||
ldapUser = await ldap.getUserById(user.services.ldap.id, user.services.ldap.idAttribute);
|
||||
} else {
|
||||
ldapUser = ldap.getUserByUsernameSync(user.username);
|
||||
ldapUser = await ldap.getUserByUsername(user.username);
|
||||
}
|
||||
|
||||
if (ldapUser) {
|
||||
syncUserData(user, ldapUser);
|
||||
await syncUserData(user, ldapUser);
|
||||
} else {
|
||||
log_info('Can\'t sync user', user.username);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
log_error(error);
|
||||
|
|
@ -459,8 +459,8 @@ const addCronJob = _.debounce(Meteor.bindEnvironment(function addCronJobDebounce
|
|||
else {
|
||||
return parser.recur().on(0).minute();
|
||||
}},
|
||||
job: function() {
|
||||
sync();
|
||||
job: async function() {
|
||||
await sync();
|
||||
},
|
||||
});
|
||||
sc.start();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue