mirror of
https://git.cloudron.io/cloudron/mastodon-app.git
synced 2024-05-23 11:38:06 +00:00
46 lines
2.3 KiB
JavaScript
Executable file
46 lines
2.3 KiB
JavaScript
Executable file
#!/usr/bin/env node
|
|
|
|
'use strict';
|
|
|
|
const execSync = require('child_process').execSync;
|
|
|
|
const ldapUserSearchCmd = `ldapsearch -LLL -x -H "${process.env.CLOUDRON_LDAP_URL}" -D "${process.env.CLOUDRON_LDAP_BIND_DN}" -w "${process.env.CLOUDRON_LDAP_BIND_PASSWORD}" -b "${process.env.CLOUDRON_LDAP_USERS_BASE_DN}"`
|
|
const postgresCmd = `PGPASSWORD=${process.env.CLOUDRON_POSTGRESQL_PASSWORD} psql -h ${process.env.CLOUDRON_POSTGRESQL_HOST} -p ${process.env.CLOUDRON_POSTGRESQL_PORT} -U ${process.env.CLOUDRON_POSTGRESQL_USERNAME} -d ${process.env.CLOUDRON_POSTGRESQL_DATABASE}`
|
|
|
|
let usersTableExists = execSync(`${postgresCmd} -AXqtc "SELECT count(*) FROM information_schema.tables WHERE table_schema LIKE 'public' AND table_type LIKE 'BASE TABLE' AND table_name = 'users'"`);
|
|
if (usersTableExists == 0) {
|
|
console.log("DB hasn't been initialised yet. Nothing to migrate.");
|
|
process.exit(0);
|
|
}
|
|
|
|
let ldapProfiles = execSync(`${postgresCmd} -AXqtc "SELECT count(*) FROM users u JOIN accounts a ON a.id=u.account_id LEFT JOIN identities i ON i.uid=a.username WHERE i.uid IS NULL"`);
|
|
console.log(`LDAP profiles in DB: ${ldapProfiles}`);
|
|
if (ldapProfiles == 0) {
|
|
console.log("Nothing to migrate. All users' profiles are up-to-date.");
|
|
process.exit(0);
|
|
}
|
|
|
|
const ldapSearchOutput = execSync(ldapUserSearchCmd, { encoding: 'utf8' });
|
|
|
|
const users = {};
|
|
|
|
let userId;
|
|
for (let line of ldapSearchOutput.split('\n')) {
|
|
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 id in users) {
|
|
let uid = execSync(`${postgresCmd} -AXqtc "SELECT u.id FROM users u JOIN accounts a ON a.id=u.account_id WHERE NOT EXISTS (SELECT 1 FROM identities WHERE provider='openid_connect' AND uid='${users[id]}') AND a.username='${users[id]}'"`).toString().trim();
|
|
if (!uid)
|
|
continue;
|
|
|
|
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');
|