migrateUsers.js updated to use username instead of email

This commit is contained in:
Vladimir D 2024-02-15 17:18:05 +04:00
parent e556c856f1
commit a08af787d7

View file

@ -26,20 +26,20 @@ const users = {};
let userId;
for (let line of ldapSearchOutput.split('\n')) {
if (line.startsWith('mail:')) userId = line.split(':')[1].trim();
if (line.startsWith('uid:')) userId = line.split(':')[1].trim();
if (line.startsWith('username:')) users[userId] = line.split(':')[1].trim();
}
console.log('Found usermapping:', users);
for (let email in users) {
let uid = execSync(`${postgresCmd} -AXqtc "SELECT id FROM users WHERE email='${email}'"`).toString().trim();
for (let id in users) {
let uid = execSync(`${postgresCmd} -AXqtc "SELECT u.id FROM users u JOIN accounts a ON a.id=u.account_id WHERE a.username='${users[id]}'"`).toString().trim();
if (!uid)
continue;
console.log(`Migrate ${email}/${users[email]} to OIDC`);
execSync(`${postgresCmd} -c "INSERT INTO identities (provider, uid, user_id, created_at, updated_at) VALUES ('openid_connect', '${users[email]}', ${uid}, NOW(), NOW())"`);
console.log(`"INSERT INTO identities (provider, uid, user_id, created_at, updated_at) VALUES ('openid_connect', '${users[email]}', ${uid}, NOW(), NOW())"`);
console.log(`Migrate ${users[id]} to OIDC`);
execSync(`${postgresCmd} -c "INSERT INTO identities (provider, uid, user_id, created_at, updated_at) VALUES ('openid_connect', '${users[id]}', ${uid}, NOW(), NOW())"`);
console.log(`"INSERT INTO identities (provider, uid, user_id, created_at, updated_at) VALUES ('openid_connect', '${users[id]}', ${uid}, NOW(), NOW())"`);
}
console.log('Done');