From ca1ead228fcce6d7b19b53b3124fba5ebe8345c9 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Tue, 30 Jan 2024 08:46:53 +0100 Subject: [PATCH] refs #5770 feat usersMap --- .../back/models/specs/samba-config.spec.js | 52 ++++++++++--------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/modules/account/back/models/specs/samba-config.spec.js b/modules/account/back/models/specs/samba-config.spec.js index 7ccb32cf8..80a7b949d 100644 --- a/modules/account/back/models/specs/samba-config.spec.js +++ b/modules/account/back/models/specs/samba-config.spec.js @@ -88,17 +88,16 @@ async function syncRoles() { // OBTENER USUARIOS Y SUS ROLES if (rolesToDelete.length > 0 || rolesToInsert.length > 0) { let users = await $.VnUser.find({ - - include: { - relation: 'role', - scope: {fields: ['name'], - where: {'name': {inq: rolesToInsert}} - } - }, - fields: ['name', 'roleFk'], - where: {'active': true} + include: { + relation: 'role', + scope: {fields: ['name'], + where: {'name': {inq: rolesToInsert}} + } + }, + fields: ['name', 'roleFk'], + where: {'active': true} }); - let accounts = await $.Account.find({ + /* let accounts = await $.Account.find({ fields: ['id'], include: { relation: 'user', @@ -113,20 +112,21 @@ async function syncRoles() { where: {'active': true} } } - }); - let userMap = toMap(users, user => { + });*/ + let usersMap = toMap(users, user => { let role = user.role(); if (!role) return; return {key: role.name, val: user.name}; }); - console.log(userMap); - let accountMap = toMap(accounts, e => { + usersMap.set('group1', ['employee']); + console.log(usersMap); + /* toMap(accounts, e => { let user = e.user(); if (!user) return; let role = user.role(); if (!role) return; return {key: role.name, val: user.name}; - }); + });*/ // for (const [key, value] of accountMap) { // value.forEach(v => { // console.log(v); @@ -137,17 +137,18 @@ async function syncRoles() { if (rolesToDelete.length > 0) { // PROCEDIMIENTO PARA ELIMINAR USUARIOS ASOCIADOS AL ROL let usersToDelete = rolesToDelete.flatMap(role => { - const exist = accountMap.get(role); + const exist = usersMap.get(role); if (exist) { - return accountMap.get(role)?.map( + return usersMap.get(role)?.map( a => sambaTool('group', ['removemembers', role, a]) ); } else return []; } ); const resultsUserDelete = await Promise.all(usersToDelete); - expect(resultsUserDelete.length).toEqual(0); + expect(resultsUserDelete.length).toEqual(1); + expect(resultsUserDelete[0]).toEqual('samba-tool removemembers group1 employee'); // PROCEDIMIENTO PARA ELIMINAR ROLES const resultsRoleDelete = await Promise.all( rolesToDelete.map(role => sambaTool('group', ['delete', role])) @@ -158,7 +159,14 @@ async function syncRoles() { if (rolesToInsert.length > 0) { // PROCEDIMIENTO PARA INSERTAR USUARIOS ASOCIADOS AL ROL - let usersToInsert = rolesToInsert.flatMap(role => accountMap.get(role).map( + const resultsRoleInsert = await Promise.all( + rolesToInsert.map(role => sambaTool('group', ['add', role])) + ); + expect(resultsRoleInsert.length).toEqual(2); + expect(resultsRoleInsert[0]).toEqual('samba-tool add administrative'); + expect(resultsRoleInsert[1]).toEqual('samba-tool add guest'); + + let usersToInsert = rolesToInsert.flatMap(role => usersMap.get(role).map( a => sambaTool('group', ['addmembers', role, a]) ) ); @@ -167,12 +175,6 @@ async function syncRoles() { expect(resultsUserInsert[0]).toEqual('samba-tool addmembers administrative administrative'); expect(resultsUserInsert[1]).toEqual('samba-tool addmembers guest guest'); // PROCEDIMIENTO PARA INSERTAR ROLES - const resultsRoleInsert = await Promise.all( - rolesToInsert.map(role => sambaTool('group', ['add', role])) - ); - expect(resultsRoleInsert.length).toEqual(2); - expect(resultsRoleInsert[0]).toEqual('samba-tool add administrative'); - expect(resultsRoleInsert[1]).toEqual('samba-tool add guest'); } } // let roleRoles = await $.RoleRole.find({