use ldapjs

This commit is contained in:
Javier Segarra 2024-04-03 14:07:06 +02:00
parent 64539a4a0d
commit 43602c7388
1 changed files with 37 additions and 11 deletions

View File

@ -182,25 +182,49 @@ module.exports = Self => {
// Prepare data // Prepare data
try { try {
const filter = '(cn=VN_*)'
const scope = 'sub'
const baseDN = 'cn=Users,dc=verdnatura,dc=es';
const ldapMembersGroups = await this.adClient.searchAll(baseDN,{
scope,
attributes: ['cn','member'],
filter
});
// OBTENER ROLES // OBTENER ROLES
let roles = (await $.VnRole.find({ let rolesBD = (await $.VnRole.find({
fields: ['id', 'name', 'description'], fields: ['id', 'name', 'description'],
order: 'modified DESC', order: 'modified DESC',
limit: 1 limit: 1
})); }));
let rolesMap = roles;//.map(({id, name, description}) => ({id, name, description})); let roles = rolesBD.map(({id, name, description}) => ({vn:`${ROLE_PREFIX}${name}`, name, id, description}));
let rolesName = roles.map(role=>role.name)
//OBTENER LDAPSJS ROLES
const ldapGroups = (await this.adClient.searchAll(baseDN,{
scope,
attributes: ['cn', 'description'],
filter
}))/*, (err, res)=>{
res.on('searchEntry', entry=>{
console.log(entry)
})
res.on('error', entry=>{
console.log(entry)
})
res.on('end', entry=>{
console.log(entry)
})
})*/
// OBTENER SAMBA ROLES // OBTENER SAMBA ROLES
let sambaCurrentRoles = handleExecResponse(await this.sambaTool('group', ['list'])).filter(group => group.startsWith(ROLE_PREFIX)); let sambaCurrentRoles = ldapGroups.map(({cn})=>cn);;// handleExecResponse(await this.sambaTool('group', ['list'])).filter(group => group.startsWith(ROLE_PREFIX));
// Encontrar elementos a eliminar // Encontrar elementos a eliminar
const rolesToDelete = differences(sambaCurrentRoles, rolesMap); const rolesToDelete = differences(sambaCurrentRoles, rolesName);
// Encontrar elementos a insertar // Encontrar elementos a insertar
const rolesToInsert = differences(rolesMap, sambaCurrentRoles); const rolesToInsert = differences(roles, sambaCurrentRoles);
// Encontrar elementos a actualizar // Encontrar elementos a actualizar
const rolesToUpdate = differences(rolesMap, [...rolesToDelete, ...rolesToInsert]); const rolesToUpdate = differences(roles, [...rolesToDelete, ...rolesToInsert]);
// OBTENER USUARIOS Y SUS ROLES // OBTENER USUARIOS Y SUS ROLES
if ( if (
@ -249,12 +273,12 @@ module.exports = Self => {
if (rolesToInsert.length > 0) { if (rolesToInsert.length > 0) {
// PROCEDIMIENTO PARA INSERTAR ROLES // PROCEDIMIENTO PARA INSERTAR ROLES
const resultsRoleInsert = await Promise.all( const resultsRoleInsert = await Promise.all(
rolesToInsert.map(({id, description,name}) => this.sambaTool('group', ['add', `${ROLE_PREFIX}${name}`, `--description="${description}"`])) rolesToInsert.map(({description,vn}) => this.sambaTool('group', ['add', vn, `--description="${description}"`]))
); );
resultsRoleInsert.forEach(({stdout}) => console.log(stdout)); resultsRoleInsert.forEach(({stdout}) => console.log(stdout));
// PROCEDIMIENTO PARA INSERTAR USUARIOS ASOCIADOS AL ROL // PROCEDIMIENTO PARA INSERTAR USUARIOS ASOCIADOS AL ROL
let usersToInsert = rolesToInsert.flatMap(role => usersMap.get(role.name).map( let usersToInsert = rolesToInsert.flatMap(({name: role} )=> usersMap.get(role).map(
a => this.sambaTool('user', ['add', a, a => this.sambaTool('user', ['add', a,
'--random-password', '--must-change-at-next-login']) '--random-password', '--must-change-at-next-login'])
) )
@ -263,8 +287,8 @@ module.exports = Self => {
resultsUserInsert.forEach(({stdout}) => console.log(stdout)); resultsUserInsert.forEach(({stdout}) => console.log(stdout));
// PROCEDIMIENTO PARA INSERTAR USUARIOS ASOCIADOS AL ROL // PROCEDIMIENTO PARA INSERTAR USUARIOS ASOCIADOS AL ROL
let usersToGroup = rolesToInsert.flatMap(role => usersMap.get(role).map( let usersToGroup = rolesToInsert.flatMap(role => usersMap.get(role.name).map(
a => this.sambaTool('group', ['addmembers', `${ROLE_PREFIX}${role}`, a]) a => this.sambaTool('group', ['addmembers', role.vn, a])
) )
); );
const resultsUserGroup = await Promise.all(usersToGroup); const resultsUserGroup = await Promise.all(usersToGroup);
@ -273,6 +297,8 @@ module.exports = Self => {
if (rolesToUpdate.length > 0) { if (rolesToUpdate.length > 0) {
let promises = []; let promises = [];
//OBTENER LDAPSJS MIEMBROS ROLES
for await (const role of rolesToUpdate) { for await (const role of rolesToUpdate) {
const users = await this.sambaTool('group', ['listmembers', role]); const users = await this.sambaTool('group', ['listmembers', role]);
const usersToDelete = differences(users, usersMap.get(role)); const usersToDelete = differences(users, usersMap.get(role));