From 43602c738808287b986b5ef30536a486dea5fa63 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Wed, 3 Apr 2024 14:07:06 +0200 Subject: [PATCH] use ldapjs --- modules/account/back/models/samba-config.js | 48 ++++++++++++++++----- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/modules/account/back/models/samba-config.js b/modules/account/back/models/samba-config.js index 2035cd0d4..d40474e10 100644 --- a/modules/account/back/models/samba-config.js +++ b/modules/account/back/models/samba-config.js @@ -182,25 +182,49 @@ module.exports = Self => { // Prepare data 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 - let roles = (await $.VnRole.find({ + let rolesBD = (await $.VnRole.find({ fields: ['id', 'name', 'description'], order: 'modified DESC', 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 - 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 - const rolesToDelete = differences(sambaCurrentRoles, rolesMap); + const rolesToDelete = differences(sambaCurrentRoles, rolesName); // Encontrar elementos a insertar - const rolesToInsert = differences(rolesMap, sambaCurrentRoles); + const rolesToInsert = differences(roles, sambaCurrentRoles); // Encontrar elementos a actualizar - const rolesToUpdate = differences(rolesMap, [...rolesToDelete, ...rolesToInsert]); + const rolesToUpdate = differences(roles, [...rolesToDelete, ...rolesToInsert]); // OBTENER USUARIOS Y SUS ROLES if ( @@ -249,12 +273,12 @@ module.exports = Self => { if (rolesToInsert.length > 0) { // PROCEDIMIENTO PARA INSERTAR ROLES 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)); // 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, '--random-password', '--must-change-at-next-login']) ) @@ -263,8 +287,8 @@ module.exports = Self => { resultsUserInsert.forEach(({stdout}) => console.log(stdout)); // PROCEDIMIENTO PARA INSERTAR USUARIOS ASOCIADOS AL ROL - let usersToGroup = rolesToInsert.flatMap(role => usersMap.get(role).map( - a => this.sambaTool('group', ['addmembers', `${ROLE_PREFIX}${role}`, a]) + let usersToGroup = rolesToInsert.flatMap(role => usersMap.get(role.name).map( + a => this.sambaTool('group', ['addmembers', role.vn, a]) ) ); const resultsUserGroup = await Promise.all(usersToGroup); @@ -273,6 +297,8 @@ module.exports = Self => { if (rolesToUpdate.length > 0) { let promises = []; + //OBTENER LDAPSJS MIEMBROS ROLES + for await (const role of rolesToUpdate) { const users = await this.sambaTool('group', ['listmembers', role]); const usersToDelete = differences(users, usersMap.get(role));