2020-11-12 22:20:25 +00:00
|
|
|
|
2023-06-07 11:34:13 +00:00
|
|
|
const models = require('vn-loopback/server/server').models;
|
2020-11-12 22:20:25 +00:00
|
|
|
|
|
|
|
module.exports = Self => {
|
|
|
|
Object.assign(Self, {
|
2023-11-20 17:39:37 +00:00
|
|
|
linkers: [],
|
2020-11-12 22:20:25 +00:00
|
|
|
|
2023-11-20 17:39:37 +00:00
|
|
|
addLinker(linker) {
|
|
|
|
this.linkers.push(linker);
|
2020-11-12 22:20:25 +00:00
|
|
|
},
|
|
|
|
|
2023-11-20 17:39:37 +00:00
|
|
|
async initEngine() {
|
|
|
|
const accountConfig = await Self.findOne({
|
2020-11-13 09:33:34 +00:00
|
|
|
fields: ['homedir', 'shell', 'idBase']
|
|
|
|
});
|
2023-11-20 17:39:37 +00:00
|
|
|
const mailConfig = await models.MailConfig.findOne({
|
|
|
|
fields: ['domain']
|
|
|
|
});
|
2020-11-12 22:20:25 +00:00
|
|
|
|
2023-11-20 17:39:37 +00:00
|
|
|
const linkers = [];
|
2020-11-12 22:20:25 +00:00
|
|
|
|
2023-11-20 17:39:37 +00:00
|
|
|
for (const Linker of Self.linkers) {
|
|
|
|
const linker = await Linker.getLinker();
|
|
|
|
if (!linker) continue;
|
|
|
|
Object.assign(linker, {accountConfig});
|
|
|
|
await linker.init();
|
|
|
|
linkers.push(linker);
|
2020-11-12 22:20:25 +00:00
|
|
|
}
|
|
|
|
|
2023-11-20 17:39:37 +00:00
|
|
|
Object.assign(accountConfig, {
|
|
|
|
linkers,
|
|
|
|
domain: mailConfig.domain
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
accountConfig,
|
|
|
|
linkers
|
|
|
|
};
|
2020-11-12 22:20:25 +00:00
|
|
|
},
|
|
|
|
|
2023-11-20 17:39:37 +00:00
|
|
|
async deinitEngine(engine) {
|
|
|
|
for (const linker of engine.linkers)
|
|
|
|
await linker.deinit();
|
2020-11-12 22:20:25 +00:00
|
|
|
},
|
|
|
|
|
2023-11-20 17:39:37 +00:00
|
|
|
async syncUser(userName, password) {
|
|
|
|
const engine = await Self.initEngine();
|
2020-11-12 22:20:25 +00:00
|
|
|
try {
|
2023-11-20 17:39:37 +00:00
|
|
|
await Self.syncUserBase(engine, userName, password, true);
|
2020-11-12 22:20:25 +00:00
|
|
|
} finally {
|
2023-11-20 17:39:37 +00:00
|
|
|
await Self.deinitEngine(engine);
|
2020-11-12 22:20:25 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-11-20 17:39:37 +00:00
|
|
|
async syncUsers() {
|
|
|
|
const engine = await Self.initEngine();
|
2020-11-12 22:20:25 +00:00
|
|
|
|
2023-11-20 17:39:37 +00:00
|
|
|
let usersToSync = new Set();
|
|
|
|
for (const linker of engine.linkers)
|
|
|
|
await linker.getUsers(usersToSync);
|
2020-11-12 22:20:25 +00:00
|
|
|
|
2023-11-20 17:39:37 +00:00
|
|
|
usersToSync = Array.from(usersToSync.values())
|
|
|
|
.sort((a, b) => a.localeCompare(b));
|
2020-11-12 22:20:25 +00:00
|
|
|
|
2023-11-20 17:39:37 +00:00
|
|
|
for (let userName of usersToSync) {
|
|
|
|
try {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log(`Synchronizing user '${userName}'`);
|
2020-11-12 22:20:25 +00:00
|
|
|
|
2023-11-20 17:39:37 +00:00
|
|
|
await Self.syncUserBase(engine, userName);
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log(` -> User '${userName}' sinchronized`);
|
|
|
|
} catch (err) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(` -> User '${userName}' synchronization error:`, err.message);
|
|
|
|
}
|
|
|
|
}
|
2020-11-12 22:20:25 +00:00
|
|
|
|
2023-11-20 17:39:37 +00:00
|
|
|
await Self.deinitEngine(engine);
|
|
|
|
await Self.syncRoles();
|
2020-11-12 22:20:25 +00:00
|
|
|
},
|
|
|
|
|
2023-11-20 17:39:37 +00:00
|
|
|
async syncUserBase(engine, userName, password, syncGroups) {
|
2020-11-12 22:20:25 +00:00
|
|
|
if (!userName) return;
|
|
|
|
userName = userName.toLowerCase();
|
|
|
|
|
|
|
|
// Skip conflicting users
|
|
|
|
if (['administrator', 'root'].indexOf(userName) >= 0)
|
|
|
|
return;
|
|
|
|
|
2023-11-20 17:39:37 +00:00
|
|
|
const user = await models.VnUser.findOne({
|
2020-11-12 22:20:25 +00:00
|
|
|
where: {name: userName},
|
|
|
|
fields: [
|
|
|
|
'id',
|
|
|
|
'nickname',
|
|
|
|
'email',
|
|
|
|
'lang',
|
|
|
|
'roleFk',
|
|
|
|
'sync',
|
|
|
|
'active',
|
|
|
|
'created',
|
2023-06-06 13:09:01 +00:00
|
|
|
'password',
|
2020-11-12 22:20:25 +00:00
|
|
|
'updated'
|
|
|
|
],
|
2022-01-20 14:33:53 +00:00
|
|
|
include: [
|
|
|
|
{
|
|
|
|
relation: 'roles',
|
|
|
|
scope: {
|
|
|
|
include: {
|
|
|
|
relation: 'inherits',
|
|
|
|
scope: {
|
|
|
|
fields: ['name']
|
|
|
|
}
|
2020-11-12 22:20:25 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-20 14:33:53 +00:00
|
|
|
}, {
|
|
|
|
relation: 'role',
|
|
|
|
fields: ['name']
|
2020-11-12 22:20:25 +00:00
|
|
|
}
|
2022-01-20 14:33:53 +00:00
|
|
|
]
|
2020-11-12 22:20:25 +00:00
|
|
|
});
|
|
|
|
|
2023-11-20 17:39:37 +00:00
|
|
|
const info = {
|
2020-11-12 22:20:25 +00:00
|
|
|
user,
|
|
|
|
hasAccount: false
|
|
|
|
};
|
|
|
|
|
|
|
|
if (user) {
|
2023-11-20 17:39:37 +00:00
|
|
|
const exists = await models.Account.exists(user.id);
|
|
|
|
const {accountConfig} = engine;
|
2020-11-12 22:20:25 +00:00
|
|
|
Object.assign(info, {
|
|
|
|
hasAccount: user.active && exists,
|
2023-11-20 17:39:37 +00:00
|
|
|
corporateMail: `${userName}@${accountConfig.domain}`,
|
|
|
|
uidNumber: accountConfig.idBase + user.id
|
2020-11-12 22:20:25 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-11-20 17:39:37 +00:00
|
|
|
const errs = [];
|
2020-11-12 22:20:25 +00:00
|
|
|
|
2023-11-20 17:39:37 +00:00
|
|
|
for (const linker of engine.linkers) {
|
2020-11-12 22:20:25 +00:00
|
|
|
try {
|
2023-11-20 17:39:37 +00:00
|
|
|
await linker.syncUser(userName, info, password);
|
2020-11-12 22:20:25 +00:00
|
|
|
if (syncGroups)
|
2023-11-20 17:39:37 +00:00
|
|
|
await linker.syncUserGroups(userName, info);
|
2020-11-12 22:20:25 +00:00
|
|
|
} catch (err) {
|
|
|
|
errs.push(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errs.length) throw errs[0];
|
|
|
|
},
|
|
|
|
|
2023-11-20 17:39:37 +00:00
|
|
|
async syncRoles() {
|
|
|
|
const engine = await Self.initEngine();
|
|
|
|
try {
|
|
|
|
await Self.rawSql(`CALL account.role_sync`);
|
2020-11-12 22:20:25 +00:00
|
|
|
|
2023-11-20 17:39:37 +00:00
|
|
|
for (const linker of engine.linkers)
|
|
|
|
await linker.syncRoles();
|
|
|
|
} finally {
|
|
|
|
await Self.deinitEngine(engine);
|
|
|
|
}
|
2020-11-12 22:20:25 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|